1. Arrow Functions
Arrow functions provide a more concise syntax for writing functions in JavaScript. They also lexically bind the this
value, making them different from regular functions in terms of how this
is handled.
Example:
// Regular function
function add(a, b) {
return a + b;
}
// Arrow function
const add = (a, b) => a + b;
console.log(add(2, 3)); // Output: 5(code-box)
Explanation:
- The arrow function syntax
(a, b) => a + b
is shorter than the traditional function declaration. - If the function body contains only one expression, you can omit the curly braces
{}
and thereturn
keyword.
2. Destructuring Assignment
Destructuring assignment allows you to unpack values from arrays or properties from objects into distinct variables.
Example:
// Array destructuring
const numbers = [1, 2, 3];
const [first, second, third] = numbers;
console.log(first); // Output: 1
console.log(second); // Output: 2
console.log(third); // Output: 3
// Object destructuring
const person = {
name: 'John',
age: 30
};
const { name, age } = person;
console.log(name); // Output: John
console.log(age); // Output: 30(code-box)
Explanation:
- In array destructuring,
[first, second, third] = numbers
assigns the first, second, and third elements of thenumbers
array to the variablesfirst
,second
, andthird
, respectively. - In object destructuring,
{ name, age } = person
assigns thename
andage
properties of theperson
object to the variablesname
andage
.
3. Template Literals
Template literals provide an easy way to create multi-line strings and perform string interpolation using backticks (`
) instead of single or double quotes.
Example:
const name = 'Alice';
const greeting = `Hello, ${name}!`;
console.log(greeting); // Output: Hello, Alice!
// Multi-line string
const multiLine = `This is
a multi-line
string.`;
console.log(multiLine);
/*
Output:
This is
a multi-line
string.
*/(code-box)
Explanation:
- The template literal
`Hello, ${name}!`
includes the variablename
directly within the string using${}
syntax. - Template literals can span multiple lines, making it easier to create multi-line strings.
4. Spread and Rest Operators
The spread operator (...
) allows an iterable (like an array or object) to be expanded in places where zero or more arguments or elements are expected. The rest operator also uses ...
, but it collects multiple elements into an array.
Example:
// Spread operator
const arr1 = [1, 2, 3];
const arr2 = [...arr1, 4, 5, 6];
console.log(arr2); // Output: [1, 2, 3, 4, 5, 6]
const obj1 = { a: 1, b: 2 };
const obj2 = { ...obj1, c: 3 };
console.log(obj2); // Output: { a: 1, b: 2, c: 3 }
// Rest operator
function sum(...numbers) {
return numbers.reduce((acc, number) => acc + number, 0);
}
console.log(sum(1, 2, 3)); // Output: 6(code-box)
Explanation:
- The spread operator
...arr1
spreads the elements ofarr1
into a new array, and...obj1
spreads the properties ofobj1
into a new object. - The rest operator
...numbers
collects all remaining arguments passed to thesum
function into an array callednumbers
.
5. Classes
Classes in JavaScript provide a more convenient and clear syntax for creating objects and handling inheritance.
Example:
class Person {
constructor(name, age) {
this.name = name;
this.age = age;
}
greet() {
console.log(`Hello, my name is ${this.name} and I am ${this.age} years old.`);
}
}
const alice = new Person('Alice', 25);
alice.greet(); // Output: Hello, my name is Alice and I am 25 years old.(code-box)
Explanation:
- The
class
keyword defines a class namedPerson
with a constructor that initializes thename
andage
properties. - The
greet
method is defined within the class and can be called on an instance of the class.
6. Modules (import/export)
Modules allow you to split your code into separate files and reuse them. You can use the export
keyword to export functions, objects, or primitive values from a module and the import
keyword to import them into another module.
Example:
// File: math.js
export function add(a, b) {
return a + b;
}
export const PI = 3.14159;
// File: app.js
import { add, PI } from './math.js';
console.log(add(2, 3)); // Output: 5
console.log(PI); // Output: 3.14159(code-box
Explanation:
- In
math.js
, theadd
function andPI
constant are exported using theexport
keyword. - In
app.js
, theadd
function andPI
constant are imported frommath.js
using theimport
keyword.
These features collectively make JavaScript more powerful and easier to work with, promoting cleaner and more maintainable code.