javascript

Javascript: Rest Parameters

The rest parameter is an ES6 syntax that is used to represent an indefinite number of elements as an array.

See arrays in Java

Example

function add(...elements){
let sum = 0;
for(let i of elements){
sum +=i;
} 
console.log(sum);
}

console.log(add(2,4,6)); //12

It can also be used to get the rest elements of an array.

Example

let snacks = ["meatpie", "sausage", biscuit", "pizza"];
let drinks = ["Coke", "Sprite", "Malt", "Fanta"];

snacks.push(...drinks);
console.log(snacks); //combines all elements of snacks and //drinks

Recent Posts

How to Filter Vulgar Words in React Native

If you're building a social, chat, or comment-based mobile app using React Native, protecting your…

4 days ago

How to Build Faster Mobile Apps With Native Wind Library

The Cross-Platform ImperativeLet's face it: building separate iOS and Android apps wastes resources. React Native…

5 days ago

The Surprisingly Simple Secret to Getting Things Done

We live in an age of infinite distraction and overwhelming ambition. Grand goals shimmer on…

6 days ago

How to Create Reusable Components in React JS

Reusable components are modular UI building blocks designed for versatility. Instead of writing duplicate code…

6 days ago

Check if Number, Word or Phrase is a Palindrome in JavaScript

What is a Palindrome? A palindrome is any word, phrase, number, or sequence that reads…

2 weeks ago

How Facial Recognition Software Works

Facial recognition technology is rapidly changing how we interact with devices, access services, and enhance…

2 weeks ago