JavaScript ES6 (also known as ECMAScript 2015 or ECMAScript 6) is the newer version of JavaScript that was introduced in 2015. ECMAScript is the standard that JavaScript programming language uses. ECMAScript provides the specification on how JavaScript programming language should work.
ES6 Syntax
- let is used when declaring a variable, the scope of the variable is either global or local.
- const is used when declaring a variable that you don't want to change or cant be reassigned opposite of var and let keyword.
Rest parameters have a prefix of three dots (…) and allow you to represent an indefinite number of arguments as an array.
function names(a,b, ...args) {
}
- Spread operator consists of three dots(…) & allows you to spread out elements of an iterable object such as an array, map, or set.
const odd = [1, 3, 5];
const combined = [2,4,6, ...odd];
console.log(combained)
// [2, 4, 6, 1, 3, 5]
- default parameter in es6 we can put default values in the function
function rectangle(height= 20 width = 30){
}
- Template Literals In ES6 we can use new syntax ${PARAMETER} inside back-ticked string
var name = `My name is ${firstname} ${lastname}`;
- In es6 multiple line string is very easy and simple to do.
var Poem = `Twinkle Twinkle
Little Stars⭐️.
How I wonder
What you are.`
- Arrow Function
// Traditional Anonymous Function
function (a, b){
let chuck = 42;
return a + b + chuck;
}
// Arrow Function
(a, b) => {
let chuck = 42;
return a + b + chuck;
}
- Promises are used for asynchronous execution. In ES6, we can use promise with arrow function shown below.
var asyncCall = new Promise((resolve, reject) => {
// do something async
resolve();
}).then(()=> {
console.log('Yay!');
})