Open In App

JavaScript typeof Operator

Improve
Improve
Like Article
Like
Save
Share
Report

In JavaScript, typeof operator returns the data type of its operand in the form of a string. The operand can be any object, function, or variable. 

Syntax:

typeof operand
// OR
typeof (operand)

Primitive Data Types

Primitive data types in JavaScript are basic data types that represent single values. They include:

  1. Number: Represents numeric values like integers and floating-point numbers.
  2. String: Represents textual data enclosed within single quotes (”) or double quotes (“”).
  3. Boolean: Represents true or false values.
  4. Undefined: Represents a variable that has been declared but has not been assigned a value.
  5. Null: Represents the intentional absence of any object value.
  6. Symbol: Represents a unique and immutable data type used as the key of an object’s property.
  7. BigInt: Represents large integers beyond the limit of the Number type.

typeof Operator Examples

Example 1: Below example show the results of typeof operator for premitive data types.

Javascript




// Define variables with different primitive data types
const num = 10;
const str = "Hello";
const bool = true;
const undef = undefined;
const nul = null;
const sym = Symbol("symbol");
const bigInt = 9007199254740991n;
  
// Use typeof operator to determine the data type
console.log(typeof num);   // Output: "number"
console.log(typeof str);   // Output: "string"
console.log(typeof bool);  // Output: "boolean"
console.log(typeof undef); // Output: "undefined"
console.log(typeof nul);   // Output: "object" (typeof null is an oddity,
                           // it returns "object")
console.log(typeof sym);   // Output: "symbol"
console.log(typeof bigInt);// Output: "bigint"


Output

number
string
boolean
undefined
object
symbol
bigint

Example 2: This example uses ‘===’ (strict equality comparison operator) which compare value and type both and then return true or false.

javascript




//Number
console.log(typeof 25 === 'number');
console.log(typeof 3.14 === 'number');
console.log(typeof (69) === 'number');
  
// log base 10
console.log(typeof Math.LN10 === 'number');
console.log(typeof Infinity === 'number');
  
// Despite being "Not-A-Number"
console.log(typeof NaN === 'number');
  
// Wrapping in Number() function
console.log(typeof Number('100') === 'number');


Explanation: In the first console.log(), the js starts compiling from left to right and it first calculates the type of 25 which is ‘number’, and then compares it with ‘number’ and then finally returns true or false accordingly. 

Output

true
true
true
true
true
true
true

Example 3: This example compares the function datatype using the typeof operator.

javascript




// function
console.log(typeof function () { } === 'function');
  
//classes too are objects
console.log(typeof class C { } === 'function');
console.log(typeof Math.sin === 'function');


Output

true
true
true




Last Updated : 07 Mar, 2024
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads