Console Learner — An interactive JavaScript course.


1.3 Type Checking

JavaScript only checks types in run-time. Dynamic type checking uses one of two operators:

Although typeof is a unary operator, for readability it is recommended to write it like a function call instead.

That is, write typeof(value) instead of typeof value.
>>>
typeof(Object.notDefined)
"undefined"
>>>
typeof(null)
"object"
This is a common pitfall. Take note.
>>>
typeof(true)
"boolean"
>>>
typeof(42)
"number"
>>>
typeof("")
"string"
>>>
typeof({ key: 'value' })
"object"
>>>
typeof([1, 2, 3])
"object"
The typeof operator returns "object" for all object types.
>>>
[1, 2, 3] instanceof Array
true
>>>
[1, 2, 3] instanceof Object
true
All object type values are naturally also instances of Object
>>>
42 instanceof Number
false
Primitive types, like number, are not the same as their wrapper object types (e.g. Number).
>>>
typeof(function () {})
"function"
Functions are really object types, but the typeof operator isn't quite consistent with the type model...
>>>
(function () {}) instanceof Object
true
>>>

The console allows you to interact with the course material and examples. Use the following keys:

A special logging function is also available:


Need more magic? Check out the other developer tools on this site.