Challenge 1 +20 XP
Print the type of the value `null` using the `typeof` operator.
- Test 1 — expects "object\n"
Need a hint? (−25% XP)
Use console.log() and the typeof operator with null.
Show solution (0 XP)
console.log(typeof null); JavaScript has a number of built-in primitive data types. These values are immutable, meaning they cannot be altered once created.
All types except Object define immutable values
console.log(typeof 42);number
console.log(typeof "hello");string
console.log(typeof true);boolean
When a variable is declared without an explicit value, JavaScript automatically initializes it with the undefined primitive. This represents the default state of uninitialized data in the language.
let notAssigned;
console.log(typeof notAssigned);undefined
On the other hand, null is a distinct primitive that you must assign deliberately. It signifies the purposeful absence of an object or value.
console.log(null === undefined);false
typeof null BugThe typeof operator normally returns a string representing the primitive type of the given value. However, there is a very famous exception when checking null.
Read the code. What exactly will it print? Commit to an answer before you look.
console.log(typeof null);object
You might wonder why typeof null returns "object" if null is actually a primitive type. "null" would be the sensible answer. What you get instead is a leak from the first implementation, in 1995, that has been too widely depended on to fix.
In that original engine, JavaScript values were stored in 32-bit chunks, where the lowest 3 bits represented a type tag. The tag for objects happened to be 000. Meanwhile, null was implemented as a traditional C-style NULL pointer, which consists of all zeros. Because its lowest 3 bits were also 000, the engine mistakenly read it as an object and returned "object".
The first version of the language was built in about ten days, and this is one of the seams that shows. A proposal to fix this bug and make it return "null" was actually drafted for ECMAScript 6, but it was ultimately rejected. The web relies on backward compatibility, and fixing it would have broken millions of older websites that unknowingly relied on the bug. So typeof cannot tell you whether something is null. Compare directly instead: value === null.
undefined and null are exactly the same thing.
They are different types. undefined means a variable has been declared but not assigned a value. null is an assignment value representing no object. — They are different types. undefined means a variable has been declared but not assigned a value. null is an assignment value representing no object.
Because typeof null is 'object', null must be an object.
null is a primitive type. typeof null returning 'object' is a known, unfixable bug in the language for backward compatibility. — null is a primitive type. typeof null returning 'object' is a known, unfixable bug in the language for backward compatibility.
Everything in JavaScript is an object.
No, primitive values like strings, numbers, and booleans are not objects, although they have object wrappers. — No, primitive values like strings, numbers, and booleans are not objects, although they have object wrappers.
typeof is a function and requires parentheses.
typeof is an operator. You can write typeof 42 instead of typeof(42). — typeof is an operator. You can write typeof 42 instead of typeof(42).
Print the type of the value `null` using the `typeof` operator.
Use console.log() and the typeof operator with null.
console.log(typeof null); Declare a variable named `mystery` without giving it a value. Then print its type.
Use 'let' to declare without assignment, then console.log the typeof the variable.
let mystery;
console.log(typeof mystery);