ecma stands for ecma international (formerly european computer manufacturers association). it develops standards in various fields of programming, including the ecmascript standard, which is the foundation for the javascript language.
what is es6
javascript introduced a major update in 2015 known as ecmascript 2015. since it was the sixth edition of the ecmascript standard, it was named es6
here are some key es6 features:
1. let and const (block-scoped variables)
-
let: allows block-scoped variables. -
const: defines constants that cannot be reassigned.
let name = "john";
name = "doe"; // ✅ allowed
const age = 30;
age = 31; // ⌠error: assignment to constant variable
2. arrow fu nctions (shorter function syntax)
// regular function
function add(a, b) {
return a + b;
}
// arrow function
const add = (a, b) => a + b;
3. template literals (string interpolation)
let name = "alice";
let message = `hello, ${name}!`; // ✅ uses backticks (``)
console.log(message); // hello, alice!
4. destructuring (extract values from arrays/objects)
// array destructuring
let [a, b] = [10, 20];
console.log(a, b); // 10 20
// object destructuring
let person = { name: "john", age: 25 };
let { name, age } = person;
console.log(name, age); // john 25
5. spread & rest operators (...)
-
spread operator (expands arrays/objects)
let arr1 = [1, 2, 3];
let arr2 = [...arr1, 4, 5]; // [1, 2, 3, 4, 5]
let obj1 = { a: 1, b: 2 };
let obj2 = { ...obj1, c: 3 }; // { a: 1, b: 2, c: 3 }
rest operator (collects remaining values)
function sum(...nums) {
return nums.reduce((acc, num) => acc + num, 0);
}
console.log(sum(1, 2, 3, 4)); // 10
6. default parameters
function greet(name = "guest") {
console.log(`hello, ${name}!`);
}
greet(); // hello, guest!
7. promises (async operations handling)
let fetchdata = new promise((resolve, reject) => {
settimeout(() => resolve("data received"), 2000);
});
fetchdata.then((data) => console.log(data)); // data received (after 2s)
8. classes (oop-style syntax)
class person {
constructor(name, age) {
this.name = name;
this.age = age;
}
greet() {
console.log(`hi, i'm ${this.name}`);
}
}
let user = new person("alice", 22);
user.greet(); // hi, i'm alice
9. modules (import/export)
-
exporting a module
// file: math.js
export const add = (a, b) => a + b;
export default function subtract(a, b) { return a - b; }
importing a module
// file: main.js
import subtract, { add } from './math.js';
console.log(add(5, 3)); // 8
console.log(subtract(5, 3)); // 2
10. map and set
let map = new map();
map.set("name", "alice");
console.log(map.get("name")); // alice
let set = new set([1, 2, 3, 3]);
console.log(set); // set {1, 2, 3}


