ES6 Features

Arrow Functions:

Arrow functions provide a concise syntax for writing functions. They don't have their own this and arguments and are especially useful for short, one-liner functions.

                                      
                                        // Traditional function
function add(a, b) {
    return a + b;
}

// Arrow function
const addArrow = (a, b) => a + b;

console.log(add(5, 3));      // Output: 8
console.log(addArrow(5, 3)); // Output: 8
                                      
                                    

Template Literals:

Template literals allow you to embed expressions inside string literals, making string manipulation more readable.

                                      
                                        const name = "John";
const greeting = `Hello, ${name}!`;

console.log(greeting);  // Output: Hello, John!
                                      
                                    

Destructuring:

Destructuring allows you to extract values from arrays or properties from objects into distinct variables.

                                      
                                        // Destructuring an array
const numbers = [1, 2, 3];
const [first, second, third] = numbers;

console.log(first);  // Output: 1
console.log(second); // Output: 2

// Destructuring an object
const person = { name: "Alice", age: 25 };
const { name, age } = person;

console.log(name); // Output: Alice
console.log(age);  // Output: 25
                                      
                                    

let and const:

let is used to declare variables whose values can be reassigned. const is used for constants whose values cannot be reassigned.

                                          
                                            let count = 0;
count = 1;  // Valid

const pi = 3.14;
// pi = 3.14159;  // Error: Assignment to a constant variable                                            
                                          
                                        

Classes:

ES6 introduced class syntax, allowing for a more convenient way to create constructor functions and prototype-based inheritance.

                                          
                                            class Animal {
                                                constructor(name) {
                                                    this.name = name;
                                                }
                                            
                                                speak() {
                                                    console.log(`${this.name} makes a sound`);
                                                }
                                            }
                                            
                                            class Dog extends Animal {
                                                speak() {
                                                    console.log(`${this.name} barks`);
                                                }
                                            }
                                            
                                            const dog = new Dog("Buddy");
                                            dog.speak();  // Output: Buddy barks                                                                                                                                    
                                          
                                        

Modules:

ES6 introduced a standardized way to organize code into reusable and maintainable modules.

                                          
                                            // math.js (module)
export const add = (a, b) => a + b;
export const subtract = (a, b) => a - b;

// main.js
import { add, subtract } from './math';

console.log(add(5, 3));       // Output: 8
console.log(subtract(5, 3));  // Output: 2                                                                                                                                   
                                          
                                        

Modules allow you to encapsulate functionality and import/export pieces of code between files.