Template Literals/ Rest Parameter & Spread Operator in ES6

Template Literals/ Rest Parameter & Spread Operator in ES6

Template Literals in ES6

The template literal has made it easier to include variables inside a string.

const first_name = “shairy”;

const last_name = “kalra”;

Before ES6

console.log('Hello ' + first_name + ' ' + last_name);

Now

console.log(`Hello ${first_name} ${last_name}`);

Rest Parameter in ES6

Rest parameter are used to represent an indefinite number of arguments in an array using ... syntax.

function show(a, b, ...args) {
  console.log(a); // one
  console.log(b); // two
  console.log(args); // ["three", "four", "five", "six"]
}
show('one', 'two', 'three', 'four', 'five', 'six')

Spread Operator in ES6

Spread syntax is used to copy the items into a single array using ... syntax.

let arr1 = ['one', 'two'];
let arr2 = [...arr1, 'three', 'four', 'five'];
console.log(arr2); // ["one", "two", "three", "four", "five"]

Both the rest parameter and the spread operator use the same syntax. However, the spread operator is used with arrays (iterable values).

Post Your Comments & Reviews

Your email address will not be published. Required fields are marked *

*

*