learn2kode.in

ES6 features in JavaScript

ES6, also known as ECMAScript 2015, is a major update to JavaScript that introduced modern syntax and powerful features to make code cleaner, shorter, and more readable. Today, ES6 features are widely used in modern web development, including frameworks like React, Vue, and Node.js.
In this tutorial, you’ll learn the most important ES6 features with simple examples:

1. let and const

let

let allows you to declare block-scoped variables, which is safer than var.

let count = 10;
count = 15; // allowed
const
const is used for values that should not be reassigned.
const PI = 3.14;
// PI = 3.5 ❌ Error

Use const by default, and let only when reassignment is needed.

2. Arrow Functions (=>)

Arrow functions provide a shorter syntax for writing functions and handle this more predictably.
Traditional Function
function add(a, b) {
  return a + b;
}
Arrow Function
const add = (a, b) => a + b;

3. Spread Operator (...)

The spread operator is used to expand arrays or objects.
Copy an Array
const numbers = [1, 2, 3];
const newNumbers = [...numbers, 4, 5];
Merge Objects
const user = { name: "John" };
const details = { age: 25 };

const fullUser = { ...user, ...details };

4. map() Method

The map() method creates a new array by transforming each element.
const numbers = [1, 2, 3, 4];
const squared = numbers.map(num => num * num);
Output

5. filter() Method

The filter() method returns a new array that meets a condition.
const ages = [12, 18, 25, 14];
const adults = ages.filter(age => age >= 18);
Output

map() vs filter() Comparison

Method Purpose Returns
map() Transform data New array (same length)
filter() Filter data New array (filtered)

Why ES6 Is Important

ES6 vs ES5 comparison table

Feature ES5 ES6 (ES2015)
Variable Declaration var let, const
Function Syntax Traditional functions Arrow functions () => {}
Scope Function scope Block scope
String Handling String concatenation Template literals `Hello ${name}`
Default Parameters Not supported Supported
Array Methods Limited map(), filter(), reduce()
Object Handling Manual assignments Destructuring, spread operator
Modules Not supported import / export
Classes Prototype-based class syntax
Readability More verbose Cleaner and concise