1️⃣2️⃣ ES6 Modern JavaScript

1️⃣ let & const

🔹 let

  • Block-scoped (only works inside { })
  • Can be reassigned
let age = 25;
age = 30; // ✅ allowed

🔹 const

  • Block-scoped
  • Cannot be reassigned (but objects/arrays can be modified)
const name = "Aditya";
// name = "Rahul"; ❌ errorconst user = { city: "Delhi" };
user.city = "Mumbai"; // ✅ allowed

2️⃣ Arrow Functions (=>)

Shorter way to write functions.

Normal Function

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

Arrow Function

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

With one parameter

const greet = name => "Hello " + name;

3️⃣ Template Literals ( )

Use backticks instead of quotes → allows variables & multi-line strings.

let name = "Aditya";
let msg = `Hello ${name}, welcome!`;console.log(msg);

Multi-line

let text = `Line 1
Line 2
Line 3`;

4️⃣ Destructuring

Extract values from arrays/objects easily.

Array Destructuring

let arr = [10, 20, 30];
let [a, b] = arr;console.log(a); // 10

Object Destructuring

let user = { name: "Aditya", age: 22 };let { name, age } = user;

5️⃣ Spread Operator (...)

Expands elements (used to copy or merge).

Array Example

let arr1 = [1, 2];
let arr2 = [...arr1, 3, 4];console.log(arr2); // [1,2,3,4]

Object Example

let user = { name: "Aditya" };
let updated = { ...user, city: "Delhi" };

6️⃣ Rest Operator (...)

Collects multiple values into one variable.

function sum(...numbers) {
return numbers.reduce((total, num) => total + num);
}console.log(sum(1,2,3,4)); // 10

👉 Difference:

  • Spread → expands
  • Rest → collects

7️⃣ Modules (import / export)

Used to split code into multiple files.


Export (file: math.js)

export const add = (a, b) => a + b;
export const sub = (a, b) => a - b;

Import (file: app.js)

import { add, sub } from './math.js';console.log(add(5, 3));

Default Export

// math.js
export default function multiply(a, b) {
return a * b;
}
// app.js
import multiply from './math.js';

🔥 Quick Summary

FeatureUse
letvariable (changeable)
constfixed variable
Arrow functionshorter functions
Template literalsdynamic strings
Destructuringextract values
Spreadexpand data
Restcollect data
Modulessplit code