Javascript Falsy Check execution order in an IF condition The 2019 Stack Overflow Developer Survey Results Are InJavaScript check if variable exists (is defined/initialized)How do JavaScript closures work?How do I check if an element is hidden in jQuery?How do I remove a property from a JavaScript object?How do I check if an array includes an object in JavaScript?Which equals operator (== vs ===) should be used in JavaScript comparisons?Setting “checked” for a checkbox with jQuery?How do I include a JavaScript file in another JavaScript file?What does “use strict” do in JavaScript, and what is the reasoning behind it?How to check whether a string contains a substring in JavaScript?How do I remove a particular element from an array in JavaScript?
Time travel alters history but people keep saying nothing's changed
Inversion Puzzle
What do the Banks children have against barley water?
Dual Citizen. Exited the US on Italian passport recently
I see my dog run
Are there any other methods to apply to solving simultaneous equations?
How come people say “Would of”?
Geography at the pixel level
Springs with some finite mass
What does "sndry explns" mean in one of the Hitchhiker's guide books?
Why isn't airport relocation done gradually?
Idiomatic way to prevent slicing?
What are the motivations for publishing new editions of an existing textbook, beyond new discoveries in a field?
Landlord wants to switch my lease to a "Land contract" to "get back at the city"
Is "plugging out" electronic devices an American expression?
aging parents with no investments
Why did Howard Stark use all the Vibranium they had on a prototype shield?
How to change the limits of integration
Why is Grand Jury testimony secret?
Why do I get badly formatted numerical results when I use StringForm?
Falsification in Math vs Science
What is the motivation for a law requiring 2 parties to consent for recording a conversation
Why can Shazam do this?
Pristine Bit Checking
Javascript Falsy Check execution order in an IF condition
The 2019 Stack Overflow Developer Survey Results Are InJavaScript check if variable exists (is defined/initialized)How do JavaScript closures work?How do I check if an element is hidden in jQuery?How do I remove a property from a JavaScript object?How do I check if an array includes an object in JavaScript?Which equals operator (== vs ===) should be used in JavaScript comparisons?Setting “checked” for a checkbox with jQuery?How do I include a JavaScript file in another JavaScript file?What does “use strict” do in JavaScript, and what is the reasoning behind it?How to check whether a string contains a substring in JavaScript?How do I remove a particular element from an array in JavaScript?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I have a program that calls a back-end service and collects a response. It is collected as follows. We destructure the response object and get the Error
and Data
.
const data, error = response;
If there is no error, the Error
object becomes null. Consider the following two options.
Option One:
if (!error)
// handle error
Option Two:
if (error !== null)
// handle error
In Option One, As per this answer, the following conditions are checked.
undefined: if the value is not defined and it's undefined
null: if it's null, for example, if a DOM element not exists...
empty string: ''
0: number zero
NaN: not a number- false
Is there an order of execution when we call Option 1? How does the internal condition checking happen in Option 1? Based on this, will there be a performance impact (in a granular level), if we use Option 1 instead of Option 2?
javascript
|
show 5 more comments
I have a program that calls a back-end service and collects a response. It is collected as follows. We destructure the response object and get the Error
and Data
.
const data, error = response;
If there is no error, the Error
object becomes null. Consider the following two options.
Option One:
if (!error)
// handle error
Option Two:
if (error !== null)
// handle error
In Option One, As per this answer, the following conditions are checked.
undefined: if the value is not defined and it's undefined
null: if it's null, for example, if a DOM element not exists...
empty string: ''
0: number zero
NaN: not a number- false
Is there an order of execution when we call Option 1? How does the internal condition checking happen in Option 1? Based on this, will there be a performance impact (in a granular level), if we use Option 1 instead of Option 2?
javascript
5
async vs speed of a micro optimization? really?
– Nina Scholz
Mar 8 at 8:53
1
Also I think you mean something else thanif (!error) // handle error
– mplungjan
Mar 8 at 8:56
3
This must be the most micro of premature optimisations I've ever seen.
– VLAZ
Mar 8 at 8:58
Interestingly the destructuring assignment is probably the slowest part in this example...
– Yoshi
Mar 8 at 9:02
I agree that it's a very micro level optimization. This came up in a Git Pull Request comment. Could I know the reason for downvotes?
– Keet Sugathadasa
Mar 8 at 9:04
|
show 5 more comments
I have a program that calls a back-end service and collects a response. It is collected as follows. We destructure the response object and get the Error
and Data
.
const data, error = response;
If there is no error, the Error
object becomes null. Consider the following two options.
Option One:
if (!error)
// handle error
Option Two:
if (error !== null)
// handle error
In Option One, As per this answer, the following conditions are checked.
undefined: if the value is not defined and it's undefined
null: if it's null, for example, if a DOM element not exists...
empty string: ''
0: number zero
NaN: not a number- false
Is there an order of execution when we call Option 1? How does the internal condition checking happen in Option 1? Based on this, will there be a performance impact (in a granular level), if we use Option 1 instead of Option 2?
javascript
I have a program that calls a back-end service and collects a response. It is collected as follows. We destructure the response object and get the Error
and Data
.
const data, error = response;
If there is no error, the Error
object becomes null. Consider the following two options.
Option One:
if (!error)
// handle error
Option Two:
if (error !== null)
// handle error
In Option One, As per this answer, the following conditions are checked.
undefined: if the value is not defined and it's undefined
null: if it's null, for example, if a DOM element not exists...
empty string: ''
0: number zero
NaN: not a number- false
Is there an order of execution when we call Option 1? How does the internal condition checking happen in Option 1? Based on this, will there be a performance impact (in a granular level), if we use Option 1 instead of Option 2?
javascript
javascript
asked Mar 8 at 8:51
Keet SugathadasaKeet Sugathadasa
725621
725621
5
async vs speed of a micro optimization? really?
– Nina Scholz
Mar 8 at 8:53
1
Also I think you mean something else thanif (!error) // handle error
– mplungjan
Mar 8 at 8:56
3
This must be the most micro of premature optimisations I've ever seen.
– VLAZ
Mar 8 at 8:58
Interestingly the destructuring assignment is probably the slowest part in this example...
– Yoshi
Mar 8 at 9:02
I agree that it's a very micro level optimization. This came up in a Git Pull Request comment. Could I know the reason for downvotes?
– Keet Sugathadasa
Mar 8 at 9:04
|
show 5 more comments
5
async vs speed of a micro optimization? really?
– Nina Scholz
Mar 8 at 8:53
1
Also I think you mean something else thanif (!error) // handle error
– mplungjan
Mar 8 at 8:56
3
This must be the most micro of premature optimisations I've ever seen.
– VLAZ
Mar 8 at 8:58
Interestingly the destructuring assignment is probably the slowest part in this example...
– Yoshi
Mar 8 at 9:02
I agree that it's a very micro level optimization. This came up in a Git Pull Request comment. Could I know the reason for downvotes?
– Keet Sugathadasa
Mar 8 at 9:04
5
5
async vs speed of a micro optimization? really?
– Nina Scholz
Mar 8 at 8:53
async vs speed of a micro optimization? really?
– Nina Scholz
Mar 8 at 8:53
1
1
Also I think you mean something else than
if (!error) // handle error
– mplungjan
Mar 8 at 8:56
Also I think you mean something else than
if (!error) // handle error
– mplungjan
Mar 8 at 8:56
3
3
This must be the most micro of premature optimisations I've ever seen.
– VLAZ
Mar 8 at 8:58
This must be the most micro of premature optimisations I've ever seen.
– VLAZ
Mar 8 at 8:58
Interestingly the destructuring assignment is probably the slowest part in this example...
– Yoshi
Mar 8 at 9:02
Interestingly the destructuring assignment is probably the slowest part in this example...
– Yoshi
Mar 8 at 9:02
I agree that it's a very micro level optimization. This came up in a Git Pull Request comment. Could I know the reason for downvotes?
– Keet Sugathadasa
Mar 8 at 9:04
I agree that it's a very micro level optimization. This came up in a Git Pull Request comment. Could I know the reason for downvotes?
– Keet Sugathadasa
Mar 8 at 9:04
|
show 5 more comments
1 Answer
1
active
oldest
votes
There is no order in this usage of if statement, JS just performs type conversion to coerce any value to boolean and then evaluates the result, check this link for more details: https://developer.mozilla.org/en-US/docs/Glossary/Falsy
Falsey results:
if (false)
if (null)
if (undefined)
if (0)
if (NaN)
if ('')
if ("")
if (``)
if (document.all)
// => false
Truthy results:
if (true)
if ()
if ([])
if (42)
if ("foo")
if new Date())
if (-42)
if (3.14)
if (-3.14)
if (Infinity)
if (-Infinity)
// => true
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%2f55059621%2fjavascript-falsy-check-execution-order-in-an-if-condition%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
There is no order in this usage of if statement, JS just performs type conversion to coerce any value to boolean and then evaluates the result, check this link for more details: https://developer.mozilla.org/en-US/docs/Glossary/Falsy
Falsey results:
if (false)
if (null)
if (undefined)
if (0)
if (NaN)
if ('')
if ("")
if (``)
if (document.all)
// => false
Truthy results:
if (true)
if ()
if ([])
if (42)
if ("foo")
if new Date())
if (-42)
if (3.14)
if (-3.14)
if (Infinity)
if (-Infinity)
// => true
add a comment |
There is no order in this usage of if statement, JS just performs type conversion to coerce any value to boolean and then evaluates the result, check this link for more details: https://developer.mozilla.org/en-US/docs/Glossary/Falsy
Falsey results:
if (false)
if (null)
if (undefined)
if (0)
if (NaN)
if ('')
if ("")
if (``)
if (document.all)
// => false
Truthy results:
if (true)
if ()
if ([])
if (42)
if ("foo")
if new Date())
if (-42)
if (3.14)
if (-3.14)
if (Infinity)
if (-Infinity)
// => true
add a comment |
There is no order in this usage of if statement, JS just performs type conversion to coerce any value to boolean and then evaluates the result, check this link for more details: https://developer.mozilla.org/en-US/docs/Glossary/Falsy
Falsey results:
if (false)
if (null)
if (undefined)
if (0)
if (NaN)
if ('')
if ("")
if (``)
if (document.all)
// => false
Truthy results:
if (true)
if ()
if ([])
if (42)
if ("foo")
if new Date())
if (-42)
if (3.14)
if (-3.14)
if (Infinity)
if (-Infinity)
// => true
There is no order in this usage of if statement, JS just performs type conversion to coerce any value to boolean and then evaluates the result, check this link for more details: https://developer.mozilla.org/en-US/docs/Glossary/Falsy
Falsey results:
if (false)
if (null)
if (undefined)
if (0)
if (NaN)
if ('')
if ("")
if (``)
if (document.all)
// => false
Truthy results:
if (true)
if ()
if ([])
if (42)
if ("foo")
if new Date())
if (-42)
if (3.14)
if (-3.14)
if (Infinity)
if (-Infinity)
// => true
edited Mar 8 at 9:04
answered Mar 8 at 8:58
Branislav PopadičBranislav Popadič
214
214
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.
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%2f55059621%2fjavascript-falsy-check-execution-order-in-an-if-condition%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
5
async vs speed of a micro optimization? really?
– Nina Scholz
Mar 8 at 8:53
1
Also I think you mean something else than
if (!error) // handle error
– mplungjan
Mar 8 at 8:56
3
This must be the most micro of premature optimisations I've ever seen.
– VLAZ
Mar 8 at 8:58
Interestingly the destructuring assignment is probably the slowest part in this example...
– Yoshi
Mar 8 at 9:02
I agree that it's a very micro level optimization. This came up in a Git Pull Request comment. Could I know the reason for downvotes?
– Keet Sugathadasa
Mar 8 at 9:04