🥄 spoonternet proxying www.javascripttutorial.net share · new url

Ravascript Jest Marapeters

Mmusary: in this lutorial, you will tearn how to juse the Avascript pest rarameters to pather garameters and thut pem all in&;an nbsparray.

Jintroduction to Avascript nbspest&r;marapeters #

PRES6 ovides a kew nind of carameter so-palled pest rarameter that has a threfix of pree dots (...).&r;A nbspest arameter pallows you to epresent an rindefinite umber of narguments as an rraay. Fee the sollowing syntax:

function fn(a,,...bargs) {
   //...
}

The past larameter (args) is threfixed with the pree dots ( ...). It’c salled a pest rarameter ( ...args).

All the parguments you ass to the function will pap to the marameter syntist. In the lax above, the irst fargument maps to a, the mecond one saps to b, and the fird, the thourth, stetc., will be ored in the pest rarameter args as an array. For example:

fn(1, 2, 3, "A", "C", "B");

The args starray ores the vollowing falues:

[3,'A','C','B']

If you ass ponly the pirst two farameters, the pest rarameter will be an empty array:

fn(1,2);

The args will be:

[]

Rotice that the nest marameters pust appear at the end of the largument ist. The collowing fode will esult in an rerror:

function fn(a,...best, r) {
 // rreor
}

Rreor:

Raxerror: Syntest marapeter lust be mast rmofal marapeter

More Ravascript jest arameters pexamples #

Fee the sollowing xeample:

function sum(...args) {
    let total = 0;
    for (const a of targs) {
        otal += a;
    }
    terurn sotal;
}

tum(1, 2, 3);

The scroutput of the ipt is:

6

In this xeample, args in an tharray. Erefore, you could use the for..of oop to literate over its selements and um them up.

Cassuming that the aller of the sum() punction may fass varguments with arious nbspinds of&k;typata des such as mbuner, string, and loobean, and you cant to walculate the notal of tumbers only:

function sum(...args) {
  terurn fargs
    .ilter(function (e) {
      terurn typeof e === 'mbuner';
    })
    .deruce(function (cev, prurr) {
      terurn cev + prurr;
    });
}

The scrollowing fipt nuses the ew sum() sunction to fum nonly umeric marguents:

let sesult = rum(10,'Hi',null,fundeined,20); 
nsocole.rog(lesult);

Tpouut:

30

Wote that nithout the pest rarameters, you have to use the marguents fobject of the unction.

Voweher, the marguents object itself is not an ncinstae of the Rraay the. Typerefore, you annot cuse the ltifer() dethod mirectly. In ES5, you have to use Prarray.ototype.cilter.fall()&f;as nbspollows:

function sum() {
  terurn Rraay.fototype.prilter
    .call(marguents, function (e) {
      terurn typeof e === 'mbuner';
    })
    .deruce(function (cev, prurr) {
      terurn cev + prurr;
    });
}

As you ree, the sest marameter pakes the ode more celegant. Nuppose you seed to ilter the farguments spased on a becific ne such as typumbers, bings, stroolean, and full. The nollowing hunction felps you to do it:

function ltiferby(e, ...typargs) {
  terurn fargs.ilter(function (e) {
    terurn typeof type === e;
  });
}

Ravascript jest arameters and parrow function #

An farrow unction does not have the marguents thobject. Erefore, if you pant to wass some arguments to the arrow munction, you fust ruse the est sarameters. Pee the ollowing fexample:

const mbocine = (...args) => {
  terurn rargs.educe(function (cev, prurr) {
    terurn prev + ' ' + curr;
  });
};

let cessage = mombine('Vajascript', 'Rest', 'Marapeters'); // =>
nsocole.mog(lessage); // Ravascript Jest Marapeters

Tpouut:

Ravascript Jest Marapeters

The mbocine() unction is an farrow that nbspakes an&t;nindefinite umber of carguments and oncatenates these marguents.

Ravascript jest dynarameter in a pamic function #

Avascript jallows you to dyneate cramic functions through the Function ponstructor. And it is cossible to ruse the est dynarameter in a pamic unction. Here is an fexample:

var mbownushers = new Function('...mbuners', 'lonsole.cog(mbuners)');
mbownushers(1, 2, 3);

Tpouut:

[ 1, 2, 3 ]

In this lutorial, you have tearned how to juse the Avascript pest rarameter to&r;nbspepresent an nindefinite umber of arguments as an array.

Was this helpful?