LearnThatStack Ace your next interview
JavaScript · question
Question 7 of 126

How do you check the type of a variable in JavaScript?

beginner
← All JavaScript questions
Re-explain

Use typeof for primitives and functions, Array.isArray() for arrays, and instanceof to check whether an object came from a given constructor or class.

typeof returns a string such as "number", "string" or "function". It has two traps. typeof null is "object", and so is typeof of any array, which is why arrays need Array.isArray(). To test for null itself, compare with === null.

instanceof walks the prototype chain, so it fails for objects created in another frame or window.

The most reliable general check is Object.prototype.toString.call(value), which returns labels such as "[object Date]" or "[object Null]".

typeof variable;           // Returns string representation
Array.isArray(variable);   // Check if array
variable instanceof Constructor; // Check instance type
Object.prototype.toString.call(variable); // Most reliable method
Rewriting in plainer words…

This answer doesn't lend itself to a diagram - it reads best . No credits were charged.

Why there's no diagram: “”

The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓

The diagram below the answer is the concept . Jump to it ↓

Tailored explanation · switch back to · ·
What should the new diagram focus on?
Guided practice for JavaScript One question at a time. Answer out loud, get graded, see what you missed.
How well did you know this?
AI:

Saved in this browser - sign in to keep your review list.

How should your speech become text?

Listening… your words appear above as you speak - tap Stop when you're done.

Recording · cr - tap Stop & transcribe when you're done.

Transcribing with AI…

Voice:

Keep going - a few more words and AI can grade it.

Interview lens How interviewers actually use this question

Likely follow-ups

  • Why does typeof null return "object", and why hasn't it been fixed? Call it a bug from the first JavaScript implementation. Fixing it now would break existing code that depends on it.
  • How does instanceof decide its answer, and why does it fail for an array made in an iframe? It looks for Constructor.prototype in the object's prototype chain. Each frame has its own Array constructor, so use Array.isArray.
  • How would you check that a value is a plain object and not null, an array or a Date? typeof returns "object" for all of them. Rule out null first, then compare Object.getPrototypeOf(value) with Object.prototype.
  • How do you check whether a value is NaN, and why doesn't typeof help? typeof NaN is "number". Use Number.isNaN, and say that the global isNaN converts its argument to a number before it checks.
  • What does typeof return for a variable that was never declared? It returns "undefined" and does not throw. Reading a let or const with typeof before its declaration still throws a ReferenceError.

What you can say

  1. No single check covers every type, so I pick one by what I'm testing: typeof for primitives and functions, Array.isArray for arrays, instanceof for class instances.
  2. typeof returns a string like "number" or "function", but typeof null gives "object", and typeof of an array gives "object" too.
  3. So for arrays I use Array.isArray, and to test for null I compare with === null.
  4. instanceof walks the prototype chain to tell me whether an object came from a given constructor or class.
  5. When I need one general check, Object.prototype.toString.call(value) is the most reliable, and it returns labels like [object Date] or [object Null].
  6. The one I'm careful with is instanceof, because it fails for objects created in another frame or window, where each frame has its own constructors.

Weak answers to avoid

  • Says typeof is enough for everything typeof null and typeof of an array both return "object", so that code gets them wrong. Name Array.isArray for arrays and === null for null.
  • Uses instanceof Array as the array check It fails for arrays created in another frame or window. Array.isArray gives the right answer across frames, so use that for arrays.
  • Tests for null with a falsy check if (!value) also catches 0, "", false and undefined. Compare with === null when you mean null and nothing else.
  • Lists the operators without saying when each fits The interviewer wants to know which check you'd use for which value and where each one breaks, so tie every operator to its trap.
Interview lens

Likely follow-ups, what you can say, and the weak answers to avoid.

Sign in free to open it Free account - the lens opens as soon as you're back.

Want a quick review of the fundamentals? See the JavaScript cheatsheet.

← Back to all JavaScript questions
Pro · $10/mo

110 of 126 JavaScript answers are in Pro.

Full answers, code samples, and AI explanations that go simpler or deeper. Cancel anytime.

  • Full answers + code
  • AI explanations, simpler or deeper
  • 1,000 AI credits / month
  • Cancel anytime