One of the most foundational things to understand about JavaScript is that programs are made up of statements, and statements have slots for expressions. In this blog post, we'll dig into how these two structures work, and see how building an intuition about this can help us solve practical problems.
JavaScript forEach() – JS Array For Each Loop Example
When working with arrays, there will be times when you need to loop or iterate through the array's values in order to either output or manipulate them. These arrays can hold any datatype, including objects, numbers, strings, and many others. In this article, we'll look at how you can
An Interactive Guide to JavaScript Events | Aleksandr Hovhannisyan
Learn how event capturing, targeting, and bubbling work in JavaScript; how to prevent an event's default behavior; how to stop event propagation; and more.
Recently built a demo that demonstrated a specific animation. Only problem: if you missed it, you had no way of restarting it. Instead of forcing the visitor to reload the page, this little JavaScript-snippet – attached to a button click – did the trick: const restartAnimations = ($el) = { $el.getAnimations().forEach((anim) = { anim.cancel(); anim.play(); … Continue reading "JavaScript: Restart all Animations of an Element"
A few months ago, I saw someone one Twitter ask:
Can we create an object by looping on some data 🤔
Today, I wanted to write about how to do just that. Let’s dig in!
An example of some data Let’s imagine you got back an array of data from an API service called WizardSchool. It provided you with a list of wizards.
Each wizard in the array is itself an object that contains the wizard’s name, and an array of spells that they know how to cast.
How not to sort an array in JavaScript | Phil Nash
Array sorting is one of those things you don’t spend too long thinking about, until it stops working for you. Recently I was working with array of items in JavaScript that were not sorting at all p...
Why is JavaScript event delegation better than attaching events to each ele
I typically recommend using event delegation for event listeners instead of attaching them to individual elements. Let’s say wanted to listen to clicks on every element with the .sandwich class. You might do this. var sandwiches = document.querySelectorAll('.sandwich'); sandwiches.forEach(function (sandwich) { sandwich.addEventListener('click', function (event) { console.log(sandwich); }, false); }); But with event delegation, you would listen for all clicks on the document and ignore ones on elements without the .
What's the difference between JavaScript event delegation, bubbling, and ca
Yesterday, I wrote about why event delegation is better than attaching events to specific elements. In response, my buddy Andrew Borstein asked: What’s the difference between event delegation/bubbling/capturing? This is a great question that I get fairly often. The terms are often used interchangeably (sometimes by me, oops!), which can cause some confusion. So let’s clear that up today. tl;dr: event delegation is the technique, bubbling is what the event itself does, and capturing is a way of using event delgation on events that don’t bubble.