3

Is it actually possible to get the Null data type as a return from the typeof function - if so what case yields that result, when is a variable actually of the Null type?

    typeof myVAR; //gives me "undefined" before the variable declaration

    var myVAR;
    typeof myVAR; //also gives me "undefined"

    myVAR = null; //assigned the null object
    typeof myVAR; //gives me "Object" (which I guess makes sense because `null` is an object and that's what I assigned to the variable)
3
  • 1
    possible duplicate of Why is null an object and what's the difference between null and undefined? Commented Oct 15, 2013 at 0:18
  • @PSL not quite, that explains what the null value is - but my question is geared more towards Null (the data type)? Commented Oct 15, 2013 at 1:46
  • Ok. I shall retract my close vote. Commented Oct 15, 2013 at 1:48

1 Answer 1

8

typeof never returns "null", but there is an internal null type:

Typeof Results:

  • Undefined: "undefined"
  • Null: "object"
  • Boolean: "boolean"
  • Number: "number"
  • String: "string"
  • Object (native and does not implement [[Call]]): "object"
  • Object (native or host and does implement [[Call]]): "function"
  • Object (host and does not implement [[Call]]): Implementation-defined except may not be "undefined", "boolean", "number", or "string".

The only way to test for null would be a direct comparison with the null value, using the === operator.

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

5 Comments

hmm..thank you, that answers part of my question, but can a variable ever be of Null data type?
@SuviVignarajah: A variable can have the null value, which is the only value of the Null data type.
I understand now - I did some research, seems like the Undefined data types "special" value is also undefined (similar to Null and null). You can declare a variable var myVAR = undefined, and typeof myVAR will yield undefined still. Seems like the typeof in javascript just doesn't return Null or null as one of the types like you state in your answer.
@SuviVignarajah: typeof doesn't return the actual name of the internal type - that may be slightly confusing.
Slightly..on another note, this explanation here gives me some closure though about why typeof null yields "object".

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.