admin管理员组

文章数量:1418700

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 parisons?

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.

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 parisons?

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.

Share Improve this question edited May 23, 2017 at 12:25 CommunityBot 11 silver badge asked Dec 29, 2015 at 5:46 Muzafar KhanMuzafar Khan 8273 gold badges15 silver badges28 bronze badges 3
  • 3 Simple answer would be, It depends what you want to pare. If it is just testing true/false values then if(value) is good enough. – Rayon Commented Dec 29, 2015 at 5: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), so I'd advise avoiding it; use === to pare and consider the type of what you're trying to pare, e.g. a String isn't a Number so they are not equal – Paul S. Commented Dec 29, 2015 at 5:56
  • Possible duplicate of Does it matter which equals operator (== vs ===) I use in JavaScript parisons? – Muzafar Khan Commented Mar 4, 2016 at 11:03
Add a ment  | 

2 Answers 2

Reset to default 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 plete list of weird stuff (and learn more about == vs ===) in this SO post.

<3 JavaScript

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

use == for paring by values only

// Example Program
var a = "0";
var b = 0;
console.log(a==b); // true
console.log(a===b); // false

本文标签: