JavaScript ES6, officially known as ECMAScript 2015, marked a major turning point in the evolution of the language. It introduced many powerful features that made JavaScript more modern, efficient, and developer-friendly. Whether you’re new to JavaScript or upgrading from older versions, understanding ES6 is essential for writing cleaner and more maintainable code.
This tutorial walks you through the most important ES6 features, how they work, and why they matter.
1. let
and const
Before ES6, we used var
for declaring variables. ES6 introduced let
and const
for better scope control.
let
– Block-scoped variable:
javascriptCopyEditlet name = "Alice";
if (true) {
let name = "Bob";
console.log(name); // Bob
}
console.log(name); // Alice
const
– Constant (cannot be reassigned):
javascriptCopyEditconst PI = 3.14;
// PI = 3.1415; // Error
Use let
for variables that may change, and const
for constants.
2. Arrow Functions
Arrow functions are a shorter way to write functions.
Traditional function:
javascriptCopyEditfunction add(a, b) {
return a + b;
}
Arrow function:
javascriptCopyEditconst add = (a, b) => a + b;
Arrow functions are especially useful for inline functions and callbacks.
3. Template Literals
With template literals, you can embed variables and expressions directly into strings using backticks (`\
).
javascriptCopyEditlet name = "Alice";
let message = `Hello, ${name}!`;
console.log(message); // Hello, Alice!
Template literals also support multiline strings:
javascriptCopyEditlet text = `This is
a multiline
string.`;
4. Destructuring Assignment
Destructuring lets you unpack values from arrays or properties from objects easily.
Array Destructuring:
javascriptCopyEditlet [a, b] = [1, 2];
console.log(a); // 1
Object Destructuring:
javascriptCopyEditlet person = { name: "Alice", age: 25 };
let { name, age } = person;
console.log(name); // Alice
5. Default Parameters
You can now give function parameters default values.
javascriptCopyEditfunction greet(name = "Guest") {
console.log("Hello, " + name);
}
greet(); // Hello, Guest
greet("Alice"); // Hello, Alice
6. Rest and Spread Operators
Rest (...
) collects arguments into an array:
javascriptCopyEditfunction sum(...numbers) {
return numbers.reduce((a, b) => a + b);
}
console.log(sum(1, 2, 3)); // 6
Spread (...
) expands arrays:
javascriptCopyEditlet arr1 = [1, 2];
let arr2 = [...arr1, 3, 4];
console.log(arr2); // [1, 2, 3, 4]
7. Classes
ES6 introduced a class-based syntax for object-oriented programming.
javascriptCopyEditclass Person {
constructor(name) {
this.name = name;
}
greet() {
console.log(`Hello, I'm ${this.name}`);
}
}
let p = new Person("Alice");
p.greet(); // Hello, I'm Alice
Under the hood, classes still use prototypes but offer a cleaner syntax.
8. Modules (Import/Export)
ES6 modules let you split your code into files and import/export functionality.
In math.js
:
javascriptCopyEditexport function add(a, b) {
return a + b;
}
In main.js
:
javascriptCopyEditimport { add } from './math.js';
console.log(add(2, 3)); // 5
Note: In browsers, ES6 modules require <script type="module">
or a build tool like Webpack.
9. Promises
ES6 introduced Promises for better asynchronous programming.
javascriptCopyEditlet promise = new Promise(function(resolve, reject) {
setTimeout(() => resolve("Done!"), 1000);
});
promise.then(result => console.log(result)); // Done!
Promises replace deeply nested callbacks and work well with async/await
(introduced in ES8).
Why ES6 Matters
- Cleaner syntax: More concise and readable code
- Better structure: Classes, modules, and arrow functions simplify large projects
- Modern features: Destructuring, default parameters, and rest/spread save time and reduce bugs
Conclusion
ES6 brought JavaScript into the modern era with powerful new syntax and features. Mastering ES6 is essential for any developer working with modern frameworks like React, Vue, or Angular. From let
and const
to classes and modules, ES6 makes writing JavaScript more efficient, maintainable, and fun.
To practice, try rewriting older ES5 code using ES6 features or build small projects using ES6 syntax. You’ll quickly see how much easier your code becomes!