What is ES6 in JavaScript?

JavaScript is a powerful and versatile language invented by Brendan Eich in 1995. ECMAScript is the standard that defines how JavaScript should work. ECMAScript is maintained by ECMA international standard organization. JavaScript became the ECMA standard in 1997. Since then, JavaScript has had different versions, and ES6 is one of them.

ES6 stands for ECMAScript 6, also known as ECMAScript 2015 or ES6. It is the sixth version of JavaScript and was introduced in 2015. JavaScript ES6 was introduced to tackle issues from ES5 and introduce new features to the language.

Major features of ES6

Some of the major features introduced by JavaScript are listed below:

  • The let keyword: The let keyword is used to declare variables; in previous versions, var was used to declare variables. Variables declared using let are block-scoped, meaning these variables are only accessible within a particular block.

// declared the age variable using let
let age = 22;
{
let age = 30;
// can be accessed only inside
console.log(age); // 30
}
console.log(age); // 22
An example showing how the let keyword is used in JavaScript.

  • const: The const keyword is used to create constants, once assigned, the value of the const variable cannot be reassigned. Variables declared using const are block scoped.

// the value of age cannot be reassigned
const age = 12;
An example showing how the const keyword is used in JavaScript.
  • Arrow functions: It allows us to create clean and concise functions compared to the traditional regular functions. it can also be used to create function expressions.

// arrow functions
const calcAge = birthYear =>2023 - birthYear;
const age = calcAge(1992)
console.log(age); //31
An example showing how, arrow functions can be used in JavaScript.
  • Template literals/template strings: It makes it easy to include variables and expressions within a string, instead of single quote or double quotes, they are enclosed in backticks (` ${expression }`).

//in previous versions we had to do
const lastName = "obi";
const firstName = "peter";
console.log('Welcome ' + lastName + ' ' + firstName);
// but with template literals
console.log(`Welcome ${lastName} ${firstName}`)
An example showing how the template strings are used in JavaScript.
  • Classes: The word class in JavaScript defines the blueprint for creating objects.

class Car {
constructor(name) {
this.name = name;
}
}
const firstCar = new Car('Toyota');
const secondCar = new Car('Mercedes');
console.log(firstCar.name); // Toyota
console.log(secondCar.name); //Mercedes
An example showing how a class is used in JavaScript.
  • Default parameter values: In ES6, we can pass default values as function parameters.

function addNum(a, b = 6){
// if the value of b is not passed, b = 6
return a + b;
}
// value of b not passed, use 6(default value)
console.log(addNum(3));
console.log(addNum(4,8));
An example showing how default values are used in JavaScript.
  • Destructuring: In ES6, this feature makes it easier to retrieve elements from an array or objects and assign them to a separate variable.

// in previous versions
const car = {
color : 'blue',
seats : 6,
tyres : 8
}
let color = car.color;
let seats = car.seats;
let tyres = car.tyres;
console.log(color);
console.log(seats);
console.log(tyres);
// with ES6 destructuring
const person = {
name : 'carly',
gender : 'female',
age : 8
}
let {name,age,gender} = person;
console.log(name);
console.log(age);
console.log(gender);
An example showing how destructuring works in JavaScript.
  • Import and export: With ES6 import and export, we can export code and import code across multiple files.

index.js
script.js
// import
import address from './index.js';
address('rhodes', 'awka');
// I live at rhodes street in awka town.
index.js shows how export works ,while script.js shows how import works in JavaScript.
  • Rest parameter and spread operator: The rest parameter allows the function to accept an indefinite amount of arguments while the the spread operator is used to copy items into a single array. Both the rest parameter and spread operator makes use of the same syntax ( ...).

// rest parameter
function displayAlphabets(a, b, ...alphabets) {
console.log(a); // a
console.log(b); // b
console.log(alphabets); // ['c', 'd', 'e', 'f', 'g', 'h', 'i']
}
displayAlphabets('c', 'd', 'e', 'f', 'g', 'h', 'i')
// spread operator
let arr1 = ['one', 'two'];
let arr2 = [...arr1, 'three', 'four', 'five'];
console.log(arr2); // ["one", "two", "three", "four", "five"]
An example showing how the rest parameter and spread operator work in JavaScript.

Copyright ©2024 Educative, Inc. All rights reserved