Modern JavaScript (ES6 and Later): The Features You Use Every Day
What is ES6 in JavaScript?
ES6, officially ECMAScript 2015, was a major update to the JavaScript language standard. It added let and const, arrow functions, template literals, destructuring, classes, modules and promises. Since then a new edition has been published every year, adding features such as optional chaining and nullish coalescing. "ES6+" usually means this whole modern syntax.
ECMAScript versions and browser support
ECMAScript is the specification that JavaScript engines implement. It is maintained by the TC39 committee at Ecma International, and editions are named by year: ES2015 (ES6), ES2016 and so on. A proposal joins the standard once it reaches stage 4.
| Feature | Edition |
|---|---|
| let, const, arrow functions, classes, modules, template literals, destructuring, promises | ES2015 |
async functions and await | ES2017 |
| Object spread and rest | ES2018 |
Optional chaining ?. and nullish coalescing ?? | ES2020 |
Logical assignment such as ??= | ES2021 |
Private class fields #name and top-level await in modules | ES2022 |
Every feature in this guide works in all current major browsers and in Node.js, so for most new projects you can write it directly without a build step.
let, const and block scope
let and const are scoped to the nearest block { }, unlike var, which is scoped to the whole function. const prevents reassigning the variable, but an object or array held in a const can still be changed. Accessing either before its declaration throws a ReferenceError; the period before the declaration is called the temporal dead zone.
const taxRate = 0.18; // cannot be reassigned
let total = 0; // can change
const cart = [];
cart.push({ item: 'Pen', price: 20 }); // allowed: the array itself is mutated
for (let i = 0; i < 3; i++) {
setTimeout(() => console.log(i)); // 0 1 2, each loop gets a fresh i
} Arrow functions and template literals
Arrow functions are a shorter syntax for function expressions. With a single expression they return it automatically. They do not have their own this or arguments; they use the values from the surrounding code, which makes them ideal for callbacks but unsuitable as object methods that rely on this.
const double = n => n * 2;
const add = (a, b) => a + b;
const makeUser = (name) => ({ name, active: true }); // wrap object literals in ()
const prices = [120, 450, 80];
const withTax = prices.map(p => Math.round(p * 1.18));
const name = 'Asha';
const course = 'PHP';
const message = `Hi ${name}, your ${course} class starts at ${9 + 1} am.
See you there.`; // multi-line, with interpolationTemplate literals use backticks and ${expression} placeholders, and they can span several lines. They build strings, not safe HTML: never insert user data into markup with them and innerHTML.
Destructuring, spread and rest
Destructuring unpacks values from arrays or properties from objects into variables. Spread (... in an expression) expands an array or object. Rest (... in a parameter list or pattern) gathers the remaining items.
const student = { id: 7, name: 'Ravi', city: 'Pune', marks: [78, 91] };
const { name, city = 'Unknown', marks: [first] } = student; // defaults and nesting
const [a, , c] = [10, 20, 30]; // skip items
const updated = { ...student, city: 'Nagpur' }; // shallow copy with one change
const all = [...[1, 2], ...[3, 4]]; // [1, 2, 3, 4]
function sum(...numbers) { // rest parameter
return numbers.reduce((t, n) => t + n, 0);
}
sum(1, 2, 3); // 6
function greet({ name, city }) { // destructure an argument
return `${name} from ${city}`;
}Spread copies are shallow: nested objects and arrays are still shared. Use structuredClone() when you need a deep copy.
Modules and classes
ES modules let each file export values and import what it needs. Load the entry file with <script type="module">; module scripts are deferred automatically, run in strict mode and have their own scope. Browsers block modules opened from file://, so serve the folder from a local web server.
// price.js
export const GST = 0.18;
export function withGst(amount) {
return Math.round(amount * (1 + GST));
}
export default class Cart {
#items = []; // private field
add(item) { this.#items.push(item); return this; }
get total() { return this.#items.reduce((t, i) => t + i.price, 0); }
}
// main.js
import Cart, { withGst } from './price.js';
const cart = new Cart().add({ name: 'Notebook', price: 60 });
console.log(withGst(cart.total)); // 71Classes are cleaner syntax over JavaScript's prototypes. They support constructors, getters, static members, extends with super, and truly private #fields that code outside the class cannot read. If you know classes from object-oriented PHP, the ideas will feel familiar.
Optional chaining and nullish coalescing
Optional chaining ?. stops and returns undefined if the value before it is null or undefined, instead of throwing. Nullish coalescing ?? supplies a fallback only for null or undefined, whereas || also replaces 0, "" and false.
const order = { customer: { address: null }, discount: 0 };
order.customer?.address?.pin; // undefined, no TypeError
order.notify?.(); // calls only if notify exists
order.discount || 10; // 10 (0 treated as missing)
order.discount ?? 10; // 0 (0 is a real value)
const settings = {};
settings.theme ??= 'light'; // assign only if null or undefinedUse ?. where a value is genuinely optional, such as fields in an API response. Sprinkling it everywhere hides bugs that should fail loudly.
Practical exercise: modernise an old script
Take an older script written with var, string concatenation and function callbacks, or write one that formats a list of course objects into text.
- Replace every
varwithconstorlet. - Rewrite callbacks passed to
map,filterandforEachas arrow functions. - Replace string concatenation with template literals.
- Use destructuring in function parameters and spread to copy objects instead of changing them.
- Move helpers into a module, import them into
main.jsand load it withtype="module". - Replace manual
&&null checks with?.and default values with??.
You are done when the script gives the same output as before, has no var left and runs without console errors from a local server. Next, learn asynchronous code in our promises and async/await guide, which the PHP foundation course also uses for front-end work.
Common mistakes
- Assuming
constmakes an object immutable; it only stops the variable being reassigned. - Using an arrow function as an object method and then wondering why
thisis not the object. - Returning an object literal from an arrow function without wrapping it in parentheses.
- Using
||for defaults when0or an empty string is a valid value. - Opening a page that uses ES modules directly from the file system instead of a local server.
Frequently asked questions
Is ES6 the same as JavaScript?
ES6 is one edition of the ECMAScript standard that JavaScript follows. Modern JavaScript includes ES6 plus every later yearly edition.
Do I still need Babel?
Not for the features in this guide if you target current browsers. Build tools are still useful for bundling, minifying and for syntax such as JSX or TypeScript.
What is the difference between spread and rest?
They use the same ... syntax. Spread expands values where a list is expected, such as in an array literal or function call. Rest collects values into an array or object, such as in a parameter list or a destructuring pattern.
Should I learn old JavaScript syntax too?
Learn to read it, because older tutorials and codebases use var and callbacks, but write new code with modern syntax. Our JavaScript fundamentals guide covers the core language.