Home > Blockchain >  How to pass only 2nd parameter's value while function has three parameters in js?
How to pass only 2nd parameter's value while function has three parameters in js?

Time:07-05

function range(start=0,end,step=1) {
}
console.log(range(10));

Here I want 10 as the "end" value. Default values of "start" and "step" are 0 and 1.

CodePudding user response:

function range({start=0, end, step=1}) {

}

console.log(range({step: 10}))

CodePudding user response:

A default value is taken for undefined (literally) value.

function range(start = 0, end, step = 1) {
    return [start, end, step];
}
console.log(range(undefined, 10));

CodePudding user response:

I found two ways to do it

function range(start=0,end, step=1) {}
console.log(range(10,null,1)) 

function range({start=0, end, step=1}) {}
console.log(range({}))
console.log(range({start: 10}))
console.log(range({start: 10,step:10})) 

the second one is easier to custom

  • Related