ES6 JavaScript
ES6 JavaScript
ES6 JavaScript
Part 2: React JS
Course 1: ES 6
▪ When using var inside blocks like for loops, while loops, if
statements and others, the variable will always be allocated
until the end of execution (even after the block ends) . This
allocation can lead to memory leaks and unexpected bugs.
▪ Note in the example below that the variable "myNumber" is still
set even after the for loop:
⚫ To create the special apostrophe (`), you can click alt+7 or click the ~
button. It depends on your keyboard.
Arrow Functions
// JavaScript (ES5)
function multiplyBy2(a) { return 2 * a; };
// ES6
const multiplyBy2 = a => 2 * a ;
Arrow Functions
• Another amazing feature provided by JavaScript ES6 is the
ability to pass a function as an argument to another function.
This is what we call a higher order function or a first class
function.
let sayHello = () => alert`Hello`;
let sayBye = () => alert`Bye`; //sayHello(); // “Hello”
will be alerted //
Let’s create a function that takes an argument and call it
as if it was a function
let doSomething = somethingToDo => { somethingToDo(); };
// Now any function we send to “doSomething()” function
will be called right away
doSomething(sayHello); // “Hello” will be alerted
doSomething(sayBye); // “Bye” will be alerted
Array Methods
• ES6 fournit des fonctions prédéfinies supplémentaires pour
faciliter le travail avec les tableaux telles que.
✓ .find()
✓ .forEach()
✓ .filter()
✓ .map()
✓ .reduce()
Array Methods:Find
• Suppose we want to find an element in an array, we would think of a for or while
loop to do that.
• On the other hand, using ES6's find() function will make it very easy.
• find method is a higher order function, so it takes a function as a parameter, the
parameter function sets the search criteria for the desired element in the array
• The find() method will return the value of the first element meeting the criteria
of research
const people = [{ name: 'Max' }, { name: 'Jack' }, { name: 'Marry' }]
.
// JavaScript
function findPerson(name) {
for (let i = 0; i < people.length; i++) {
let person = people[i]
if (person.name == name) {
return person
}}}
// ES6
function findPerson(name) {
return people.find(person =>person.name == name)
Array Methods:Find