Skip to main content

JavaScript Destructuring, Spread Operator, Rest Parameters, Nullish Coalescing Operator, and Logical Assignment Operators

JavaScript offers several powerful syntactic features for manipulating data and simplifying code: destructuring, spread operator, rest parameters, nullish coalescing operator, and logical assignment operators. These features enhance the language's expressiveness and make it easier to write concise and readable code.


Destructuring

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


Object Destructuring:


const person = { name: "John", age: 30 };


// Extract name and age

const { name, age } = person;


console.log(name); // John

console.log(age); // 30


Array Destructuring:


const colors = ["red", "green", "blue"];


// Extract the first two colors

const [first, second] = colors;


console.log(first); // red

console.log(second); // green


Spread Operator

The spread operator (...) allows you to expand an array or object into individual elements within a new array or object.


Array Expansion:


const numbers1 = [1, 2, 3];

const numbers2 = [4, 5, 6];


// Create a new array by combining both arrays

const combinedNumbers = [...numbers1, ...numbers2];


console.log(combinedNumbers); // [1, 2, 3, 4, 5, 6]


Object Expansion:


const person1 = { name: "Alice" };

const person2 = { age: 25 };


// Create a new object by merging both objects

const mergedPerson = { ...person1, ...person2 };


console.log(mergedPerson); // { name: "Alice", age: 25 }


Rest Parameters

Rest parameters are used to collect remaining arguments passed to a function into an array.



function sum(...numbers) {

  // numbers is an array containing all the arguments passed to the function

  return numbers.reduce((a, b) => a + b, 0);

}


console.log(sum(1, 2, 3, 4, 5)); // 15


Nullish Coalescing Operator (??)

The nullish coalescing operator (??) returns its right-hand operand if the left-hand operand is null or undefined, otherwise it returns the left-hand operand.


const name = "John";

const username = name ?? "guest";


console.log(username); // John

In the above example, since the value of name is not null or undefined, the value of username is set to name.


Logical Assignment Operators (??=, &&=, ||=)

Logical assignment operators combine a logical operator with an assignment operator to perform a conditional assignment.


Nullish Assignment Operator (??=):


let name;

name ??= "guest";

console.log(name); // guest

The above code assigns the value "guest" to the name variable if it is currently null or undefined.


And Assignment Operator (&&=):


let isLoggedIn = false;

isLoggedIn &&= 1;

console.log(isLoggedIn); // false

The above code assigns the value 1 to the isLoggedIn variable only if it is currently true.


Or Assignment Operator (||=):


let isLoggedIn = null;

isLoggedIn ||= 0;

console.log(isLoggedIn); // 0

The above code assigns the value 0 to the isLoggedIn variable if it is currently null or undefined.


Conclusion

Destructuring, spread operator, rest parameters, nullish coalescing operator, and logical assignment operators are powerful tools that can greatly enhance the readability, conciseness, and expressiveness of your JavaScript code. By leveraging these features, you can write code that is more maintainable, bug-free, and efficient.

Comments

Archive

Show more

Topics

Show more