Arun Kumar
2 min readMar 4, 2023

ES6 Features: Arrow Functions, Destructuring, and Spread Syntax

Arrow Functions

Arrow functions are a more concise way to write function expressions in JavaScript. They have a simpler syntax and automatically bind to the surrounding context. Here’s an example:

// Function expression
const add = function(a, b) {
return a + b;
};

// Arrow function
const add = (a, b) => {
return a + b;
};

In this example, we define a function called add that takes two parameters and returns their sum. We can write the same function using an arrow function expression, which is shorter and easier to read.

Destructuring

Destructuring is a way to extract values from arrays or objects into separate variables. It can be used to simplify code and make it more readable. Here’s an example:

// Array destructuring
const [first, second, third] = [1, 2, 3];
console.log(first); // 1
console.log(second); // 2
console.log(third); // 3

// Object destructuring
const person = {
name: 'John Doe',
age: 30,
gender: 'male'
};
const {name, age, gender} = person;
console.log(name); // 'John Doe'
console.log(age); // 30
console.log(gender); // 'male'

In this example, we use array destructuring to extract the values from an array into separate variables. We also use object destructuring to extract the values from an object into separate variables.

Spread Syntax

Spread syntax is a way to expand an iterable object into individual elements. It can be used to simplify code and make it more readable. Here’s an example:

// Spread syntax with arrays
const arr1 = [1, 2, 3];
const arr2 = [...arr1, 4, 5, 6];
console.log(arr2); // [1, 2, 3, 4, 5, 6]

// Spread syntax with objects
const obj1 = {a: 1, b: 2, c: 3};
const obj2 = {...obj1, d: 4, e: 5, f: 6};
console.log(obj2); // {a: 1, b: 2, c: 3, d: 4, e: 5, f: 6}

In this example, we use spread syntax to expand an array into individual elements and then use those elements to create a new array. We also use spread syntax to expand an object into individual key-value pairs and then use those pairs to create a new object.

Arun Kumar

Experienced web developer with 10+ years expertise in JavaScript, Angular, React and Vue. Collaborative team player with focus on results.