Hey DEV community! π ES6 introduced game-changing features every modern JavaScript developer still relies on β and in 2025, theyβre more essential than ever! Hereβs a quick guide to the must-know ES6 features for writing cleaner, faster, and better code. Letβs dive in! πββοΈ
1οΈβ£ let and const β Block-Scoped Variables
-> Before ES6, we only had var, which is function-scoped and can cause tricky bugs. ES6 introduced let and const:
let count = 0; // mutable const name = "DEV"; // immutable β
Use let for values that change
β
Use const for constants
2οΈβ£ Arrow Functions β‘οΈ
-> Arrow functions provide shorter syntax and fix this context issues:
const add = (a, b) => a + b; No more .bind(this) for callbacks! 3οΈβ£ Template Literals π
-> String interpolation becomes a breeze:
const user = "Alice"; console.log(`Hello, ${user}!`); Also supports multi-line strings without messy \n.
4οΈβ£** Destructuring** π§©
-> Unpack arrays or objects easily:
const [first, second] = [10, 20]; const { title, year } = { title: "ES6", year: 2015 }; Perfect for cleaner code when working with props or API responses.
5οΈβ£** Default Parameters **βοΈ
-> Set default values for function arguments:
function greet(name = "Manu") { console.log(`Hello, ${name}!`); } 6οΈβ£ Rest & Spread Operators β¨
-> Rest: Collects remaining items into an array:
function sum(...nums) { return nums.reduce((a, b) => a + b); } -> Spread: Expands arrays or objects:
const arr = [1, 2, 3]; const newArr = [...arr, 4, 5]; 7οΈβ£ Enhanced Object Literals π οΈ
-> Define objects with shorthand properties and methods:
const age = 30; const user = { name: "Manu", age, greet() { console.log("Hi!"); } }; 8οΈβ£ Promises π€
-> Handle async operations more elegantly than callbacks:
fetch("https://api.example.com/data") .then(response => response.json()) .then(data => console.log(data)) .catch(err => console.error(err)); 9οΈβ£ Modules π¦
-> Break code into reusable files:
// math.js export function add(a, b) { return a + b; } // app.js import { add } from './math.js'; console.log(add(2, 3)); π Conclusion
These ES6 features transformed JavaScript from a simple scripting language into the powerful, modern tool we use today.
Top comments (0)