“let” keyword:
This is basically a super local variable, it’s very useful for using them in for loops. Now you can have multiple nested for loops and declare all of your loop counter variables “i” and not worry about conflicts.
Example
Using var:
var i = 5;
for (var i = 0; i < 10; i++) {
// after last loop i equals 10
}
// Here i is 10
Using let:
let i = 5;
for (let i = 0; i < 10; i++) {
// after last loop i equals 10
}
// Here i is 5
“for of” statement
This is another way to loop through an array, It is easier to write although it only works on iterables so this means it doesn’t work on objects as opposed to “for in” which works on both objects and arrays.
Example
Considering:
var array = [1];
Using “for in”:
for (let index in array){
let value = array[index];
//value equals 1
}
Using “for of”:
for (let value of array){
//value equals 1
}
String includes() Method
To check if a string includes some text.
Example
Considering the following string:
var myString = "The quarantine is a hoax designed by the government to change the batteries in all the birds";
Using the search method:
var includesBirds = myString.search("birds") != -1; //true
Using includes method:
var includesBirds = myString.includes("birds"); //true
So your code would end up looking like this:
var sendFBI = myString.includes("birds") &&
myString.includes("government") &&
myString.includes("batteries"); //true
Array includes() Method
This is the same thing as the previous example with the String includes() method. It is easier and more elegant than using Array indexOf.
Example
Considering the following string:
var myArray = ["banana", "apple", "illuminati", "watermelon"];
Using the indexOf method:
var includesIlluminati = myArray.indexOf("illuminati") != -1; //true
Using includes method:
var includesIlluminati = myArray.includes("illuminati"); //true
Array every() Method
This method is useful for validating if all of the elements of the array meet certain conditions, if one of them doesn’t meet the condition then it returns false.
Example
var guestAges = [17,18,21,22,24];
var allAdults = guestAges.every( function (age){ return age >= 18; } );
//allAdults equals false because one of the guest ages is under 18
Set Object
The set object is a list of unique values of any type. This is very useful for removing duplicates from an Array.
Example
let myArray = [1,2,3,4,4,4,4,4,4,4,4,4,4,4,4,4,5];
let mySet = new Set(myArray); // mySet now equals {1, 2, 3, 4, 5}
let deDuplicatedArray = Array.from(mySet); //deDuplicatedArray now equals [1,2,3,4,5]
Spread syntax
This is basically used to expand an array or compact several parameters into an array.
Examples
Useful for concatenating two arrays like:
let array1 = ["bears", "beets"];
let array2 = ["battlestar galactica", ...array1 ];
//array2 equals ["bears","beets","battlestar galactica"]
Or making copies of arrays like:
let array1 = [1, 2, 3];
let array1Copy = array1; //This is a shallow copy
let array1CopySpread = [...array1 ];
array1Copy.push(4); //Pushing to the shallow copy
//array1 equals [1,2,3,4]
//array1Copy equals [1,2,3,4]
//array1CopySpread equals [1,2,3] //The copy using spread remains unmodified
Template literals (Template strings)
You can now use backtick characters (`) to have more freedom when defining strings, they also support placeholders and multi-line strings!
Example
Using string concatenation:
var myNumber = 23;
var myString = 'text line 1\n' +
'text line 2\n' +
'text with "double quotes" and \'single quotes\'\n' +
'text with variable value of ' + myNumber;
Using template literals:
var myNumber = 23;
var myString =
`text line 1
text line 2
text with "double quotes" and 'single quotes'
text with variable value of ${myNumber}`;
Arrow functions
These functions work the same as regular functions but have a more compact syntax.
Example
Regular function:
function hello() {
return "Hello World!";
}
Arrow function:
hello = () => {
return "Hello World!";
}
And it can get even shorter:
hello = () => "Hello World!";
Anonymous arrow function with params:
((name)=> `Hello ${name}!`)('stranger'); //"Hello stranger!"