Online admissions open for the SEO, web development and networking & cybersecurity courses. Enquire for the next batch.

Modern JavaScript (ES6 and Later): The Features You Use Every Day

5 min read Updated By the SkillNewton team

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.

FeatureEdition
let, const, arrow functions, classes, modules, template literals, destructuring, promisesES2015
async functions and awaitES2017
Object spread and restES2018
Optional chaining ?. and nullish coalescing ??ES2020
Logical assignment such as ??=ES2021
Private class fields #name and top-level await in modulesES2022

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 interpolation

Template 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));       // 71

Classes 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 undefined

Use ?. 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.

  1. Replace every var with const or let.
  2. Rewrite callbacks passed to map, filter and forEach as arrow functions.
  3. Replace string concatenation with template literals.
  4. Use destructuring in function parameters and spread to copy objects instead of changing them.
  5. Move helpers into a module, import them into main.js and load it with type="module".
  6. 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 const makes an object immutable; it only stops the variable being reassigned.
  • Using an arrow function as an object method and then wondering why this is not the object.
  • Returning an object literal from an arrow function without wrapping it in parentheses.
  • Using || for defaults when 0 or 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.

Want to Learn This Properly?

The PHP Web Development: Foundation & Practical Implementation (Online) turns guides like this into practised skills, with projects on real websites.

WhatsApp Call Us Now

Free counselling

Send an Enquiry

Tell us which course you are interested in. We reply by email or phone with the next batch and how to enrol.

Protected by reCAPTCHA. Google Privacy and Terms apply.

We reply by email or phone within working hours. No spam.