brk
brk

Reputation: 50291

if(false) vs if(undefined) in conditional statement

I will like to know which will be more appropriate to use while writing a conditional statement in javascript

if(false){}

or

if(undefined){} 

Also I know if(undefined) will be converted to false is javascript

Background

I have a function & an object like this

var obj ={}
var someFunc = function(a){
     obj = {
         key1:true,
         key2:a, // i will like to have it as a?true:false
         key3: !a
    }

I am calling the function like this

someFunc(true);

if I call like this someFunc() , without passing the argument , then key2 will be undefined.

later I want to use this key in conditional statement

if(obj.key2){

}

As mentioned I know if obj.key2 is undefined, it will be coerced as if(false).

Upvotes: 2

Views: 3935

Answers (4)

kamprasad
kamprasad

Reputation: 648

It's depend on scenario that how do you check the value, in my opinion.

  • If you want to check any value exist You just need if(obj.key2)

  • If you want to check value does not exist, opposite of previous condition, just like if(!obj.key2)

  • If you want to check specific type, you would have used type check with === either if(obj.key2 === undefined) or if(obj['key2'] === undefined)

  • Else you can set default value as false. key2: a || false, Also ES6 has given nice feature for assigning default value into parameter. function(a = false){ }

  • Hope this will make your clear.

Upvotes: 0

Clomp
Clomp

Reputation: 3308

What you're asking about is known as "Object Detection". The code needs to be built in a way to detect if the object exists & if so, then operate on that object. The preferred way of making your code bullet-proof / bug free is to do this:

if (obj && obj.key2){ // If object and object property both exist...
  // Do something here...
}

To properly use the undefined type in JavaScript with the var keyword (in order to avoid the unexpected conditional flipping behavior described at this SO article: Behavior of the typeof keyword), you may want to prefix it with the "typeof" keyword. Then its output can be compared as a string, using the the triple equal "===" operator. Like so:

if (typeof value === 'undefined'){ 
  // Do something...
}

I tend to use these optional parenthesis, but they aren't required. It's just a coding style, which I like to read:

if (typeof(value) === 'undefined'){ 
  // Do something...
}

Then your code would look like this:

var obj = {};
var someFunc = function(a){
   obj = {
      "key1": true,
      "key2": a,
      "key3": !a
   };

   // True > False Example:
   if (obj && obj.key2) { // true
      // Do something true...
   } else { // false
      // Do something else...
   }

   // False > True Example, using a "not condition" AKA "!" AKA an exclamation point:
   if (obj && !obj.key3) { // false
      // Here you're checking for the object, but the false value for the key3 property.
   } else if (obj && obj.key3) { // true
      // Here you'd need the object detection again, as you'd be checking for the key3 property on that object.
   }
};

That's just an example, but you won't need the object detection, because you are declaring obj as a global & then assigning values to it in your function. Do you see why?

It's also better to put the var into the function, like so:

var obj = {
   "key1": true,
   "key2": a,
   "key3": !a
};

That removes it from the global scope & scopes it into the function block itself.

I'd also recommend learning about the "let" keyword & how it can replace "var".

Something awesome, would look like this:

let someFunc = function(json) {
   // Another object detection example:
   if (json) {

      // Since key1 is always true, we don't need the if/else for it.
      // Just add any key1 code here, without the if(...) statement.

      // Switches are faster than if/else-if/else statements.
      switch(json.key2) {
         case true:
            // Do something true with key2...
            break;
         case false:
            // Do something else with key2...
            break;
      }

      switch(json.key3) {
         case true:
            // Do something true with key3...
            break;
         case false:
            // Do something else with key3...
            break;
      }
   }
};

// Usage:
someFunc({
  "key1": true,
  "key2": a,
  "key3": !a
});

Upvotes: 1

Assafi Cohen-Arazi
Assafi Cohen-Arazi

Reputation: 847

Definitions of the 'false' and 'undefined':

If you give an if statement that compares false, then it is specifically looking for the boolean false. But, if a variable is undefined, it is automatically false anyway.

If an if statement looks for undefined, it is specifically looking for a variable that has never been defined or was assigned undefined as its value. An if statement if (!variable) will accept both false or undefined as the value of variable.

So what's the bottom line?

It makes no difference from false or undefined if and only if the if statement looks like this:

if (variable) {
// variable can be undefined or false
}

But, if the statement looks like it is below, it will only accept one of the two, depending on which it was matched with:

if (variable == false) {
// Now it strictly must be "false". Undefined is not accepted
}

Upvotes: 1

kamprasad
kamprasad

Reputation: 648

if you need to check specifically value type then you have to use following like this.

if(value === false){} 
or
if(value === undefined){}

other that using if(false) or if(undefined) are ok.

Upvotes: 1

Related Questions