3

I am facing a lot of issues while populating data in the DOM, because of there are no values in the objects or arrays, which I want to use.

For example, I have an object:

var obj = {
  name: 'rajat',
  age: 20
}

and I am populating data on HTML Elements Dynamically, Like below:

<p id='paragraph'></p>  

var data = document.getElementById('paragraph');
data.innerHTML = obj.name;

Now, I just want to add condition before assigning value to HTML element, I am confused by if(obj.a) and if(obj.a!=undefined && obj.a!=null).

Can anyone please explain the difference between the two?

Luca Kiebel
  • 9,790
  • 7
  • 29
  • 44
Rajat
  • 486
  • 1
  • 10
  • 35

4 Answers4

5

if(obj.a) will evaluate to true, if the value of obj.a is truthy, everything is truthy, but

  • false
  • 0
  • +0 and -0
  • "", '' and `` - strings of length 0
  • null
  • undefined
  • NaN

if(obj.a!=undefined && obj.a!=null) will evaluate to true even, if obj.a is

  • false
  • 0
  • +0 and -0
  • "", '' and `` - strings of length 0
  • NaN

So it's less exclusive, and obj.a can be more values without the if not being entered.

Luca Kiebel
  • 9,790
  • 7
  • 29
  • 44
4
if (obj.a)

tests to see whether obj.a is not any of null, undefined, NaN, 0, false, or "" (the empty string).

if (obj.a != undefined && obj.a != null)

just checks for null and undefined, and is exactly the same as

if (obj.a != null)

because that's how != is defined. In fact this is one of the few cases where using != is preferable to !==.

[edit] it's pointed out in a comment that because -0 exists in JavaScript (as it does in all IEEE floating point systems), that's also "falsey".

Pointy
  • 405,095
  • 59
  • 585
  • 614
  • This list is incomplete. The full list is [here](https://stackoverflow.com/a/4535656/4642212). – Sebastian Simon Aug 22 '18 at 17:17
  • @Xufox ok you got me with `-0` but `document.all`? That can't possibly be in the language spec. – Pointy Aug 22 '18 at 17:24
  • @Pointy MDN: *Provides access to all elements in the document. This is a legacy, non-standard property and should not be used.* – Luca Kiebel Aug 22 '18 at 17:28
  • 1
    @Luca sure, and it'll be `null` or `undefined`, but the language spec does not know about or care about browser APIs. In other words there's no difference as far as JavaScript is concerned between `document.all` and `document.zimbabwe` – Pointy Aug 22 '18 at 17:38
1

In Javascript, if evaluates the "truthiness" of the condition. The following values are "falsey":

  • false
  • undefined
  • null
  • ""
  • +/-0
  • NaN

Anything else is evaluated as true. So in your case, if(obj.a) will evaluate false if obj.a is any of the above bulleted values. However if(obj.a != undefined && obj.a != null) will only check for those two values (undefined and null) specifically, meaning that if the value is any of the other falsey items, it will still evaluate true.

This blog serves as a good resource for further explanation: https://javascriptweblog.wordpress.com/2011/02/07/truth-equality-and-javascript/

TFrazee
  • 792
  • 6
  • 20
  • 1
    Also `NaN` is "falsey". – Pointy Aug 22 '18 at 17:05
  • This is still incomplete. The full list is [here](https://stackoverflow.com/a/4535656/4642212). – Sebastian Simon Aug 22 '18 at 17:16
  • @Xufox While that list may be more explicit, I'd argue that `0` implies `+/-0`. I recognize that they're different, but both are 0. That said, I'll make that explicit in the post. The only other difference is `document.all`, which will actually evaluate `true` in old IE browsers (an unlikely scenario, but not impossible). I would consider it bad practice to assume `document.all` is falsey. – TFrazee Aug 22 '18 at 17:22
0

This has to do with JavaScript type-coercion which is a concept which determines how values are converted to different types. JavaScript is a loosely typed language (no strict types like int, string, ect.).

Therefore if the JS engine comes across an if statement and has convert the expression to a boolean.

For example:

// Boolean converts the value to a boolean
// The following values are converted to false, the rest is converted to true

console.log(Boolean(0));  // false
console.log(Boolean(''));  // false
console.log(Boolean(null));  // false
console.log(Boolean(undefined));  // false
console.log(Boolean(NaN));  // false
console.log(Boolean(false));  // false

// Bonus you can also convert to boolean really concise with the double !! operator 
// For example:

console.log(!!5);  // true
console.log(!!'random');  // true

Therefore the code:

if(a!=null && a!=undefined){}

is not the same as

 if(a){}

Because you miss the other types which will be converted to false. For example:

if(NaN){
  console.log('I wont log');
}
Luca Kiebel
  • 9,790
  • 7
  • 29
  • 44
Willem van der Veen
  • 33,665
  • 16
  • 190
  • 155