function delay(n){
n = n || 2000
//other stuff
}
what does the first line do? Thanks.
function delay(n){
n = n || 2000
//other stuff
}
what does the first line do? Thanks.
It means that if n
, the argument passed to the function, is falsey, 2000 will be assigned to it.
Here, it's probably to allow callers to have the option of either passing an argument, or to not pass any at all and use 2000 as a default:
function delay(n){
n = n || 2000
console.log(n);
}
delay();
delay(500);
But it would be more suitable to use default parameter assignments in this case.
function delay(n = 2000){
console.log(n);
}
delay();
delay(500);