53

Other than creating a function, is there a shorter way to check if a value is undefined,null or false only in JavaScript?

The below if statement is equivalent to if(val===null && val===undefined val===false) The code works fine, I'm looking for a shorter equivalent.

if(val==null || val===false){
  ;
}

Above val==null evaluates to true both when val=undefined or val=null.

I was thinking maybe using bitwise operators, or some other trickery.

pb2q
  • 58,613
  • 19
  • 146
  • 147
Lime
  • 13,400
  • 11
  • 56
  • 88

9 Answers9

40

Well, you can always "give up" :)

function b(val){
    return (val==null || val===false);
}
hugomg
  • 68,213
  • 24
  • 160
  • 246
  • 3
    Well I piratically gave up, but did end up changing `false` to `!1` – Lime Jul 28 '11 at 02:33
  • 1
    I think we can change val === false to val == false :) – Konza Nov 28 '13 at 13:30
  • @Konza: [That just how `==` is defined](http://stackoverflow.com/questions/359494/does-it-matter-which-equals-operator-vs-i-use-in-javascript-comparisons#359509). Its fairly complicated and its why many people recommend using `===` unless you really know what you are doing. – hugomg Nov 29 '13 at 06:54
  • doesnot work better can be used as if(typeof variable_here === 'undefined'){ // your code here. }; – Sam Jun 25 '14 at 06:52
  • It's crazy but for me it doesn't worw with `null` but with `'null'` !! And also not work with `val == ''` (tested both in firefox console and live....if you have a clue ? – TOPKAT May 25 '16 at 21:56
  • thats strange. you might want to create a separate question thread for that. – hugomg May 25 '16 at 21:59
  • piratically - adv. do do something as pirate would... – JJS Feb 12 '21 at 20:28
19

I think what you're looking for is !!val==false which can be turned to !val (even shorter):

You see:

function checkValue(value) {
    console.log(!!value);
}

checkValue(); // false
checkValue(null); // false
checkValue(undefined); // false
checkValue(false); // false
checkValue(""); // false

checkValue(true); // true
checkValue({}); // true
checkValue("any string"); // true

That works by flipping the value by using the ! operator.

If you flip null once for example like so :

console.log(!null) // that would output --> true

If you flip it twice like so :

console.log(!!null) // that would output --> false

Same with undefined or false.

Your code:

if(val==null || val===false){
  ;
}

would then become:

if(!val) {
  ;
}

That would work for all cases even when there's a string but it's length is zero. Now if you want it to also work for the number 0 (which would become false if it was double flipped) then your if would become:

if(!val && val !== 0) {
  // code runs only when val == null, undefined, false, or empty string ""
}
SudoPlz
  • 20,996
  • 12
  • 82
  • 123
18

The best way to do it I think is:

if(val != true){
//do something
} 

This will be true if val is false, NaN, or undefined.

David Hoffman
  • 189
  • 1
  • 2
2

One way to do it is like that:

var acceptable = {"undefined": 1, "boolean": 1, "object": 1};

if(!val && acceptable[typeof val]){
  // ...
}

I think it minimizes the number of operations given your restrictions making the check fast.

Eugene Lazutkin
  • 43,776
  • 8
  • 49
  • 56
2

Another solution:

Based on the document, Boolean object will return true if the value is not 0, undefined, null, etc. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean

If value is omitted or is 0, -0, null, false, NaN, undefined, or the empty string (""), the object has an initial value of false.

So

if(Boolean(val))
{
//executable...
}
Yiding
  • 2,914
  • 3
  • 21
  • 16
1

Boolean(val) === false. This worked for me to check if value was falsely.

Brigette
  • 57
  • 1
  • 7
1

only shortcut for something like this that I know of is

var val;
(val==null || val===false) ? false: true;
Joseph Marikle
  • 76,418
  • 17
  • 112
  • 129
  • @missingno can't see why not... it's shorter than writing out `if ... else ...` and in any case, how shorter can you get a conditional statement like this? ... better answer than "just give up" imo >.> – Joseph Marikle Jul 26 '11 at 22:07
  • All your conditional does is convert things to a boolean. However, this can be done much succintely with `!!` or, in this case, by doing nothing at all as the consition already is a boolean. – hugomg Jul 26 '11 at 22:11
  • @missingno no no no.... any code you want to execute based on the condition would go *in place of* the `false` and `true`... those are just there for clarity sake as to what results in what – Joseph Marikle Jul 26 '11 at 22:12
  • Some people consider this as bad style. The ternary operator should be used to return a value, not for it's side effects. – Felix Kling Jul 26 '11 at 22:17
  • I would add functions where the `false` and `true` are then. Now it just appears as being over engineered. – Lime Jul 26 '11 at 22:23
0

Using ? is much cleaner.

var ? function_if_exists() : function_if_doesnt_exist();
Pedro Pereira
  • 480
  • 5
  • 12
-1

Try like Below

var Boolify = require('node-boolify').Boolify;
if (!Boolify(val)) {
    //your instruction
}

Refer node-boolify

Ratan Uday Kumar
  • 5,738
  • 6
  • 35
  • 54