JavaScript ES6 Features Complete Guide
Short answer: ES6 (also called ES2015) was a major update that added let/const, arrow functions, template literals, destructuring, spread/rest, classes, modules, promises and more. These features are supported in all current browsers and Node.js, and they are the standard way to write modern JavaScript.
let and const
let count = 0; // can be reassigned
const PI = 3.14159; // cannot be reassigned
if (true) {
let inside = "block scoped";
}
// inside is not accessible here
Both are block-scoped. Prefer const by default and use let only when the value changes. Avoid var, which is function-scoped and hoisted in surprising ways. Note that const prevents reassignment, not mutation: a const array can still be pushed to.
Arrow functions
const add = (a, b) => a + b;
const square = (n) => n * n;
const greet = (name) => {
return "Hello " + name;
};
[1, 2, 3].map((n) => n * 2); // [2, 4, 6]
Arrow functions are shorter and do not have their own this; they use the this of the surrounding code, which is helpful in callbacks.
Template literals
const name = "Asha";
const msg = `Hello, ${name}! 2 + 3 = ${2 + 3}`;
const multi = `line one
line two`;
Destructuring
const user = { name: "Asha", age: 30, city: "Pune" };
const { name, age } = user;
const [first, second = 0, ...rest] = [10, 20, 30, 40];
// first = 10, second = 20, rest = [30, 40]
function show({ name, age = 18 }) {
console.log(name, age);
}
Spread and rest
const a = [1, 2];
const b = [...a, 3, 4]; // [1, 2, 3, 4]
const base = { theme: "dark" };
const settings = { ...base, lang: "en" }; // copy and extend
function sum(...numbers) { // rest: collects arguments
return numbers.reduce((t, n) => t + n, 0);
}
Spread makes a shallow copy; nested objects are still shared.
Default parameters and shorthand
function greet(name = "friend") {
return "Hi " + name;
}
const x = 1, y = 2;
const point = { x, y }; // same as { x: x, y: y }
const obj = { sayHi() { return "hi"; } };
Classes
class Animal {
constructor(name) {
this.name = name;
}
speak() {
return this.name + " makes a sound";
}
}
class Dog extends Animal {
speak() {
return this.name + " barks";
}
}
console.log(new Dog("Rex").speak());
Classes are cleaner syntax over JavaScript's existing prototype system.
Modules
// math.js
export const add = (a, b) => a + b;
export default function multiply(a, b) { return a * b; }
// app.js
import multiply, { add } from "./math.js";
Promises and iterators
- Promises standardized asynchronous results; see promises and async/await.
for...ofloops over arrays, strings, Maps and Sets.MapandSetstore key-value pairs and unique values.Symbolcreates unique identifiers.
const unique = [...new Set([1, 2, 2, 3])]; // [1, 2, 3]
for (const ch of "hey") console.log(ch);
Quick reference
| Feature | Old way | ES6 way |
|---|---|---|
| Variables | var | let / const |
| Functions | function(a) { return a * 2 } | a => a * 2 |
| Strings | "Hi " + name | `Hi ${name}` |
| Copy and merge | Object.assign | Spread { ...a, ...b } |
| Imports | require() | import / export |
Continue with array methods you must know.
FAQs
What is the difference between ES6 and JavaScript?
ES6 is a version of the ECMAScript standard that JavaScript implements. Later yearly versions (ES2016 onwards) added more features.
Do I need Babel for ES6?
Not for current browsers. It is only needed if you must support very old environments.
Why use const instead of let?
It signals that a binding will not be reassigned, making code easier to read and reason about.
Do arrow functions replace normal functions?
No. Use normal functions when you need your own this, arguments or a constructor.
