4

I'm using the following syntax to ensure that my input parameters aren't null.

function hazaa(shazoo){
  shazoo = shazoo || " ";
}

It works for everything I tested for except the zero.

null -> " "
"beep" -> "beep"
4 -> 4
but...
0 -> " "

I'm guessing that the zero is regarded as null or false, hence creating the gotcha. What's the syntax to get it right, so that zero is zero?

If it makes the issue considerably simpler to suggest a syntax, we can assume that the input is going to be a char, string, number or null.

Konrad Viltersten
  • 36,151
  • 76
  • 250
  • 438

7 Answers7

7

I'm using the following syntax to ensure that my input parameters aren't null.

If all you are trying to do is to use " " only if the input is null, then use ternary operator, like this

shazoo = shazoo === null ? " " : shazoo;

This answer lists the values which are considered as Falsy in JavaScript. The table shows that zeroes are considered as Falsy. That is why shazoo || " " is evaluated to be " ", when shazoo is zero.

Community
  • 1
  • 1
thefourtheye
  • 233,700
  • 52
  • 457
  • 497
1

Boolean(0) outputs false, so it took second option ""

you can try (check this fiddle)

function hazaa(shazoo){
  return shazoo == null ? " " : shazoo;
} 
hazaa(0); //output 0
gurvinder372
  • 66,980
  • 10
  • 72
  • 94
  • *"`0 == false` outputs `true`"* while this is correct, that is not what happens here. **Comparing** a value to a Boolean is very different from **converting** a value to a Boolean (which is what `||` does). `0 == false` actually ends up being `0 == 0`. The correct explanation is hat `Boolean(0)` is `false`. This might be more convincing if you apply your explanation to `null`: `null == false` is `false` and yet `null || ""` returns `""`. `Boolean(null)` is `false` however. – Felix Kling Jan 06 '16 at 13:41
1

I'm guessing that the zero is regarded as null or false

Yes 0 is treated as false.

What's the syntax to get it right, so that zero is zero?

You can try to use

shazoo = shazoo === null ? " " : shazoo;
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
1

Use the following:

if (shazoo === null){
...
}else{
...
}
Andres
  • 10,561
  • 4
  • 45
  • 63
1

You can us === operator and the best way would be

if (shazoo === null)
 shazoo = " ";

No need to use else

shazoo = shazoo //no sense

Anand Singh
  • 2,343
  • 1
  • 22
  • 34
1

In my opinion, this is one of the few places where you don't want to do a typesafe comparison. In such places you want to threat undefined the same way as null; and you don't want to write it every time.

//so better use this
shazoo = shazoo == null ? " " : shazoo;

//than this
shazoo = shazoo === null || shazoo === undefined ? " " : shazoo;
Thomas
  • 3,513
  • 1
  • 13
  • 10
1

New notation

You can also in ES6 use the default argument notation that will default if undefined is passed for that argument.

function foo(shazoo = "default"){
    console.log(shazoo);
}

foo();          // "default"
foo(undefined); // "default"
var bar;        // 
foo(bar);       // "default"
foo("my string"); // "my string"
foo(null);      // null

It does not handle null

Blindman67
  • 51,134
  • 11
  • 73
  • 136
  • @KonradViltersten ES6 is ECMAScript6 is the latest version of JavaScript. Though there is still not full support on current browsers. Here is a rundown for ES6 support in Firefox https://developer.mozilla.org/en-US/docs/Web/JavaScript/New_in_JavaScript/ECMAScript_6_support_in_Mozilla – Blindman67 Jan 06 '16 at 20:29