Chit Chat Some JavaScript ES6 Features

Tania Akter
2 min readMay 6, 2021

ECMAScript 2015 or ES2015 is added many great features to the JavaScript programming language. That’s why programmers write less code in an efficient way. Today we discuss some amazing features of ES2015 (often called is ES6).

Default Function Parameters: In a function, we can set the default parameters after this if we invoke the function without arguments, the function will execute without error like undefined.

example:

function add(x=3, y=4){

return x + y;

}

add(); // output will be 7, not undefined.

If we pass the arguments then the default parameters value doesn’t execute. Function execute according to arguments that given.

Rest Parameters: ES6 has a new type of parameter called rest parameter. It has three dots before the parameter name (like: …rest). If a function has two parameters, we invoke it more than two arguments then the first one map for the first parameter and the second one for the second parameter rest of the argument map for …rest parameter as an array.

Example:

function foo(x, y, …z){

console.log x , y, x;

}

foo(1, 2, 3, 4,5); //1, 2, [3, 4, 5]

This rest parameter must be declared in the end otherwise it will through an error.

Spread Operators: Spread operators spread the elements of iterable map sets or objects. It almost looks like the rest parameter but they are not the same. The difference between rest parameters and spread operators has spread operators unpack the from an array and spread it otherwise rest parameters bind the rest of the arguments into an array.

Example: const array = [1, 2, 3]

const newArray = [4, 5, 6, …array]

console.log(newArray); //output [4, 5, 6, 1, 2, 3]

for…of loop: ES6 introduced a new type of loop that iterating over iterable objects. for(variable of iterable) here every time iteration, the value of the iterable object is assigned to the variable. Variable can be declared with let, const, or var.

Example: let values = [35, 49, 29];

for (let value of values){

value = value + 10;

console.log(value);

} // output 45, 59, 39

Object destructuring: JavaScript object destructuring assigns values of an object to individual variables. suppose you have a car object with two properties: brand and color.

Example: let car = {

brand: ‘Tesla’

color: ‘red’

};

now restructure car object like ,

let {brand, color} = car;

export and import: If a javaScript variable or function declared with the export word, that function or variable can use another file by import it. Create a file foo.js then write the following code,

export let foo = ‘foo foo’

then create another file named app. js that uses the foo.js module.

import {foo} from ‘./foo.js’

console.log(foo) //output foo foo.

--

--