Scalable Business for Startups
Get the oars in the water and start rowing. Execution is the single biggest factor in achievement so the faster and better your execution.
Chapter 17: ES6 and Modern JavaScript Features
In this chapter, we’ll cover key features and syntax enhancements introduced with ES6 (ECMAScript 2015) and beyond. ES6 marked a significant update in JavaScript, adding modern programming paradigms, cleaner syntax, and features that greatly enhance functionality and readability. This chapter will cover each feature with detailed explanations and examples.
1. Let and Const
let
and const
provide block-scoping, unlike var
, which is function-scoped.
let
: Used for variables that can be reassigned.const
: Used for variables that remain constant after initialization.
function scopeExample() {
var x = 10; // Function-scoped
if (true) {
let y = 20; // Block-scoped
const z = 30; // Block-scoped and cannot be reassigned
console.log(y, z); // 20, 30
}
console.log(x); // 10
// console.log(y); // Error: y is not defined
}
2. Arrow Functions
Arrow functions (=>
) provide a shorthand for writing functions and do not bind their own this
.
const greet = name => `Hello, ${name}!`;
console.log(greet("Alice")); // Hello, Alice!
// Example of this-binding
function Counter() {
this.count = 0;
setInterval(() => {
this.count++; // `this` refers to the Counter object
console.log(this.count);
}, 1000);
}
new Counter(); // Increments count every second
3. Template Literals
Template literals allow embedding expressions within string literals using backticks (`
) and ${expression}
syntax.
const name = "Alice";
const age = 25;
const message = `Hello, my name is ${name} and I am ${age} years old.`;
console.log(message); // Hello, my name is Alice and I am 25 years old.
4. Default Parameters
Functions can have default values for parameters if not provided during the call.
function greet(name = "Guest") {
return `Hello, ${name}!`;
}
console.log(greet()); // Hello, Guest!
console.log(greet("Alice")); // Hello, Alice!
5. Destructuring Assignment
Destructuring allows unpacking values from arrays or properties from objects.
Array Destructuring
const colors = ["red", "green", "blue"];
const [first, second, third] = colors;
console.log(first, second, third); // red, green, blue
Object Destructuring
const person = { name: "Alice", age: 25 };
const { name, age } = person;
console.log(name, age); // Alice, 25
6. Spread and Rest Operators (`...`)
Spread: Expands elements of an array or object into individual elements.
Rest: Aggregates multiple elements into a single array.
const arr1 = [1, 2, 3];
const arr2 = [4, 5, 6];
const combined = [...arr1, ...arr2];
console.log(combined); // [1, 2, 3, 4, 5, 6]
function sum(...numbers) {
return numbers.reduce((a, b) => a + b, 0);
}
console.log(sum(1, 2, 3)); // 6
7. Enhanced Object Literals
ES6 introduced shorthand syntax for defining object properties and methods.
const name = "Alice";
const age = 25;
const person = {
name, // Shorthand for name: name
age, // Shorthand for age: age
greet() { // Method shorthand
return `Hello, I'm ${this.name}`;
}
};
console.log(person.greet()); // Hello, I'm Alice