2

I have search a lot and i got multiple way to check if statement is true or false. I found the standard function to check for null, undefined, or blank variables is to use truthy value like.

  if(value) { }

Is there a standard function to check for null, undefined, or blank variables in JavaScript?

I also found that the '===' operator is better to use over '==' operator.

Which equals operator (== vs ===) should be used in JavaScript comparisons?

I need the shorter and save way for doing this. Now i am confuse with these two solution. Do i need to follow the standard way to check the statement is true or false or i need to use the '===' operator.

Community
  • 1
  • 1
Muzafar Khan
  • 826
  • 3
  • 15
  • 28
  • 3
    Simple answer would be, _It depends what you want to compare_. If it is just testing `true`/`false` values then `if(value)` is good enough. – Rayon Dec 29 '15 at 05:48
  • 1
    You often see people use `==` in _JavaScript_ because they're used to the syntax from other languages. However, most people don't [_really know_ exactly what `==` does (even if they've read the spec)](http://es5.github.io/#x11.9.3), so I'd advise avoiding it; use `===` to compare and consider the _type_ of what you're trying to compare, e.g. a _String_ isn't a _Number_ so they are not equal – Paul S. Dec 29 '15 at 05:56
  • Possible duplicate of [Does it matter which equals operator (== vs ===) I use in JavaScript comparisons?](http://stackoverflow.com/questions/359494/does-it-matter-which-equals-operator-vs-i-use-in-javascript-comparisons) – Muzafar Khan Mar 04 '16 at 11:03

2 Answers2

3

The standard when checking if a value is null or undefined ("blank" in your terminology) is to use x == null. This is short for doing x === null || x === undefined.

You will find that doing x === null doesn't actually work for checking undefined, since

null == undefined // true
null === undefined // false

There is a difference between checking for a "truthy" value and checking for null or undefined. However, both null and undefined are "falsey" values, so if all you want to do is check if your variable exists and is "truthy", then if(x) is fine. Note that certain things you might expect (without experience) to be true/false are not. For example:

'' == true // false
0 == true // false

Then there are some values that aren't "truthy" or "falsey". For example:

NaN == true // false
NaN == false // false

Find a more complete list of weird stuff (and learn more about == vs ===) in this SO post.

<3 JavaScript

Community
  • 1
  • 1
Matthew Herbst
  • 29,477
  • 23
  • 85
  • 128
  • `NaN === NaN; // false` you can actually test for _NaN_ quite easily because of this by doing `x !== x`. Also note you get weird quirks with `==` like `1 == {toString: x => '1'}; // true` – Paul S. Dec 29 '15 at 06:22
  • Yeah. It's unclear if OP is more focused on `null`/`undefined`, `true`/`false`, or just `==`/`===`. If he cares more about `==`/`===` then for sure look at the question I linked to. – Matthew Herbst Dec 29 '15 at 06:25
3

use === for comparing the value as well as type.

use == for comparing by values only

// Example Program
var a = "0";
var b = 0;
console.log(a==b); // true
console.log(a===b); // false
Daksh Gupta
  • 7,554
  • 2
  • 25
  • 36
  • Do you have a source for the quoted part of your answer? I think it does a great job cutting to the core of this often-misunderstood issue. – Joshua Breeden Apr 18 '17 at 03:52