Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Functions argument default value

Tags:

javascript

function foo( a, b ) {

  a = a || '123';
  b = b || 55;  
  document.write( a + ',' + b );
}

foo(); // prints: 123,55
foo('bar'); // prints: bar,55
foo('x', 'y'); // prints x,y

but:

foo(0,''); // prints: 123,55

why dont it print 0 ,55?

like image 631
Maizere Pathak.Nepal Avatar asked Jun 18 '26 15:06

Maizere Pathak.Nepal


1 Answers

Because || tests for truthiness, and 0 is among the values that are considered to be false.

like image 56
deceze Avatar answered Jun 21 '26 04:06

deceze