Skip to content
Menu
TechFacts007.in
  • Information Zone
  • Reviews
    • Apps Reviews
    • PC’s
  • Gadgets
  • Hosting
    • Domain and Hosting
      • cPanel
    • WordPress
  • More
    • Tech Facts
    • Tips & Tricks
TechFacts007.in
March 9, 2022September 28, 2022

Spread Operator and Rest Parameters in JavaScript

Spread Operator and Rest Parameter

 

Spread Operator


The Spread operator in JavaScript was introduced in ES6 (2015). Many new features were introduced in JavaScript. The syntax of the spread operator is 3 dots (…). The spread operator allows us to spread out the elements of an iterable object such as an Array, Map, or Set. By the use of a spread operator, we can merge arrays, extract elements from an array, split a string from characters, copy array items, etc…

 

Concatenate arrays

We can concatenate arrays by the concat method but we can also do the same with the spread operator. An example is given below.

let arr1 = [1, 2, 3]
let arr2 = [7, 8, 9] 
let newArr = [...arr1, ...arr2] 
console.log(newArr)

OUTPUT

[1, 2, 3, 7, 8, 9]

 

Extract elements from an array

The spread operator can also be used to extract elements from an array. we can extract elements by index numbers but we can also extract array items by the below example,

// WITH SPREAD OPERATOR
let arr = [10, 15, 18, 19]
let [a, b, c] = [...arr]
console.log(a, b, c) // 10 15 18


// WITHOUT SPREAD OPERATOR
let arr = [10, 15, 18, 19]
let a = arr[0]
let b = arr[1]
let c = arr[2]
console.log(a, b, c) // 10 15 18

 

Split string by characters

To split a string character by character we can use the split method on the string but we can also do the same by spread operator. The below example will give us an array of characters.

let str = "Hello World"
let splitted = [...str]

console.log(splitted)

OUTPUT

["H", "e", "l", "l", "o", " ", "W", "o", "r", "l", "d"]

 

Copy array items

To copy an array to another variable we can do it by spread operator. Without a spread operator, we can not do this.

let arr1  = [1, 2, 3]
let arr2 = [...arr1]
arr1.push(4)

console.log(arr1, arr2)

OUTPUT

[ 1, 2, 3, 4 ] [ 1, 2, 3 ]

 


Rest Parameter

The rest parameter in javascript was introduced in ECMA Script 6 in 2015. This feature allows us to handle multiple parameters in any function. We can pass an indefinite number of parameters to any function. By the use of the rest parameter, we can call a function with any number of arguments.

The rest parameter is used by 3 dots(…). The syntax of the rest parameter is similar to the spread operator.

Here is an example of a sum function that accepts a limited number of parameters.

function findSum(a, b, c){
    return a + b + c;
}

console.log(findSum(2, 4, 6))

OUTPUT

12

Now we are creating the same function that accepts indefinite number of parameters.

Example 1:

function findSum(...args) {
    return args.reduce(function (prev, curr) {
        return prev + curr
    }, 0)
}

console.log(findSum(2, 4, 6))

OUTPUT

12

 

Example 2:

function myFunc(type, ...args) {
    if(type === 'multiply'){
        return args.reduce(function(prev, curr){
            return prev * curr
        }, 1)
    }
    else if(type === 'sum'){
        return args.reduce(function(prev, curr){
            return prev + curr
        }, 0)
    }
}

console.log(myFunc('sum', 1, 2, 3, 4, 5))
console.log(myFunc('multiply', 3, 4, 8))

OUTPUT

15
96

 

Example 3:

function sum(...args){
    return args.reduce((prev, curr) => prev + curr, 0);
}

let arr1 = [1, 2, 3, 4, 5]
let arr2 = [5, 6, 7, 8]

console.log(sum(...arr1, ...arr2));

OUTPUT

41

 


MUST READ

Arrow Functions in JavaScript

Post Views: 23

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Trending

  • Tech Facts
    52.376552,5.198303(Dead Body Found) Google Map
  • Information Zone
    Create Sitemap for React.Js Web App – Sitemap.xml for react app
  • Information Zone, Programming Languages, Tutorials
    React Hook To Check If User Has Changed/Minimize the Browser Tab
  • How to, Information Zone, Tips & Tricks
    Download any paid Software. Get any software free for lifetime
  • Codes, Programming Languages
    Find out if you can reach the last tile – Program Code
  • Codes, Programming Languages
    Write a function to take a positive integer and return the largest power of two less than or equal to this integer – JavaScript Program
  • How to, Information Zone, Tips & Tricks
    Encrypt Javascript and CSS Code – Hide Original Code from Users
  • PC's, Tips & Tricks
    Mount and Unmount Virtual Hard Disk (CMD Command and batch File Method)

Recent Posts

  • Worker Threads in JavaScript: Enhancing Performance with Parallelism July 31, 2023
  • Create Sitemap for React.Js Web App – Sitemap.xml for react app May 27, 2023
  • The Importance of Sitemap.xml: Enhancing Website Visibility and SEO May 27, 2023
  • Web Page Generation – Difference between SSR, CSR and SSG May 26, 2023
  • React.js vs. Next.js – Choosing the Right Framework for Your Web Development Needs May 26, 2023
  • NGINX vs. Apache: Exploring the Differences and Choosing the Right Web Server May 21, 2023
  • Setting Up NGINX on Ubuntu Server and Activating a Domain on NGINX Server May 21, 2023
  • Functional Component vs Class Component in React.Js January 18, 2023
  • React.Js Lifecycle Methods January 18, 2023
  • What is componentDidMount and useEffect with Example January 18, 2023

Categories

  • Apps Reviews (4)
  • Gadgets (4)
  • Hack With Us (2)
  • Hosting (12)
    • Domain and Hosting (7)
      • cPanel (5)
    • Wordpress (5)
  • How to (11)
  • Information Zone (65)
  • PC's (7)
  • Product Reviews (11)
  • Programming Languages (11)
    • Codes (5)
    • Tutorials (6)
  • Smart Phones (8)
  • Tech Facts (13)
  • Tips & Tricks (13)

Browse By Date

December 2023
M T W T F S S
 123
45678910
11121314151617
18192021222324
25262728293031
« Jul    

More

Contact Us
Privacy Policy
About Us
About Admin

Advertisement

Other Sites

ShanuTheWebDev.in
QrCode.ShanuTheWebDev.in
©2023 TechFacts007.in | Powered by WordPress and Superb Themes!