2

I understand that both empty string and null are falsy according to the ECMAScript. If both are falsy then why doesn't the following evaluate to true?

    var emptyString = '';
    if (emptyString == null) {
        console.log('emptyString == null');
    }
    else {
        console.log('emptyString does not == null'); // but why?
    }
8
  • 9
    Because an empty string is not the same as no string. Commented Mar 5, 2015 at 17:00
  • Because you're not comparing each to false(y), you're comparing an empty string to a null value. Commented Mar 5, 2015 at 17:01
  • 1
    Apples and oranges are both fruit, but apples == oranges would also evaluate false. Commented Mar 5, 2015 at 17:01
  • 4
    This question isn't that farfetched. The equality operator in JavaScript performs type coercion and is somewhat inconsistent. For example, '' == [] is true, but '' is a falsey value and [] is a truthy value. Also null == undefined is true, so there is at least one other case where null "equals" another falsey value. Commented Mar 5, 2015 at 17:07
  • 3
    Also just to prove why I am confused by all of this; try 0 == '' and you will see that it in fact evaluates to true. Commented Mar 5, 2015 at 17:21

2 Answers 2

5

both empty string and null are falsy

Yes, but that doesn't mean all falsy values would be equal to each other. NaN and 0 are both falsy as well, but they're definitely not equal. The reverse doesn't hold either, "0" == 0 but "0" ain't falsy.

The sloppy equivalence of values is defined by the Abstract Equality Algorithm and its type coercions, and null simply isn't == to anything but undefined.

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

Comments

3

The more commonly used abstract comparison (e.g. ==) converts the operands to the same Type before making the comparison.

Here, null is a falsy value, but null is not == false

The falsy values null and undefined are not equivalent to anything except themselves:

(null == false); // false
(null == null); // true
(undefined == undefined); // true
(undefined == null); // true

since the other operand is null( which is also a type in javascript ), the abstract comparison of empty string(falsy value) and null doesn't give a truthy value.

I think this will help you.

Comparison Operators

and this too

Truthy and Falsy: When All is Not Equal in JavaScript

1 Comment

I suppose this is starting to make sense now. In practice you wouldn't want anything to == or === null since null should be used to check against other values and types. Which is exactly why they have a truthy/falsy evaluations for other values and types. That provides a way for something like if (emptyString) which can be used to substitute the check for null if you do not know whether or not the value you will be receiving is an empty string or null.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.