Skip to content

JavaScript Tutorial Variables and Values

JavaScript typeof and Dynamic Typing

Lesson 18 of 258 2 min read By Deepak Paswan Updated Oct 4, 2026

Dynamic typing

In JavaScript, the value has a type, not the variable. A variable can hold a number now and text later. That is called dynamic typing.

script.js
JS
let thing = 42;
console.log(typeof thing); // → number

thing = "now I am text";
console.log(typeof thing); // → string

thing = true;
console.log(typeof thing); // → boolean

This is flexible, but it also means mistakes show up when the code runs rather than before. Careful names and checks help, and tools such as TypeScript add type checking on top of JavaScript if you need it later.

typeof

typeof returns the type of a value as a string.

script.js
JS
console.log(typeof "hi");        // → string
console.log(typeof 5);           // → number
console.log(typeof 5n);          // → bigint
console.log(typeof true);        // → boolean
console.log(typeof undefined);   // → undefined
console.log(typeof Symbol());    // → symbol
console.log(typeof {});          // → object
console.log(typeof [1, 2]);      // → object
console.log(typeof function () {}); // → function
console.log(typeof null);        // → object

The quirks

  • typeof null is "object" (a historical bug).
  • typeof [] is "object". To check for an array, use Array.isArray(value).
  • typeof NaN is "number". NaN means “not a number” but it still belongs to the number type.
  • A function has its own result, "function", even though functions are objects.
script.js
JS
console.log(Array.isArray([1, 2])); // → true
console.log(Array.isArray({}));     // → false
console.log(typeof NaN);            // → number

typeof never fails on missing variables

script.js
JS
console.log(typeof notDeclaredAnywhere); // → undefined

It returns "undefined" instead of throwing, which makes it handy for checking whether a global exists.

lightbulb
Type conversion
JavaScript also converts types automatically in some situations, such as "5" + 1. This can be surprising. Arithmetic Operators and Strict vs Loose Equality show where it matters.