HomeTutorsContact

How To Check Data Types in JavaScript?

By Gulshan Saini
Published in JavaScript
June 20, 2020
1 min read

Data types are an important concept in any programming language. They tell us what type of data variable holds. In JavaScript typeof operator can be used to get the data type of any variable.

A variable in JavaScript could hold primitive, object, and function data type. The functions are not data structure however when we evaluate any expression that holds function as it’s value the typeof returns function in return as the data type.

Primitive data types are string, number, bigint, boolean, undefined, and symbol.

On the other side the Object data type includes null, array and any instance created using new keyword like new Object(), new Array(), new Map(), new Set(), new WeakMap(), new WeakSet(), and new Date().

Below are examples of how to check different data types

String

typeof 'Tutorial.Tips'

Output

"string"

Number

typeof 100.24

Output

"number"

Bigint

A BigInt is created by appending n to the end of an integer literal. BigInts, can operate with numbers beyond the Number.MAX_SAFE_INTEGER i.e. 9007199254740991.

typeof 9007199254740991n

Output

"bigint"

Boolean

typeof true

Output

"boolean"

undefined

// variable whose value is not set
let undefinedVariable
typeof undefinedVariable

Output

"undefined"

Symbol

Symbol were introduced in ES6 i.e. ECMAScript 2015. A Symbol value represents a unique identifier.

typeof Symbol('$')

Output

"symbol"

Object

Object Literal

// car object
let car = { wheels: 4 }
typeof car

Output

"object"

Date instance

// date instance created using new keyword
let date = new Date()
typeof date

Output

"object"

null

You would be surprised to know that null is also having data type of object

// we have explicitly created
// variable and assigned it null value
let person = null
typeof person

Output

"object"

Arrays

let students = ['Ram', 'Sham', 'John']
typeof students

Output

"object"

Function

let printTime = function() {
  console.log(Date.now())
}
typeof printTime

Output

"function"

Tags

#javascript
Previous Article
How to disable prettier in VSCode for a specific project?

Related Posts

JavaScript
JavaScript get tomorrow's date
December 13, 2021
1 min
Gulshan Saini

Gulshan Saini

Fullstack Developer

Topics

Angular
JavaScript
ReactJS
Typescript
Linux

Subscribe to our newsletter!

We'll send you the best of our blog just once a month. We promise.

Quick Links

Contact UsBrowserCSSPythonPuppeteer

Social Media