1
function delay(n){
n = n || 2000
//other stuff
} 

what does the first line do? Thanks.

woxel
  • 157
  • 8
  • 3
    if n is 0, null or undefined. then n = 2000 – D. Seah Jan 03 '21 at 00:47
  • Also related: [this](https://stackoverflow.com/questions/894860/set-a-default-parameter-value-for-a-javascript-function) and [this](https://stackoverflow.com/questions/5409641/javascript-set-a-variable-if-undefined) – costaparas Jan 03 '21 at 00:52

1 Answers1

1

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);
CertainPerformance
  • 356,069
  • 52
  • 309
  • 320