Are variables declared with let or const hoisted?

@thefourtheye is correct in saying that these variables cannot be accessed before they are declared. However, it’s a bit more complicated than that. Are variables declared with let or const not hoisted? What is really going on here? All declarations (var, let, const, function, function*, class) are “hoisted” in JavaScript. This means that if a … Read more

Does ES6 introduce a well-defined order of enumeration for object properties?

Note: As of ES2020, even older operations like for-in and Object.keys are required to follow property order. That doesn’t change the fact that using property order for fundamental program logic probably isn’t a good idea, since the order for non-integer-index properties depends on when the properties were created. Answer for ES2015-ES2019: For for-in, Object.keys, and … Read more

setState doesn’t update the state immediately

Your state needs some time to mutate, and since console.log(this.state.boardAddModalShow) executes before the state mutates, you get the previous value as output. So you need to write the console in the callback to the setState function openAddBoardModal() { this.setState({ boardAddModalShow: true }, function () { console.log(this.state.boardAddModalShow); }); } setState is asynchronous. It means you can’t … Read more

When should I use a return statement in ES6 arrow functions

Jackson has partially answered this in a similar question: Implicit return, but only if there is no block. This will result in errors when a one-liner expands to multiple lines and the programmer forgets to add a return. Implicit return is syntactically ambiguous. (name) => {id: name}returns the object {id: name}… right? Wrong. It returns … Read more

What’s the meaning of “=>” (an arrow formed from equals & greater than) in JavaScript?

What It Is This is an arrow function. Arrow functions are a short syntax, introduced by ECMAscript 6, that can be used similarly to the way you would use function expressions. In other words, you can often use them in place of expressions like function (foo) {…}. But they have some important differences. For example, … Read more

Are ‘Arrow Functions’ and ‘Functions’ equivalent / interchangeable?

tl;dr: No! Arrow functions and function declarations / expressions are not equivalent and cannot be replaced blindly. If the function you want to replace does not use this, arguments and is not called with new, then yes. As so often: it depends. Arrow functions have different behavior than function declarations / expressions, so let’s have … Read more