In if statement,undefined equals with false
I'm confused with the code below:
if(undefined){
//code will not be executed
}
and
if(!undefined){
//code will be executed
}
Is that mean the "undefined" equals with false?
Here the question related,but no one point above situation out.
javascript if-statement undefined
add a comment |
I'm confused with the code below:
if(undefined){
//code will not be executed
}
and
if(!undefined){
//code will be executed
}
Is that mean the "undefined" equals with false?
Here the question related,but no one point above situation out.
javascript if-statement undefined
3
The truthy value ofundefined
in Javascript isfalse
. Hence the behaviour that you see.
– stackErr
May 11 '16 at 15:42
1
Boolean(undefined)
isfalse
.but your comments should be interchanged
– Madhawa Priyashantha
May 11 '16 at 15:43
@FastSnail Is that means if statement will switch every parameter passed to it inside?I mean,just like this:` if(Boolean(parameter)){//code}`,right?
– Xheldon Cao
May 12 '16 at 2:25
@XheldonCao if you pass a none Boolean value then yes.javascript useBoolean()
function to get a boolean value because if condition need a boolean value
– Madhawa Priyashantha
May 12 '16 at 3:18
@FastSnail Now I know,thanks guys!
– Xheldon Cao
May 12 '16 at 3:44
add a comment |
I'm confused with the code below:
if(undefined){
//code will not be executed
}
and
if(!undefined){
//code will be executed
}
Is that mean the "undefined" equals with false?
Here the question related,but no one point above situation out.
javascript if-statement undefined
I'm confused with the code below:
if(undefined){
//code will not be executed
}
and
if(!undefined){
//code will be executed
}
Is that mean the "undefined" equals with false?
Here the question related,but no one point above situation out.
javascript if-statement undefined
javascript if-statement undefined
edited May 23 '17 at 10:31
Community♦
11
11
asked May 11 '16 at 15:40
Xheldon Cao
779
779
3
The truthy value ofundefined
in Javascript isfalse
. Hence the behaviour that you see.
– stackErr
May 11 '16 at 15:42
1
Boolean(undefined)
isfalse
.but your comments should be interchanged
– Madhawa Priyashantha
May 11 '16 at 15:43
@FastSnail Is that means if statement will switch every parameter passed to it inside?I mean,just like this:` if(Boolean(parameter)){//code}`,right?
– Xheldon Cao
May 12 '16 at 2:25
@XheldonCao if you pass a none Boolean value then yes.javascript useBoolean()
function to get a boolean value because if condition need a boolean value
– Madhawa Priyashantha
May 12 '16 at 3:18
@FastSnail Now I know,thanks guys!
– Xheldon Cao
May 12 '16 at 3:44
add a comment |
3
The truthy value ofundefined
in Javascript isfalse
. Hence the behaviour that you see.
– stackErr
May 11 '16 at 15:42
1
Boolean(undefined)
isfalse
.but your comments should be interchanged
– Madhawa Priyashantha
May 11 '16 at 15:43
@FastSnail Is that means if statement will switch every parameter passed to it inside?I mean,just like this:` if(Boolean(parameter)){//code}`,right?
– Xheldon Cao
May 12 '16 at 2:25
@XheldonCao if you pass a none Boolean value then yes.javascript useBoolean()
function to get a boolean value because if condition need a boolean value
– Madhawa Priyashantha
May 12 '16 at 3:18
@FastSnail Now I know,thanks guys!
– Xheldon Cao
May 12 '16 at 3:44
3
3
The truthy value of
undefined
in Javascript is false
. Hence the behaviour that you see.– stackErr
May 11 '16 at 15:42
The truthy value of
undefined
in Javascript is false
. Hence the behaviour that you see.– stackErr
May 11 '16 at 15:42
1
1
Boolean(undefined)
is false
.but your comments should be interchanged– Madhawa Priyashantha
May 11 '16 at 15:43
Boolean(undefined)
is false
.but your comments should be interchanged– Madhawa Priyashantha
May 11 '16 at 15:43
@FastSnail Is that means if statement will switch every parameter passed to it inside?I mean,just like this:` if(Boolean(parameter)){//code}`,right?
– Xheldon Cao
May 12 '16 at 2:25
@FastSnail Is that means if statement will switch every parameter passed to it inside?I mean,just like this:` if(Boolean(parameter)){//code}`,right?
– Xheldon Cao
May 12 '16 at 2:25
@XheldonCao if you pass a none Boolean value then yes.javascript use
Boolean()
function to get a boolean value because if condition need a boolean value– Madhawa Priyashantha
May 12 '16 at 3:18
@XheldonCao if you pass a none Boolean value then yes.javascript use
Boolean()
function to get a boolean value because if condition need a boolean value– Madhawa Priyashantha
May 12 '16 at 3:18
@FastSnail Now I know,thanks guys!
– Xheldon Cao
May 12 '16 at 3:44
@FastSnail Now I know,thanks guys!
– Xheldon Cao
May 12 '16 at 3:44
add a comment |
3 Answers
3
active
oldest
votes
It means that undefined
is a falsy value, list of falsy values are:
"" # Empty string
null # null
undefined # undefined, which you get when doing: var a;
false # Boolean false
0 # Number 0
NaN # Not A Number eg: "a" * 2
If you negate a falsy value you will get true:
!"" === true
!null === true
!undefined === true
!0 === true
!NaN === true
And when you nagate a truly value you will get false:
!"hello" === false
!1 === false
But undefined
is not equal false
:
undefined === false // false
undefined == false // false
And just for the fun if it:
undefined == null // true
I will write it down my memorandum,thanks
– Xheldon Cao
May 12 '16 at 2:46
add a comment |
In javascript strict mode, undefined
is not false, but javascript try to convert the object or var to a boolean
value (this is called in javascript truthy value), that's the reason you got an undefined
as false. This happens with null also, for example.
You can force that with this strict no equality:
if(undefined!==false) console.log("Is not false");
add a comment |
Please take a look below checked falsy values:
""==false?
Ans: true
null == false?
Ans: false
undefined == false?
Ans: false
0 == false?
Ans: true
NaN == false?
Ans: false
null == NaN?
Ans: false
We can see that null == false
,undefined == false
,null == NaN
, and NaN == false
are not true
That means they are not equal. From the above result, we got 3 falsy values group:
- The False group
- The Null group and
- The NaN group
But a negative falsy value is always true:
!"" === true
!null === true
!undefined === true
!0 === true
!NaN === true
For example:
To check true
value of dataTitle
variable
if(dataTitle && (dataTitle != null))
{
console.log('hi');
}
The above statement will check the false group as well as the null group
To check false
value of dataTitle
variable
if(!dataTitle)
{
console.log('hi');
}
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f37167380%2fin-if-statement-undefined-equals-with-false%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
It means that undefined
is a falsy value, list of falsy values are:
"" # Empty string
null # null
undefined # undefined, which you get when doing: var a;
false # Boolean false
0 # Number 0
NaN # Not A Number eg: "a" * 2
If you negate a falsy value you will get true:
!"" === true
!null === true
!undefined === true
!0 === true
!NaN === true
And when you nagate a truly value you will get false:
!"hello" === false
!1 === false
But undefined
is not equal false
:
undefined === false // false
undefined == false // false
And just for the fun if it:
undefined == null // true
I will write it down my memorandum,thanks
– Xheldon Cao
May 12 '16 at 2:46
add a comment |
It means that undefined
is a falsy value, list of falsy values are:
"" # Empty string
null # null
undefined # undefined, which you get when doing: var a;
false # Boolean false
0 # Number 0
NaN # Not A Number eg: "a" * 2
If you negate a falsy value you will get true:
!"" === true
!null === true
!undefined === true
!0 === true
!NaN === true
And when you nagate a truly value you will get false:
!"hello" === false
!1 === false
But undefined
is not equal false
:
undefined === false // false
undefined == false // false
And just for the fun if it:
undefined == null // true
I will write it down my memorandum,thanks
– Xheldon Cao
May 12 '16 at 2:46
add a comment |
It means that undefined
is a falsy value, list of falsy values are:
"" # Empty string
null # null
undefined # undefined, which you get when doing: var a;
false # Boolean false
0 # Number 0
NaN # Not A Number eg: "a" * 2
If you negate a falsy value you will get true:
!"" === true
!null === true
!undefined === true
!0 === true
!NaN === true
And when you nagate a truly value you will get false:
!"hello" === false
!1 === false
But undefined
is not equal false
:
undefined === false // false
undefined == false // false
And just for the fun if it:
undefined == null // true
It means that undefined
is a falsy value, list of falsy values are:
"" # Empty string
null # null
undefined # undefined, which you get when doing: var a;
false # Boolean false
0 # Number 0
NaN # Not A Number eg: "a" * 2
If you negate a falsy value you will get true:
!"" === true
!null === true
!undefined === true
!0 === true
!NaN === true
And when you nagate a truly value you will get false:
!"hello" === false
!1 === false
But undefined
is not equal false
:
undefined === false // false
undefined == false // false
And just for the fun if it:
undefined == null // true
edited May 11 '16 at 16:05
answered May 11 '16 at 15:43
andlrc
35.5k126498
35.5k126498
I will write it down my memorandum,thanks
– Xheldon Cao
May 12 '16 at 2:46
add a comment |
I will write it down my memorandum,thanks
– Xheldon Cao
May 12 '16 at 2:46
I will write it down my memorandum,thanks
– Xheldon Cao
May 12 '16 at 2:46
I will write it down my memorandum,thanks
– Xheldon Cao
May 12 '16 at 2:46
add a comment |
In javascript strict mode, undefined
is not false, but javascript try to convert the object or var to a boolean
value (this is called in javascript truthy value), that's the reason you got an undefined
as false. This happens with null also, for example.
You can force that with this strict no equality:
if(undefined!==false) console.log("Is not false");
add a comment |
In javascript strict mode, undefined
is not false, but javascript try to convert the object or var to a boolean
value (this is called in javascript truthy value), that's the reason you got an undefined
as false. This happens with null also, for example.
You can force that with this strict no equality:
if(undefined!==false) console.log("Is not false");
add a comment |
In javascript strict mode, undefined
is not false, but javascript try to convert the object or var to a boolean
value (this is called in javascript truthy value), that's the reason you got an undefined
as false. This happens with null also, for example.
You can force that with this strict no equality:
if(undefined!==false) console.log("Is not false");
In javascript strict mode, undefined
is not false, but javascript try to convert the object or var to a boolean
value (this is called in javascript truthy value), that's the reason you got an undefined
as false. This happens with null also, for example.
You can force that with this strict no equality:
if(undefined!==false) console.log("Is not false");
answered May 11 '16 at 15:47
Christopher Díaz
31817
31817
add a comment |
add a comment |
Please take a look below checked falsy values:
""==false?
Ans: true
null == false?
Ans: false
undefined == false?
Ans: false
0 == false?
Ans: true
NaN == false?
Ans: false
null == NaN?
Ans: false
We can see that null == false
,undefined == false
,null == NaN
, and NaN == false
are not true
That means they are not equal. From the above result, we got 3 falsy values group:
- The False group
- The Null group and
- The NaN group
But a negative falsy value is always true:
!"" === true
!null === true
!undefined === true
!0 === true
!NaN === true
For example:
To check true
value of dataTitle
variable
if(dataTitle && (dataTitle != null))
{
console.log('hi');
}
The above statement will check the false group as well as the null group
To check false
value of dataTitle
variable
if(!dataTitle)
{
console.log('hi');
}
add a comment |
Please take a look below checked falsy values:
""==false?
Ans: true
null == false?
Ans: false
undefined == false?
Ans: false
0 == false?
Ans: true
NaN == false?
Ans: false
null == NaN?
Ans: false
We can see that null == false
,undefined == false
,null == NaN
, and NaN == false
are not true
That means they are not equal. From the above result, we got 3 falsy values group:
- The False group
- The Null group and
- The NaN group
But a negative falsy value is always true:
!"" === true
!null === true
!undefined === true
!0 === true
!NaN === true
For example:
To check true
value of dataTitle
variable
if(dataTitle && (dataTitle != null))
{
console.log('hi');
}
The above statement will check the false group as well as the null group
To check false
value of dataTitle
variable
if(!dataTitle)
{
console.log('hi');
}
add a comment |
Please take a look below checked falsy values:
""==false?
Ans: true
null == false?
Ans: false
undefined == false?
Ans: false
0 == false?
Ans: true
NaN == false?
Ans: false
null == NaN?
Ans: false
We can see that null == false
,undefined == false
,null == NaN
, and NaN == false
are not true
That means they are not equal. From the above result, we got 3 falsy values group:
- The False group
- The Null group and
- The NaN group
But a negative falsy value is always true:
!"" === true
!null === true
!undefined === true
!0 === true
!NaN === true
For example:
To check true
value of dataTitle
variable
if(dataTitle && (dataTitle != null))
{
console.log('hi');
}
The above statement will check the false group as well as the null group
To check false
value of dataTitle
variable
if(!dataTitle)
{
console.log('hi');
}
Please take a look below checked falsy values:
""==false?
Ans: true
null == false?
Ans: false
undefined == false?
Ans: false
0 == false?
Ans: true
NaN == false?
Ans: false
null == NaN?
Ans: false
We can see that null == false
,undefined == false
,null == NaN
, and NaN == false
are not true
That means they are not equal. From the above result, we got 3 falsy values group:
- The False group
- The Null group and
- The NaN group
But a negative falsy value is always true:
!"" === true
!null === true
!undefined === true
!0 === true
!NaN === true
For example:
To check true
value of dataTitle
variable
if(dataTitle && (dataTitle != null))
{
console.log('hi');
}
The above statement will check the false group as well as the null group
To check false
value of dataTitle
variable
if(!dataTitle)
{
console.log('hi');
}
edited Nov 20 at 10:11
answered Nov 20 at 9:04
Bablu Ahmed
940720
940720
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f37167380%2fin-if-statement-undefined-equals-with-false%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
3
The truthy value of
undefined
in Javascript isfalse
. Hence the behaviour that you see.– stackErr
May 11 '16 at 15:42
1
Boolean(undefined)
isfalse
.but your comments should be interchanged– Madhawa Priyashantha
May 11 '16 at 15:43
@FastSnail Is that means if statement will switch every parameter passed to it inside?I mean,just like this:` if(Boolean(parameter)){//code}`,right?
– Xheldon Cao
May 12 '16 at 2:25
@XheldonCao if you pass a none Boolean value then yes.javascript use
Boolean()
function to get a boolean value because if condition need a boolean value– Madhawa Priyashantha
May 12 '16 at 3:18
@FastSnail Now I know,thanks guys!
– Xheldon Cao
May 12 '16 at 3:44