JavaScript ES6 (ECMAScript 2015) and later versions introduced several new features and improvements to the language. These enhancements make JavaScript more powerful and easier to work with.
Arrow functions provide a shorter syntax for writing function expressions and lexically bind the this value.
const add = (a, b) => a + b;Template literals allow for easier string interpolation and multi-line strings.
const name = 'Josh';
const greeting = `Hello, ${name}!`;Destructuring allows for unpacking values from arrays or properties from objects into distinct variables.
const [a, b] = [1, 2];
const {name, age} = {name: 'Josh', age: 30};Default parameters allow you to set default values for function parameters.
function greet(name = 'Guest') {
return `Hello, ${name}!`;
}The rest operator (...) allows you to represent an indefinite number of arguments as an array. The spread operator allows an iterable to expand in places where multiple arguments or elements are expected.
function sum(...numbers) {
return numbers.reduce((acc, num) => acc + num, 0);
}
const arr = [1, 2, 3];
const newArr = [...arr, 4, 5];ES6 introduced a new syntax for creating objects using the class keyword.
class Person {
constructor(name, age) {
this.name = name;
this.age = age;
}
greet() {
return `Hello, my name is ${this.name} and I am ${this.age} years old.`;
}
}Promises provide a way to handle asynchronous operations more easily.
const fetchData = () => {
return new Promise((resolve, reject) => {
setTimeout(() => resolve('Data fetched'), 1000);
});
};
fetchData().then(data => console.log(data));ES6 introduced a standardized module system to JavaScript.
// Exporting a module
export const add = (a, b) => a + b;
// Importing a module
import { add } from './math.js';ES6+ has significantly improved JavaScript by introducing new syntax and features that make the language more powerful and easier to work with. These features help developers write cleaner, more maintainable code.