3

I found the following legacy Javascript code:

function doSomething(parameter) {
    if (parameter != null && parameter != undefined && parameter != '') {
        ...
    }
}

I wonder if it is equal to check

if(parameter) {
    ...
}

i.e. if it is equal to check if the parameter is truthy. Is there a case when this is not equal?

4
  • Type undefined === true into a browser console Commented Dec 13, 2013 at 8:21
  • @RGraham undefined is falsy, what shall your comment say to my question? Am i missing something? Commented Dec 13, 2013 at 8:26
  • 2
    these statements are not equivalent for 0, NaN and false. Commented Dec 13, 2013 at 8:32
  • 1
    @thg435 only for NaN. false and 0 fall in parameter != '' (see my answer) Commented Dec 13, 2013 at 8:40

4 Answers 4

4

No is not the same. In case of parameter = NaN your IF is true while if(paramater) is evalueted to false.

In fact:

var parameter = NaN;
if (parameter != null && parameter != undefined && parameter != '') {
    console.log("first if");
}
if(parameter) 
     console.log("second if");

just log first if.

In the case of 0 or false the two IF make the same result caused by not strict equality of parameter != ''.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the hint to NaN, now I can martyr myself by deciding whether the orginal coder really intended that or not. ;)
3

Check the link about Converting to Boolean part

http://jibbering.com/faq/notes/type-conversion/

Only NaN, 0, false, null,undefined, '' will be converted to false.

Comments

1

In javascript there are few values are converted as boolean false else almost all are converted to true

undefined
The empty string ""
null
The number 0
The number NaN
The boolean false

so it better to have

if(!parameter)
{
  // your code
}

2 Comments

You are right that one often needs !parameter, but in this case with empty string however, you are not right (!'' === true). I don't understand why this answer is upvoted in the context of this question.
@jan when you use strict equal to then compares type, i mentioned that comparision result is implicitly converted to boolean false, not the values used to compare.
0

It's all the same as you're using loose comparison != not strict !== so undefined == null, and '' == 0, false == '', etc.

Edit: As pointed out, they're not exactly the same. As a general rule though you'd use either:

if (parameter != null) {

}

Where undefined == null so you check for both. Or:

if (parameter) {

}

Which tests any falsey value such as null, undefined, empty string, zero...

2 Comments

So is this a "yes" and there is no case where the checks don't equal?
There are specific rules. But as a general rule, you usually check in one of the ways I posted above, plus a third way (irrelevant to the question) that tests for existence, with the typeof operator. It depends on the situation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.