What are ES6 Features in JavaScript?

Some of the ES6 Features are,

JavaScript Arrow Function:

  // Traditional function expression
  function add(a, b) {
    return a + b;
  }

  // Arrow function
  const multiply = (a, b) => a * b;

  console.log(add(2, 3));       // Output: 5
  console.log(multiply(2, 3));  // Output: 6
Arrow functions have a shorter syntax with the following characteristics:

JavaScript Spread Operator:

  const array1 = [1, 2, 3];
  const array2 = [4, 5, 6];
  const combinedArray = [...array1, ...array2];
  console.log(combinedArray);  // Output: [1, 2, 3, 4, 5, 6]
  const originalArray = [1, 2, 3];
  const copiedArray = [...originalArray];
  console.log(copiedArray);  // Output: [1, 2, 3]
  function sum(a, b, c) {
    return a + b + c;
  }

  const numbers = [1, 2, 3];
  const result = sum(...numbers);
  console.log(result);  // Output: 6
  const originalObject = { name: 'John', age: 30 };
  const copiedObject = { ...originalObject };
  console.log(copiedObject);  // Output: { name: 'John', age: 30 }
  const string = 'Hello';
  const characters = [...string];
  console.log(characters);  // Output: ['H', 'e', 'l', 'l', 'o']

JavaScript Map:

Example:
  const numbers = [1, 2, 3, 4, 5];

  const squaredNumbers = numbers.map((num) => {
    return num * num;
  });

  console.log(squaredNumbers);  // Output: [1, 4, 9, 16, 25]

JavaScript Destructuring Assignment

Example:
  const numbers = [1, 2, 3, 4, 5];
  const [a, b, c] = numbers;

  console.log(a);  // Output: 1
  console.log(b);  // Output: 2
  console.log(c);  // Output: 3
  const numbers = [1, 2, 3, 4, 5];
  const [, , c, d] = numbers;

  console.log(c);  // Output: 3
  console.log(d);  // Output: 4
  const person = { name: 'John', age: 30, country: 'USA' };
  const { name, age } = person;

  console.log(name);  // Output: 'John'
  console.log(age);   // Output: 30
  const person = { name: 'John', age: 30, country: 'USA' };
  const { name: personName, age: personAge } = person;

  console.log(personName);  // Output: 'John'
  console.log(personAge);   // Output: 30

Conclusion:

Leave a Reply

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


The reCAPTCHA verification period has expired. Please reload the page.