Javascript Tutorial
Javascript Tutorial Introduction to Javascript JavaScript Code Editors & IDE JavaScript Hello World Example Javascript Syntax and Rules syntax_rules javascript_identifiers JavaScript Keywords & Reserved Words javascript_variables JavaScript Const JavaScript let vs var vs const Data Types in JavaScript JavaScript String Template Literals & String interpolation in JavaScript Tagged Templates in JavaScript String to Number in JavaScript Number Data Type in JavaScript NaN in JavaScript JavaScript Number Min & Max & Safe Values JavaScript EPSILON & Floating point precision Infinity in JavaScript JavaScript Bigint BigInt Vs Number in JavaScript Boolean Data Type in JavaScript Undefined in JavaScript Null in JavaScript Null vs Undefined in JavaScript JavaScript Operators Arithmetic Operators in JavaScript Unary plus & minus operators in JavaScript Increment & Decrement Operators in JavaScript Comparison or Relational operators in JavaScript Strict Equality (==) Loose Equality (===) in JavaScript Ternary Conditional Operator in JavaScript Logical Operators in JavaScript Bitwise Operators in JavaScript Assignment Operators in JavaScript Nullish Coalescing Operator in JavaScript Comma Operator in JavaScript Typeof JavaScript Operator Precedence in JavaScript JavaScript if, else & nested if statement Switch Statement in JavaScript While & Do While Loops in JavaScript For Loop in JavaScript Break statement in JavaScript Continue Statement in JavaScript Arrays in JavaScript Array Constructor in Javascript Sparse Array Vs Dense Array in JavaScript How to merge Arrays in JavaScript Array Methods in JavaScript Functions in JavaScript Function Parameters & Arguments in JavaScript JavaScript Default Parameters Pass by Value and Pass by Reference in Javascript Function Expression in Javascript Nested Functions in JavaScript Immediately-invoked Function Expressions (IIFE) JavaScript Callback Functions Arrow Functions in JavaScript Arguments Object In JavaScript Rest Parameters in JavaScript Objects in Javascript Create Objects in JavaScript JavaScript Object Properties Computed Property Names in JavaScript Object Literal in JavaScript Constructor Function & New Operator in JavaScript Delete Operator in JavaScript hasOwnProperty in JavaScript Using Getters and Setters in Javascript DefineProperty in JavaScript JavaScript Property Descriptors Enumerable, Writable & Configurable Object Destructuring in JavaScript Variable Scope in JavaScript Hoisting in JavaScript Lexical Scope & Closures in JavaScript This in JavaScript Global Object, Window & Globalthis in JavaScript Call function in Javascript Prototype In Javascript Prototype Inheritance in JavaScript Instanceof Operator in JavaScript Spread Operator in JavaScript

Typeof JavaScript

A typeof keyword returns a string indicating the Data Type of the identifier. We use it for getting the type of primitive values.

Using Typeof

The syntax of the Typeof is as shown below. It returns the data type of the identifier, which we place right next to it.

                            
typeof identifier
typeof (identifier)
 
                     
                            
                        

For Example

                            
var str="hello world"
console.log(typeof str)       //string
 
                     
                            
                        

Typeof correctly returns the type in case of number, string, boolean, symbol, undefined, function. Everything else is object. That also includes null.

                            
console.log(typeof 1337)            // number
console.log(typeof NaN)             // number
console.log(typeof Infinity)        // number
console.log(typeof Number('1'))     // number
 
console.log(typeof 512n))          // bigint 
 
console.log(typeof "foo")           // string
console.log(typeof true)            // boolean
console.log(typeof {})              // object
 
console.log(typeof Math.round)      // function
 
console.log(typeof Symbol())        // symbol
 
console.log(typeof undefined)       // undefined
 
console.log(typeof null)            // object
 
console.log(typeof [1, 2, 3])          // object
console.log(typeof Math)               // object
console.log(typeof new Boolean(true))  // object
console.log(typeof new Number(10))     // object
console.log(typeof new String('xyz'))  // object
 
                     
                            
                        

The following table summarizes the possible return values of typeof.

Type Result
Undefined “undefined”
Null “object”
Boolean “boolean”
Number “number”
BigInt “bigint”
String “string”
Symbol “symbol”
Function object “function”
Everything Else “object”

Typeof null is object

Although the null is a data type in JavaScript, typeof null returns “object”

                            
 console.log(typeof null)        // object
                     
                            
                        

In the first implementation of JavaScript, JavaScript values were represented as a type tag and a value. The type tag for objects was 0. null was represented as the NULL pointer (0x00 in most platforms). Consequently, null had 0 as type tag, hence the typeof return value “object”

A fix was proposed for ECMAScript (via an opt-in), but was rejected. It would have resulted in typeof null === ‘null’.