https://bgoonz-blog.netlify.app/docs/javascript/review/

Javascript Concepts Review

Core Concept Review

index

This appendix is a non-exhaustive list of new syntactic features and methods that were added to JavaScript in ES6. These features are the most commonly used and most helpful.

While this appendix doesn't cover ES6 classes, we go over the basics while learning about components in the book. In addition, this appendix doesn't include descriptions of some larger new features like promises and generators. If you'd like more info on those or on any topic below, we encourage you to reference the Mozilla Developer Network's website (MDN).

Prefer const and let over var

If you've worked with ES5 JavaScript before, you're likely used to seeing variables declared with var:

ar myVariable = 5;

Both the const and let statements also declare variables. They were introduced in ES6.

Use const in cases where a variable is never re-assigned. Using const makes this clear to whoever is reading your code. It refers to the "constant" state of the variable in the context it is defined within.

If the variable will be re-assigned, use let.

We encourage the use of const and let instead of var. In addition to the restriction introduced by const, both const and let are block scoped as opposed to function scoped. This scoping can help avoid unexpected bugs.

Arrow functions

There are three ways to write arrow function bodies. For the examples below, let's say we have an array of city objects:

onst cities = [
  { name: 'Cairo', pop: 7764700 },
  { name: 'Lagos', pop: 8029200 },
];

If we write an arrow function that spans multiple lines, we must use braces to delimit the function body like this:

const formattedPopulations = cities.map((city) => {
    const popMM = (city.pop / 1000000).toFixed(2);
    return popMM + ' million';
});
console.log(formattedPopulations);

Note that we must also explicitly specify a return for the function.