JavaScript Tutorial

JavaScript Typeof

typeof returns a string: "number", "string", "object". Arrays and null both say "object" — check them extra.

A string that names the kind

typeof value is an operator, not a function. It returns a string you can compare. Use it when you need to know whether a name holds a number, a string, a function, or something else before you call a method on it.

Parentheses are optional: typeof x and typeof(x) do the same job. The result is always a string, even when the value is undefined.

The usual answers

Valuetypeof
42 or NaN"number"
"hi""string"
true"boolean"
undefined"undefined"
a function"function"
an object, array, or null"object"
10n"bigint"
a symbol"symbol"

Example

console.log(typeof 7);
console.log(typeof "7");
console.log(typeof true);
console.log(typeof undefined);
console.log(typeof console.log);

Click Try it in JavaScript under an example. That opens/javascript/try — a live page and a console.

Arrays say object

An array is an object with a length and indexed slots. typeof [] is"object", not "array". Ask Array.isArray(value) when the difference matters.

Example

const list = [1, 2, 3];
const record = { n: 3 };

console.log(typeof list);
console.log(typeof record);
console.log(Array.isArray(list));
console.log(Array.isArray(record));

null says object

typeof null is "object". That is an old language bug that was never fixed, because fixing it would break existing pages. Treat null as its own check:value === null.

Example

const empty = null;
const box = {};

console.log(typeof empty);
console.log(typeof box);
console.log(empty === null);
console.log(box === null);

If you only test typeof x === "object", you will treat null as a usable object and then crash on x.name.

A safe object check

When you need “a real object I can read fields from,” combine the tests. Arrays may or may not count, depending on the job.

Example

function describe(value) {
  if (value === null) return "null";
  if (Array.isArray(value)) return "array";
  return typeof value;
}

console.log(describe(null));
console.log(describe([1, 2]));
console.log(describe({ a: 1 }));
console.log(describe("hi"));

typeof and missing names

typeof undeclaredName is "undefined" and does not throw. Reading the name without typeof throws ReferenceError. That is one reason people wrap a check in typeof before using a global that might not exist.

For a variable you declared, typeof still returns "undefined" if you have not assigned yet. That is normal, not an error.

What to remember

  • typeof returns a string such as "number" or "string".
  • Arrays and null both report "object".
  • Use Array.isArray and === null for those two cases.
  • Functions report "function", which is the useful exception.

Next: type conversion — turning a string into a number on purpose, and how + does it by accident.

FAQ: JavaScript Typeof

Common questions about this page.

What is the StudyGrid JavaScript tutorial?

The StudyGrid JavaScript tutorial is a full beginner track: variables, functions, arrays, if/else, the DOM, events, storage, and fetch. Each chapter has copy-and-run examples.

Should I run javascript typeof examples locally for better learning?

Yes. Use the browser editor on StudyGrid for a quick check, then Download the example and run it on your computer. Local runs show real errors and the real toolchain, which is one of the fastest ways to learn javascript typeof in this JavaScript JavaScript lesson (JavaScript Typeof).

Is the JavaScript editor the same as Try Python?

No. Try JavaScript is a live page plus a console at /javascript/try. Try Python stays at /try. JavaScript lessons never open the Python editor.

Do I need to install anything to learn JavaScript?

No. Open a chapter, click Try it in JavaScript, and the page and console update in the browser.

Where should I start the JavaScript tutorial?

Start at JavaScript Intro, then Where To, Output, and Syntax. After variables and functions, continue to the DOM and events. Use Next at the bottom of each chapter.

Is the JavaScript tutorial free?

Yes. The JavaScript studio on StudyGrid (studygrid.in) is free: dashboard, chapters, and the live editor.