⚡ Day 12 — Arrow Functions & ES6 Features
Hi Guys, Welcome to Day 12 🌟 JavaScript has evolved a lot — and **ES6 (ECMAScript 2015)** introduced modern features that make coding cleaner and easier. Today, you’ll learn about **arrow functions**, **let/const**, **template literals**, **destructuring**, and more.
🧩 1️⃣ Arrow Functions
Arrow functions are a shorter and more elegant way to write functions.
// Normal function
function greet(name) {
return "Hello " + name;
}
// Arrow function
const greetArrow = (name) => "Hello " + name;
console.log(greetArrow("Rahul"));
🔸 2️⃣ let & const
let and const are block-scoped variables (better than var).
let age = 23; const country = "India"; age = 24; // country = "USA"; ❌ Error - const cannot be changed
🔹 3️⃣ Template Literals
Use backticks (`) to embed variables directly.
let name = "Rahul";
let msg = `Hello ${name}, welcome to JavaScript!`;
console.log(msg);
🔹 4️⃣ Object Destructuring
let user = {name: "Rahul", age: 23, city: "Hyderabad"};
let {name, age, city} = user;
console.log(name, age, city);
🔹 5️⃣ Spread & Rest Operators
let arr1 = [1, 2, 3];
let arr2 = [...arr1, 4, 5];
console.log(arr2);
function sum(...numbers) {
return numbers.reduce((a, b) => a + b);
}
console.log(sum(10, 20, 30));
🎯 Summary
Modern JavaScript (ES6+) helps you write cleaner, faster, and more efficient code. Next, we’ll move on to **Iterators, for...of, and for...in loops** — mastering data traversal in a modern way!
Comments
Post a Comment