Skip to content

7. ES6+ Features

ES6 (ECMAScript 2015) introduced many modern JavaScript features.

These features make code:

  • Cleaner
  • Shorter
  • Easier to maintain
  • More readable

Modern React and frontend development heavily use ES6+ concepts.


Topics Covered

  • let and const
  • Arrow Functions
  • Template Literals
  • Destructuring
  • Spread Operator
  • Rest Operator
  • Default Parameters
  • Modules
  • Optional Chaining
  • Nullish Coalescing
  • Enhanced Object Literals

1. let and const

Introduced in ES6 as better alternatives to var.


let

Used when value may change later.


Example

js
let age = 25;

age = 26;

console.log(age);

const

Used when value should not be reassigned.


Example

js
const name = "Dipak";

console.log(name);

Important Difference

Featureletconst
ReassignmentYesNo
ScopeBlockBlock
RedeclareNoNo

Block Scope Example

js
{
  let a = 10;
  const b = 20;
}

console.log(a); // Error
console.log(b); // Error

Variables exist only inside block.


2. Arrow Functions (VERY IMPORTANT)

Short syntax for writing functions.


Normal Function

js
function add(a, b) {
  return a + b;
}

Arrow Function

js
const add = (a, b) => {
  return a + b;
};

Short Syntax

js
const add = (a, b) => a + b;

If only one line exists, return is automatic.


Example

js
const square = (num) => num * num;

console.log(square(5));

Important Points

  • Short syntax
  • No own this
  • Cannot be constructor
  • Common in React

3. Template Literals (VERY IMPORTANT)

Used for string interpolation.

Uses backticks ` instead of quotes.


Old Way

js
const name = "Dipak";

console.log("Hello " + name);

ES6 Template Literal

js
const name = "Dipak";

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

Benefits

  • Cleaner syntax
  • Multi-line strings
  • Variable embedding

Multi-Line String

js
const text = `
Hello
World
`;

4. Destructuring (VERY IMPORTANT)

Extract values from objects or arrays into variables.


Object Destructuring

js
const user = {
  name: "Dipak",
  age: 25,
};

const { name, age } = user;

console.log(name);

Array Destructuring

js
const colors = ["red", "blue"];

const [first, second] = colors;

console.log(first);

Benefits

  • Cleaner code
  • Shorter syntax
  • Used heavily in React props/state

5. Spread Operator (...) (VERY IMPORTANT)

Spread operator expands values.


Array Example

js
const arr1 = [1, 2];

const arr2 = [...arr1, 3];

console.log(arr2);

Output

js
[1, 2, 3];

Object Example

js
const user = {
  name: "Dipak",
};

const updatedUser = {
  ...user,
  age: 25,
};

console.log(updatedUser);

Common Uses

  • Copy arrays
  • Copy objects
  • Merge arrays
  • Merge objects

6. Rest Operator (...)

Rest operator collects multiple values into array.


Example

js
function sum(...nums) {
  return nums;
}

console.log(sum(1, 2, 3));

Output

js
[1, 2, 3];

Difference Between Spread and Rest

SpreadRest
Expands valuesCollects values
Used while copyingUsed in parameters
Array → individual valuesValues → array

7. Default Parameters

Allows default values in functions.


Example

js
function greet(name = "Guest") {
  console.log(`Hello ${name}`);
}

greet();

Output

js
Hello Guest

8. Modules (VERY IMPORTANT)

Modules help split code into separate files.

Makes code organized and reusable.


Export

js
export const name = "Dipak";

Import

js
import { name } from "./file.js";

Default Export

Export

js
export default function greet() {
  console.log("Hello");
}

Import

js
import greet from "./file.js";

Why Modules are Important

  • Better organization
  • Reusable code
  • Used heavily in React projects

9. Optional Chaining (?.) (VERY IMPORTANT)

Safely accesses nested properties.

Prevents errors if property does not exist.


Without Optional Chaining

js
console.log(user.address.city);

If address is undefined → Error.


With Optional Chaining

js
console.log(user?.address?.city);

Example

js
const user = {
  name: "Dipak",
};

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

Output

js
undefined;

Benefits

  • Prevents application crashes
  • Cleaner code
  • Common in API responses

10. Nullish Coalescing (??) (VERY IMPORTANT)

Provides default value only when value is:

  • null
  • undefined

Example

js
const name = null;

const result = name ?? "Guest";

console.log(result);

Output

js
Guest;

Difference Between || and ??


Using ||

js
const value = 0;

console.log(value || 100);

Output

js
100;

Because 0 is falsy.


Using ??

js
const value = 0;

console.log(value ?? 100);

Output

js
0;

Because ?? checks only:

  • null
  • undefined

11. Enhanced Object Literals

Shorter object syntax in ES6.


Old Way

js
const name = "Dipak";

const user = {
  name: name,
};

ES6 Version

js
const name = "Dipak";

const user = {
  name,
};

Method Shorthand

js
const user = {
  greet() {
    console.log("Hello");
  },
};

Important Interview Questions


Q1. Difference Between let, const, and var

Featurevarletconst
ScopeFunctionBlockBlock
ReassignYesYesNo
RedeclareYesNoNo
HoistingYesYesYes

Q2. What is Optional Chaining?

Optional chaining safely accesses nested properties.

js
user?.address?.city;

Avoids errors if property is undefined.


Q3. What is Nullish Coalescing?

Provides default value only for:

  • null
  • undefined
js
const value = data ?? "Default";

Q4. Spread vs Rest Operator

Spread

Expands values.

js
const arr2 = [...arr1];

Rest

Collects values.

js
function test(...nums) {}

Q5. Why are Arrow Functions Important?

  • Short syntax
  • Cleaner code
  • Used heavily in React

Most Asked ES6 Topics

Focus heavily on:

  • Arrow functions
  • Destructuring
  • Spread operator
  • Optional chaining
  • Template literals
  • Modules
  • let and const

Best Practices

  • Prefer const by default
  • Use let only when value changes
  • Use template literals for dynamic strings
  • Use optional chaining for nested API data
  • Use modules for better code organization
  • Prefer spread operator over mutation

© 2025 DDocs · Dipak's Documentation Guide