Check Data Types with JavaScript typeof

02/12/2022
Contents
In this article, you will learn how to check data types with JavaScript typeof.
JavaScript data types
In JavaScript, values are categorized into some data type, and they behave according to each classification.
Below is a list of the main JavaScript data types.
- Boolean
- null
- undefined
- Number
- String
- Object
The typeof operator
In the JavaScript, the typeof operator is used to determine which type a value is classified into.
The syntax is below.
typeof value
Examples
//Boolean
typeof true //-> 'boolean'
//Number
typeof 123 //-> 'Number'
//Object
typeof {name : 'apple'} //-> 'object'
In the actual code, the type is checked by using the condition of the if statement.
Below is sample code to determine if the data type is numeric.
const num1 = 10; //Number
const num2 = '10'; //String
if (typeof num1 === 'number') {
console.log('The num1 is Number');
}
if (typeof num2 === 'string') {
console.log('The num2 is String');
}