Getting everything ready for you…

Loading courses, problems and your progress. This takes just a moment.

0s

Notes › JavaScript

ES6+ Features Every Developer Uses

ES6 (2015) and the yearly versions after it changed how JavaScript is written. React and Node code uses these everywhere.

Arrow functions

const add = (a, b) => a + b;              // implicit return
const square = (x) => x * x;
const makeUser = (name) => ({ name });    // wrap object literals in ()

No own this — see The this Keyword.

Template literals

const name = "Asha", score = 92;
const msg = `Hi ${name}, you scored ${score > 90 ? "A+" : "A"}`;
const html = `
  <div class="card">${name}</div>
`;   // multi-line

Destructuring

const user = { name: "Ravi", age: 22, city: "Jaipur" };
const { name, age } = user;                 // from objects
const { city: home = "Delhi" } = user;      // rename + default
const [first, second, ...rest] = [10, 20, 30, 40];   // from arrays
let x = 1, y = 2;
[x, y] = [y, x];                            // swap without a temp → x = 2, y = 1

function show({ name, age = 18 }) { return `${name} ${age}`; }   // in parameters

Spread and rest (...)

const nums = [1, 2, 3];
const more = [...nums, 4, 5];               // spread an array
const copy = { ...user, age: 23 };          // copy object + override a field
Math.max(...nums);                          // 3

function sum(...values) {                   // rest: gather arguments into an array
  return values.reduce((s, v) => s + v, 0);
}
const { password, ...safeUser } = dbUser;   // rest in destructuring: drop a field

Spread makes a shallow copy — nested objects are still shared (structuredClone for deep copies).

Default parameters

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

Modules

// math.js
export const PI = 3.14159;
export function area(r) { return PI * r * r; }
export default function circumference(r) { return 2 * PI * r; }

// app.js
import circumference, { area, PI } from "./math.js";
import * as math from "./math.js";
const { heavy } = await import("./heavy.js");    // dynamic import (code splitting)

Named exports must be imported with the same name; one default export per file.

Classes

class Animal {
  #secret = "private field";          // # = truly private
  static count = 0;
  constructor(name) { this.name = name; Animal.count++; }
  speak() { return `${this.name} makes a sound`; }
}
class Dog extends Animal {
  speak() { return `${super.speak()} — woof`; }
}

Classes are syntax over prototypes.

Optional chaining and nullish coalescing

const city = user?.address?.city;            // undefined instead of a crash
const first = list?.[0];
callback?.();                                // call only if defined

const port = config.port ?? 3000;            // only null/undefined fall back
const count = 0 || 10;    // 10  (|| treats 0, "", false as missing)
const count2 = 0 ?? 10;   // 0   (?? keeps 0)

Array methods (ES5+, used constantly)

const marks = [45, 82, 91, 67];
marks.map((m) => m + 5);                    // transform
marks.filter((m) => m >= 60);               // keep some
marks.reduce((sum, m) => sum + m, 0);       // combine → 285
marks.find((m) => m > 80);                  // 82
marks.findIndex((m) => m > 80);             // 1
marks.some((m) => m < 50);                  // true
marks.every((m) => m > 40);                 // true
marks.includes(91);                         // true
[...marks].sort((a, b) => a - b);           // numeric sort (default sort is by string!)
marks.at(-1);                               // 67 (ES2022)
[[1, 2], [3]].flat();                       // [1, 2, 3]

Map, Set and objects

const m = new Map([["a", 1]]);
m.set("b", 2); m.get("a"); m.has("b"); m.size;   // any key type, keeps insertion order
const s = new Set([1, 2, 2, 3]);                  // {1, 2, 3}
const unique = [...new Set(arr)];

Object.keys(user); Object.values(user); Object.entries(user);
Object.fromEntries([["x", 1]]);                    // { x: 1 }

Other useful additions

Interview one-liners

More JavaScript notes