You are not logged in. Your edit will be placed in a queue until it is peer reviewed.
We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.
Required fields*
- 29the first code-piece can be incorrect if x is being set from a function call. like x = A(); if A doesnt return anything, it will return "undefined" by default. Doing a !x would be true which would be logically correct. However, if A() returns 0 then !x should be false as x=0. However in JS, !0 is also true.Rajat– Rajat2009-12-30 00:49:07 +00:00Commented Dec 30, 2009 at 0:49
- the second code can be shortened to: if(!typeof(XX)){ ... }else{ ... }Alejandro Silva– Alejandro Silva2014-06-06 21:53:42 +00:00Commented Jun 6, 2014 at 21:53
- 2@AlejandroSilva Sorry for late reply. That won't work since typeof returns a string, so it will return 'undefined' for an undefined variable, which in turn will evaluate as TRUE therefore leading to a false positive of a defined var.Dmitri Farkov– Dmitri Farkov2015-03-17 20:29:28 +00:00Commented Mar 17, 2015 at 20:29
- 5Please get rid of the first snippet, it's just badRuan Mendes– Ruan Mendes2016-02-12 12:06:26 +00:00Commented Feb 12, 2016 at 12:06
- 2Other comments have pointed out that the first example is bad, but not clearly why. So, for any new coders: !x doesn't test whether x is defined, but whether it's truthy. Strings, boolean true, and positive numbers are all truthy (and I might be forgetting some things), but other potentially valid values like 0, boolean false, and an empty string are not truthy. The first example can work for specific use cases (e.g., testing for a string if you can treat empty the same as undefined), but because of the many where it won't, it should not be considered the default way to check.cfc– cfc2018-11-14 16:50:32 +00:00Commented Nov 14, 2018 at 16:50
| Show 4 more comments
How to Edit
- Correct minor typos or mistakes
- Clarify meaning without changing it
- Add related resources or links
- Always respect the author’s intent
- Don’t use edits to reply to the author
How to Format
- create code fences with backticks ` or tildes ~ ```
like so
``` - add language identifier to highlight code ```python
def function(foo):
print(foo)
``` - put returns between paragraphs
- for linebreak add 2 spaces at end
- _italic_ or **bold**
- indent code by 4 spaces
- backtick escapes
`like _so_` - quote by placing > at start of line
- to make links (use https whenever possible) <https://example.com>[example](https://example.com)<a href="https://example.com">example</a>
How to Tag
A tag is a keyword or label that categorizes your question with other, similar questions. Choose one or more (up to 5) tags that will help answerers to find and interpret your question.
- complete the sentence: my question is about...
- use tags that describe things or concepts that are essential, not incidental to your question
- favor using existing popular tags
- read the descriptions that appear below the tag
If your question is primarily about a topic for which you can't find a tag:
- combine multiple words into single-words with hyphens (e.g. python-3.x), up to a maximum of 35 characters
- creating new tags is a privilege; if you can't yet create a tag you need, then post this question without it, then ask the community to create it for you
lang-js