CS Engineering Gyan

Modern JavaScript (ES6+ Features)

JavaScript has continuously improved with new versions to make programming easier, faster, and more efficient. ES6, also known as ECMAScript 2015, introduced many powerful features that changed the way developers write JavaScript code.

Modern JavaScript features help developers create cleaner, shorter, and more maintainable programs. These features are widely used in professional web development, frontend frameworks, and modern applications.

ES6+ refers to all JavaScript improvements introduced after ES6, including features added in ES7, ES8, ES9, and later versions.


What is ES6 in JavaScript?

ES6 stands for ECMAScript 2015. It is a major update of JavaScript released in 2015 that introduced many new programming features.

Before ES6, JavaScript programs were often longer and required more code. ES6 introduced modern syntax that makes development simpler and improves code readability.

Major ES6 Features


Why Use Modern JavaScript Features?

Modern JavaScript features provide better ways to write and manage code. They reduce complexity and improve developer productivity.

Advantages of ES6+ Features


let Keyword in JavaScript

The let keyword was introduced in ES6 to create variables with block-level scope.

Variables declared using let can be updated later but cannot be redeclared in the same scope.

Syntax


let variableName = value;

Example


let age = 20;


age = 21;


console.log(age);


Output


21


const Keyword in JavaScript

The const keyword is used to declare variables whose values cannot be reassigned after creation.

It is useful when a value should remain constant throughout the program.

Example


const pi = 3.14;


console.log(pi);


Output


3.14


Difference Between var, let, and const

var let const
Function scoped Block scoped Block scoped
Can be redeclared Cannot be redeclared Cannot be redeclared
Can be updated Can be updated Cannot be reassigned
Older JavaScript method Modern variable declaration Used for fixed values

Arrow Functions in JavaScript

Arrow functions provide a shorter syntax for writing functions. They were introduced in ES6 to simplify function expressions.

Traditional Function


function add(a,b)
{

return a+b;

}

Arrow Function


let add = (a,b) => a+b;


Example


let sum = (x,y) => x+y;


console.log(sum(10,20));


Output


30


Benefits of Arrow Functions


Template Literals

Template literals provide an easier way to create strings using backticks (`). They allow embedding variables directly inside strings.

Traditional Method


let name = "Rahul";


console.log("Hello " + name);


Template Literal Method


let name = "Rahul";


console.log(`Hello ${name}`);


Output


Hello Rahul


Advantages of Template Literals



Default Parameters in JavaScript

Default parameters allow developers to assign default values to function parameters. If no value is provided during function calling, JavaScript automatically uses the default value.

This feature reduces the need to manually check whether a parameter contains a value or not.

Syntax


function functionName(parameter = defaultValue)
{

// statements

}


Example of Default Parameters


function welcome(name = "Guest")
{

console.log("Hello " + name);

}


welcome();


welcome("Amit");


Output


Hello Guest

Hello Amit

In the first function call, no argument is passed, so JavaScript uses the default value.


Multiple Default Parameters

A function can contain multiple parameters with default values.

Example


function calculate(price, tax = 5)
{

return price + tax;

}


console.log(calculate(100));


Output


105


Destructuring Assignment in JavaScript

Destructuring is a modern JavaScript feature that allows developers to extract values from arrays and objects and store them into variables easily.

It provides a cleaner alternative to accessing individual values manually.


Array Destructuring

Array destructuring extracts values from an array based on their position.

Example


let colors = [
"Red",
"Green",
"Blue"
];


let [first, second, third] = colors;


console.log(first);


console.log(second);


console.log(third);


Output


Red

Green

Blue


Skipping Array Values

Developers can skip unwanted array values by leaving empty spaces.

Example


let numbers =
[10,20,30];


let [a,,c] = numbers;


console.log(a);


console.log(c);


Output


10

30


Object Destructuring

Object destructuring allows developers to extract object properties directly into variables.

Example


let student =
{

name:"Rahul",

age:21,

course:"JavaScript"

};


let {name, age} = student;


console.log(name);


console.log(age);


Output


Rahul

21


Destructuring with Function Parameters

Objects can also be destructured directly inside function parameters.

Example


function display(
{name, age}
)
{

console.log(name);

console.log(age);

}


display(
{
name:"Amit",
age:22
}
);



Spread Operator (...) in JavaScript

The spread operator was introduced in ES6. It allows an iterable such as an array or object to be expanded into individual elements.

It is represented using three dots (...).


Spread Operator with Arrays

Example


let numbers1 =
[1,2,3];


let numbers2 =
[4,5,6];


let result =
[
...numbers1,
...numbers2
];


console.log(result);


Output


[1,2,3,4,5,6]


Copying Arrays Using Spread Operator

Spread operator creates a new array without modifying the original array.

Example


let oldArray =
[10,20,30];


let newArray =
[...oldArray];


console.log(newArray);



Spread Operator with Objects

Spread syntax can also combine or copy object properties.

Example


let user =
{

name:"John",

age:25

};


let details =
{

...user,

city:"Delhi"

};


console.log(details);


Output


{
name:"John",
age:25,
city:"Delhi"
}


Rest Parameter in JavaScript

The rest parameter allows a function to accept an unlimited number of arguments as an array.

It also uses three dots (...) but works differently from the spread operator.

Syntax


function functionName(...parameters)
{

// statements

}


Example of Rest Parameter


function sum(...numbers)
{

let total = 0;


for(let number of numbers)
{

total += number;

}


return total;


}


console.log(sum(10,20,30,40));


Output


100


Difference Between Spread and Rest Operator

Spread Operator Rest Parameter
Expands elements. Collects elements.
Used with arrays and objects. Mostly used in function parameters.
Breaks data into individual values. Combines multiple values into an array.
Uses three dots (...). Uses three dots (...).

Enhanced Object Literals

ES6 introduced improved object syntax that allows developers to create objects with less code.

Property Shorthand

When the variable name and object property name are the same, ES6 allows writing only the variable name.

Example


let name =
"JavaScript";


let version =
"ES6";


let language =
{

name,

version

};


console.log(language);


Output


{
name:"JavaScript",
version:"ES6"
}


Method Shorthand in Objects

ES6 allows shorter syntax for defining object methods.

Example


let user =
{

show()
{

console.log("Welcome User");

}

};


user.show();


Output


Welcome User


Optional Chaining Operator (?.)

Optional chaining allows developers to safely access nested object properties without causing errors when a property does not exist.

Example


let student =
{

name:"Rahul",

address:
{

city:"Delhi"

}

};


console.log(
student.address?.city
);


Output


Delhi

If any property is missing, JavaScript returns undefined instead of generating an error.



JavaScript Classes (ES6)

Classes were introduced in ES6 to provide a simpler and cleaner way to create objects. They are based on object-oriented programming concepts and help developers organize large applications efficiently.

A class works as a blueprint for creating multiple objects with similar properties and methods.

Syntax of Class


class ClassName
{

constructor()
{

// Initialize properties

}


methodName()
{

// Method code

}


}


Example of JavaScript Class


class Student
{


constructor(name, age)
{

this.name = name;

this.age = age;

}


display()
{

console.log(this.name);

console.log(this.age);

}


}



let student1 =
new Student("Rahul",21);


student1.display();


Output


Rahul

21


The Constructor Method

The constructor method is a special method that runs automatically when a new object is created from a class.

It is mainly used to initialize object properties.

Example


class Car
{


constructor(brand)
{

this.brand = brand;

}


}


let car1 =
new Car("BMW");


console.log(car1.brand);


Output


BMW


Class Inheritance

Inheritance allows one class to access properties and methods of another class.

ES6 provides the extends keyword to create child classes from existing classes.

Example


class Animal
{


sound()
{

console.log("Animal Sound");

}


}



class Dog extends Animal
{


bark()
{

console.log("Dog Bark");

}


}



let dog =
new Dog();


dog.sound();


dog.bark();


Output


Animal Sound

Dog Bark


JavaScript Modules

Modules allow developers to divide a large JavaScript program into smaller and reusable files.

Each module can contain variables, functions, or classes that can be shared with other files.

ES6 introduced the import and export system for working with modules.


Export in JavaScript Modules

The export keyword is used to make code available outside the current file.

Example


// math.js


export function add(a,b)
{

return a+b;

}



Import in JavaScript Modules

The import keyword allows another file to use exported code.

Example


// main.js


import {add} from "./math.js";


console.log(add(10,20));


Output


30


Advantages of JavaScript Modules


Promises in JavaScript

Promises are used to handle asynchronous operations in JavaScript.

Many tasks such as API requests, file loading, and database communication take time to complete. Promises allow JavaScript to manage these operations efficiently.

A Promise represents a future result that may be successful or unsuccessful.


Promise States

State Description
Pending Initial state before operation completes.
Fulfilled Operation completed successfully.
Rejected Operation failed with an error.

Creating a Promise


let promise =
new Promise(
function(resolve,reject)
{


let success = true;


if(success)
{

resolve("Task Completed");

}

else
{

reject("Task Failed");

}


});



Handling Promise Results

Promises use then() and catch() methods to handle successful and failed operations.

Example


promise

.then(
result =>
{

console.log(result);

}

)


.catch(
error =>
{

console.log(error);

}

);



Async and Await in JavaScript

Async and await are modern JavaScript features used to write asynchronous code in a simpler and more readable way.

They work on top of promises and make asynchronous programs look similar to normal synchronous code.


async Function

The async keyword is used before a function to make it return a promise.

Example


async function message()
{

return "Hello JavaScript";

}


message()
.then(
data =>
console.log(data)
);



await Keyword

The await keyword pauses the execution of an async function until a promise is completed.

Example


async function getData()
{


let response =
await fetch(
"data.json"
);


let data =
await response.json();


console.log(data);


}



Map Object in JavaScript

Map is an ES6 feature that stores data in key-value pairs. Unlike normal objects, Map allows keys of any data type.

Example


let students =
new Map();


students.set(
1,
"Rahul"
);


students.set(
2,
"Amit"
);


console.log(
students.get(1)
);


Output


Rahul


Set Object in JavaScript

Set is a collection that stores unique values. Duplicate values are automatically removed.

Example


let numbers =
new Set(
[1,2,2,3,4]
);


console.log(numbers);


Output


1,2,3,4


Real-World Uses of ES6+ Features


Best Practices for Modern JavaScript


Common Mistakes While Using ES6 Features


JavaScript ES6+ Interview Questions

  1. What is ES6 in JavaScript?
  2. What are the advantages of modern JavaScript features?
  3. Difference between let, var, and const?
  4. What are arrow functions?
  5. Explain template literals.
  6. What is destructuring in JavaScript?
  7. Difference between spread and rest operators?
  8. What are JavaScript classes?
  9. What is inheritance in ES6?
  10. Explain JavaScript modules.
  11. What are promises?
  12. Difference between async/await and promises?
  13. What is Map and Set?
  14. Why are ES6 features important?
  15. How do modern JavaScript features improve development?

Summary

Modern JavaScript ES6+ features introduced powerful improvements that changed the way developers write JavaScript applications. Features like let, const, arrow functions, destructuring, spread operator, classes, modules, promises, and async/await make programs cleaner, faster, and easier to maintain.

Learning ES6+ is essential for every modern web developer because these concepts are widely used in frontend frameworks, APIs, and professional JavaScript projects.

A strong understanding of modern JavaScript features helps developers create scalable, efficient, and user-friendly web applications.


← Previous: JavaScript Error Handling Next: Asynchronous JavaScript →
Home Visit Our YouTube Channel