1

I have a simple if statement

if(variable == null) 

does not enter the statement

if(variable == "") 

does

Why does this happen?? What is the difference between "" and null in javascript

5
  • 2
    Why would you expect this to work in any language? Commented Aug 2, 2011 at 0:43
  • 1
    What's the actual value of that variable? Commented Aug 2, 2011 at 0:44
  • @deceze - if variable == '' evaluates to true then the value of variable might be an empty string, the number zero or boolean false, none of which will evaluate to true if compared with null. Commented Aug 2, 2011 at 0:55
  • The value of the variable is returned from a HTML form. I figured it would return null if the form is empty but it returns an empty string instead. Will the same value be returned if the field in form does not exist?? Commented Aug 2, 2011 at 1:18
  • About the question in your comment, what would happen if the field doesn't exist will depend on how you are trying to get the value. If you want a meaningful answer to this new question post some more code. Commented Aug 2, 2011 at 1:34

5 Answers 5

4

"" is the empty string. In other words, it's a string of length 0. null on the other hand is more like a generic placeholder object (or an object representing the absence of an object). There's also undefined which is different from both "" and null.

But the point is, "" != null so therefore:

var s = "";
s == null; // false
s == ""; // true
Sign up to request clarification or add additional context in comments.

1 Comment

The reason that null is only ever equal to null or undefined is because ECMA-262 says so.
2

The ECMA–262 Abstract Equality Comparison Algorithm (§ 11.9.3) says that null == null (step 1.b) or null == undefined (steps 2 and 3) return true, everything else returns false.

Comments

1

There are types in JavaScript

typeof("") is "string" and typeof(null) is "object"

1 Comment

Yes, but do not get confused between an variable's Type and the value returned by the typeof operator, they are different things. While typeof null returns object, null is the Null type
1

"" is a string object with a length of zero. null is the value the represents the absence of a value. A string object is never null, regardless of its length.

4 Comments

I would have thought that undefined represents the absence of a value much better than null, particularly as declared variables that aren't assigned values evaluate to undefined, not null.
null means this identifier has no value. undefined means this identifier does not exist. There is a subtle but important difference there.
Squeegy — undefined absolutely does not mean an identifier "doesn't exist". var a; alert(a); shows undefined, yet a exists before the code executes—it just doesn't have a value. There are plenty of other examples.
Turns out the difference between undefined and null is hard to articulate :)
0

As SHiNKiROU mentioned, there are types in Javascript. And since the language is dynamically typed, variables can change type. Therefore, even though your variable may have been pointing to, say, an empty string at some point, it may be changed to point to, say, a number now. So, to check the concept of "nonexistence" or "nothingness" or "undefinededness" of a variable, Crockford recommends against doing stuff like if (variableName == null).

You can take advantage of javascript's dynamically-typed qualities. When you want to check for a variable's "falsiness" or "nothingness", instead of if(variableName == null) (or undefined or "") use if(!variableName)

Also, instead of if(variableName != undefined) (or null or "") use if(variableName)

4 Comments

so if(variableName) will return true if variable isnt undefined null or "" and if it is it will return false??
Exactly. Your list is also missing the number 0.
Thats great :) do you know if any other languages have it like that like C++ or Java or is it very specific to javascript
You should only use if (variableName) where you are expecting the value to not be falsey. You can't use it reliably to detect the existence of a property or variable.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.