Skip to content

ES6+ Features

ES6 (ECMAScript 2015) and later versions introduced modern features that make JavaScript:

  • More readable
  • Less error-prone
  • More expressive
  • Better for large-scale applications

graph TD A[Old JS Problems] --> B[Global Scope Issues] A --> C[Verbose Syntax] A --> D[Callback Hell] B --> E[let/const] C --> F[Arrow Functions, Destructuring] D --> G[Promises, async/await]

let count = 1;
count = 2;
const appName = "StudyVault";

  • let → block scoped
  • const → block scoped + immutable binding

console.log(a); // error
let a = 10;

const name = "Asha";
const msg = `Hello ${name}`;

const multi = `
Line 1
Line 2
`;

const user = { name: "Riya", city: "Pune" };
const { name, city } = user;

const { name: username, age = 18 } = user;

const arr = [1, 2];
const newArr = [...arr, 3]; // spread
function sum(...nums) {
// rest
return nums.reduce((a, b) => a + b, 0);
}

graph LR A[Spread] --> B[Expand Values] C[Rest] --> D[Collect Values]

function greet(name = "Guest") {
return `Hello ${name}`;
}

function test(a = b, b = 2) {}
// error (TDZ)

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

No this binding

Uses lexical this

No arguments object

Use rest instead

Cannot be constructor

Cannot use new


const obj = {
value: 10,
fn: () => console.log(this.value),
};
obj.fn(); // undefined

const name = "Asha";
const user = {
name,
greet() {
return `Hi ${this.name}`;
},
};

// old way
const { add } = require("./math.js");
// export
export function add(a, b) {
return a + b;
}
// import
import { add } from "./math.js";

  • Named export
  • Default export
export default function () {}

graph LR A[Module A] --> B[Exports] B --> C[Module B Imports]

class User {
constructor(name) {
this.name = name;
}
greet() {
return `Hi ${this.name}`;
}
}

Promise.all([p1, p2]);
Promise.race([p1, p2]);
Promise.allSettled([p1, p2]);

all

waits for all

race

first resolved/rejected

allSettled

waits all (no fail)


user?.profile?.email;

const value = input ?? "default";

0 || "default"; // "default"
0 ?? "default"; // 0

graph TD A[ES6+] --> B[Syntax] A --> C[Async] A --> D[Modules] B --> E[let const destructuring] C --> F[promises async await] D --> G[import export]

  1. Use const by default
  2. Use destructuring carefully
  3. Prefer arrow functions for callbacks
  4. Use modules for structure
  5. Avoid overusing short syntax

try {
const response = await fetch("/api/data");
if (!response.ok) {
throw new Error(`HTTP ${response.status}`);
}
const data = await response.json();
console.log(data);
} catch (error) {
console.error("Request failed:", error.message);
}