W3schools Typeof Operator Guide JavaScript TypeError Check

Logan Jul 13, 2026

Understanding the data type of a variable is fundamental in programming. JavaScript, in particular, is a dynamically typed language, which means you don't need to declare the type of a variable before assigning a value to it. However, sometimes you may need to check the type of a variable to write conditional statements or to manipulate the data accordingly.

W3Schools.com
W3Schools.com

The typeof operator in JavaScript comes in handy when you want to know the type of a variable. It prompts a string that represents the type of the unevaluated operand's type, returning values like "string", "number", "object", "undefined", "boolean", "symbol", and "function".

W3Schools.com
W3Schools.com

Basic Usage of typeof Operator

In its simplest form, the typeof operator is used with a single operand. It returns a string indicating the type of the operand. Here's a basic example:

learn html and css with w3schols, 3rd ed / workbook
learn html and css with w3schols, 3rd ed / workbook

let x = 10;
console.log(typeof x);
*This would output 'number'*

Checking for undefined

Types of school (education)
Types of school (education)

One of the standout features of typeof operator is its ability to detect the undefined type. When you use typeof with a variable that has not been assigned any value, it returns 'undefined'.

let y;
console.log(typeof y);
*This would output 'undefined'*

typeof and Objects

W3Schools.com
W3Schools.com

When used with an object, typeof returns 'object', which can sometimes be misleading. For instance, it doesn't differentiate between different types of objects.

let obj = { name: 'John', age: 30 };
console.log(typeof obj);
*This would output 'object'*

Types of Data typeof Operator Can't Detect

an english student's guide for the class
an english student's guide for the class

While typeof is quite powerful, it does have some limitations. On one hand, it can't detect the null type. Instead, it returns "object".

let z = null;
console.log(typeof z);
*This would output 'object'*

Typesy: The Online Typing Course for the ENTIRE Family
Typesy: The Online Typing Course for the ENTIRE Family
two women and a baby are looking at something in front of them with the words 3 must include types of elects for homeschool high school
two women and a baby are looking at something in front of them with the words 3 must include types of elects for homeschool high school
How to Get Started With Homeschooling
How to Get Started With Homeschooling
three different types of text are shown in this graphic diagram, and each one is labeled with
three different types of text are shown in this graphic diagram, and each one is labeled with
a person sitting at a desk with their feet up on the table and legs crossed
a person sitting at a desk with their feet up on the table and legs crossed
118K views · 630 reactions | On your demand Hand written grammar Notes in the queue Dm to buy ✅🤯
Life time access 
- #phonics
- #phonicsforkids
- #phonicsfun
- #phonicsactivities
- #phonicsgames
- #earlychildhoodedu | Punit Kumar Phonics Games, Phonics For Kids, Elementary Writing, Phonics Activities, Handwriting, Grammar, Phonics, Writing
118K views · 630 reactions | On your demand Hand written grammar Notes in the queue Dm to buy ✅🤯 Life time access - #phonics - #phonicsforkids - #phonicsfun - #phonicsactivities - #phonicsgames - #earlychildhoodedu | Punit Kumar Phonics Games, Phonics For Kids, Elementary Writing, Phonics Activities, Handwriting, Grammar, Phonics, Writing
First UK-wide hybrid school offering home and class learning to open in 2024
First UK-wide hybrid school offering home and class learning to open in 2024
the word school written in black on a white background
the word school written in black on a white background
Homeschooling Different Learning Styles
Homeschooling Different Learning Styles
three laptops with pictures on them and one is open to the web page, which shows
three laptops with pictures on them and one is open to the web page, which shows
Types Of Memory
Types Of Memory
the types of css in web design and development infographical diagram with caption
the types of css in web design and development infographical diagram with caption
an image of a computer screen with the text css chatsheet
an image of a computer screen with the text css chatsheet
Alternatives to GCSEs [For Homeschoolers and Others]
Alternatives to GCSEs [For Homeschoolers and Others]
HOMESCHOOL PROS AND CONS
HOMESCHOOL PROS AND CONS
4 Most Effective Learning Styles for Homeschooled Students
4 Most Effective Learning Styles for Homeschooled Students
Types of Alternative Education: Microschools, Co-ops, Hybrid Schools & Homeschooling
Types of Alternative Education: Microschools, Co-ops, Hybrid Schools & Homeschooling
Hybrid Schooling: Can You Homeschool and Go To Public School?
Hybrid Schooling: Can You Homeschool and Go To Public School?

typeof and Symbol

The typeof operator can't detect some exotic data types like Symbol. It Return "symbol" when used with a Symbol.

let sym = Symbol('foo');
console.log(typeof sym);
*This would output 'symbol'*

Understanding the typeof operator will help you write more efficient JavaScript code, ensuring you're working with the correct data types. Now, go ahead, check the types and make your coding adventures more secured!