-->

  • ES6 Rest Operator in Javascript


    ES6 rest operator


     

    ES6 Rest Operator

     

    The rest operator in JavaScript is used to collect all remaining arguments of a function into a single array. It is represented by three consecutive dots (...) followed by a parameter name. This operator is particularly useful when you have a function that accepts a variable number of arguments.

     
    ES6 is different version of the ECMAScript specification. Which is the standard that defines the scripting language that javascript is based on.

    ES6 release on 2015 and there are so many features Arrow function, classes, let and const for variables.

    Suppose we have a function with some parameter like -

     

    function  sum(a, b) {
        return a+b;
    }
    
    sum(2, 6)  // output is 8
    
    




    But there is some problem because of if user have 2 paramters then we  will enter the value of 2 paramters only But if we use this like below - 

     


    function sum(...args) {
        console.log(args)
    }
    
    sum(2, 5, 4, 7)
    
    
    



    when we use this type of method we can print all the numbers as a argument value like 2, 5, 4, 7 and also we can add these all numbers.


    Actually we have used this code in reactjs called as TheArrow.jsx



    Example



    import React from 'react'
    
    function TheRest() {
    
        const therest = ( num1, num2, ...args) => {
           
                console.log(`The number is ${args}`)
                let sum = num1 + num2;
                for(let i = 0; i < args.length; i++) {
                    sum +=args[i];
                }
                console.log('The total sum is' , sum);
        }
    
        therest(2, 3, 4, 5)
      return (
        <div>
        
        <h1>Check console</h1>
        
        
        </div>
      )
    }
    
    export default TheRest
    




    Output



    ES6 Rest Operator in Javascript



    Disclaimer

     

    All tutorials are for informational and educational purposes only and have been made using our own routers, servers, websites and other vulnerable free resources. we do not contain any illegal activity. We believe that ethical hacking, information security and cyber security should be familiar subjects to anyone using digital information and computers. Hacking Truth is against misuse of the information and we strongly suggest against it. Please regard the word hacking as ethical hacking or penetration testing every time this word is used. We do not promote, encourage, support or excite any illegal activity or hacking.


  • 0 comments:

    Post a Comment

    For Any Tech Updates, Hacking News, Internet, Computer, Technology and related to IT Field Articles Follow Our Blog.