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 thenif(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
2 Answers
Reset to default 3The 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
本文标签:
版权声明:本文标题:Check If statement is true or false by using javascript Truthy and Falsy Value VS '===' operator - Stack Overflo 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745295459a2652048.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论