Original address:
Medium.freecodecamp.org/make-your-c…
Read more on the translator’s Blog:
Github.com/elevenbeans…
Template string
Template template strings make string concatenation easier than ever before. It starts with the reverse tag (the character right above Tab), and you can insert variables using ${variable}. Compare the following two lines of code:
123 |
|
This will make everything simpler and the code easier to read. You can put anything in curly braces: variables, equations, or function calls. I’ll use them in examples throughout this article.
Block-level scope
JavaScript itself has always been functional scoped, which is why we often wrap an entire JavaScript file in an empty call-immediate-function expression (IIFE). This is done to isolate all variables in the file, so there are no variable name conflicts globally.
Now we have block-level scope and two new variable declarations bound to the block.
Let
This is similar to VAR, but with some significant differences. Because the variables it declares have block-level scope, new local (internal) variables with the same name can be declared without affecting the external variables.
123456 |
|
Because it is bound to a block (i.e., the range between {and}), it solves the classic interview question: “What is the output and how do you make it work the way you want it to?”
123 |
|
In this case, it prints “5, 5, 5, 5” because the variable I is changed in each iteration, and everything ends up with the last change.
If you switch from var to let, that’s a different story. Each loop now creates a new block scope with the value bound to that loop. You write:
12345 |
|
Another difference between var and let is that let is not promoted like VAR.
123456 |
|
Because of its stricter scoping and more predictable behavior, some have said that let should be used instead of VAR, unless declarations with VAR are specifically needed to enhance or require its looser scope.
Const
If you want to declare a constant variable in JavaScript, the convention is to name the variable in uppercase. However, this does not guarantee that it is a constant – it just lets other developers know that it is a constant and should not be changed.
Now we have a const declaration.
12345 |
|
Const does not make a variable immutable, only locks its assignment. If you have a complex assignment (object or array), the value can still be modified.
12345678 |
|
Block-level scoping function problems
Function declarations are now specified that they must be bound to block-level scopes.
12345 |
|
The problem comes when you declare a function in an if statement.
Consider this:
123456 |
|
Prior to ES6, both function declarations would have been enhanced, resulting in ‘I didn’t pass’, no matter what something was.
Now we get ‘ReferenceError’ because baz() is always bound by the block scope.
Extended operator
ES6 introduces… Operator, which is called the “extended operator.” It has two main uses: to split arrays or objects into new arrays or objects, and to combine multiple parameters into one array.
The first use case is the most you’re likely to encounter, so let’s look at it first.
12345678 |
|
You can also extend the object by entering each key-value pair into a new object. (It’s actually phase 4 of the proposal and will be officially launched in ES2018, only with Chrome 60 or later, Firefox 55 or later, and Node 6.4.0 or later)
123 |
|
. Another feature of the operator is that it creates a new array or object. The following example creates a new array for B, but C just points to the same array.
123456789 |
|
The second example is to collect variables together into an array. This is useful when you don’t know how many variables are passed to the function.
1234 |
|
Function default arguments
You can now define functions with default parameters. Missing or undefined values are initialized with default values. Just be careful – because null and false values are forced to be zero.
12345678 |
|
Default values can be more than just values – they can also be expressions or functions.
1234567 |
|
deconstruction
Deconstruction is the process of breaking up the array or object to the left of the equals sign. Arrays or objects can come from variables, functions, or equations.
12345 |
|
Object deconstruction lets you list the key of an object in braces to extract the key-value pair.
12345 |
|
Sometimes, you want to extract the values but assign them to a new variable. This is done using a ‘key: variable’ pairing on the left of the equals sign.
Sometimes you want to take values and assign them to a new variable. This is done by pairing the key: variable to the left of the equals sign.
1234567891011 |
|
Another thing object deconstruction allows is to assign values to multiple variables.
12 |
|
Object literals and concise arguments
When you create object literals from variables, ES6 allows you to omit the key name if it is the same as the key and the variable name.
1234 |
|
This can also be used in conjunction with deconstruction to make your code simpler and cleaner.
123456789101112 |
|
It can also be used to deconstruct objects passed to functions. Methods 1 and 2 are how you do it before ES6, and method 3 uses deconstructed and concise parameters.
1234567891011121314151617181920212223 |
|
With ES6, we can extract age, name, and company without additional variable declarations.
Dynamic property name
ES6 adds the ability to create or add attributes using dynamically allocated keys.
123456 |
|
Arrow function
Arrow functions have two main aspects: structure and this binding.
They can have a simpler structure than traditional functions because they don’t require function keywords, and they automatically return anything after the arrow.
1234 |
|
If the function needs more than a simple calculation, curly braces can be used, and the function returns whatever the curly brace block scope returns.
12345 |
|
One of the most useful places for arrow functions is in array functions like map(), forEach(), or sort().
123 |
|
In addition to having a shorter syntax, it also fixes problems that often occur in binding behavior. A fix with pre-ES6 functionality is to store this reference, usually as an argument.
12345678910 |
|
This must be done because the binding is dynamic. That means that this thing in this event listener and this thing in doSomething don’t refer to the same thing.
In the arrow function, the binding is lexical, not dynamic. This is the main design feature of the arrow function.
While this kind of binding can be great, sometimes it’s not desirable.
12345678 |
|
When we use a.onthing (6), the this.otherthing (b) reference fails because this does not point to an object, but to the surrounding scope. Be aware of this if you are rewriting legacy code using ES6 syntax.
The for… Of circulation
ES6 adds a way to iterate over each value in an array. This is different from the existing… In is different. It loops over key/index.
123456789 |
|
Using the new… of loop saves adding a let val = a[idx] inside each loop.
Arrays, strings, generators and collections are all iterable in standard JavaScript. Plain objects can’t normally be iterated over, unless you have defined an iterator for it.
Use the new for… Of loop, do not add a let val = a[idx] inside each loop.
Arrays, strings, generators, and collections can all be iterated over in standard JavaScript. A normal object cannot iterate unless you have defined an Iterator for it.
Numeric literal
ES5 code handles decimal and hexadecimal number formats well, but does not specify octal format. In fact, it’s banned under strict rules.
ES6 adds a new format that adds an O (note the letter) after the initial 0 to declare the number as octal. ES6 also adds a binary format.
12345 |
|
And a lot more…
ES6 gives us more features to make our code cleaner, shorter, easier to read and more powerful. My goal is to write a sequel to this article that covers lesser-known parts of ES6.