JavaScript - typeof Operator
JavaScript - typeof Operator
The typeof Operator
Thetypeofoperator in JavaScript is a unary operator used to get the data type of a particular variable. It is placed before its single operand, which can be of any type. Its returns a string value indicating the data type of its operand. JavaScript contains primitive and non-primitive data types.
There are seven primitive or basic in JavaScript data types number, string, boolean, undefined, null, symbol, and bigint. There is also a composite data type called object. The object data type contains three sub data type Object, Array and Date.
Syntax
Following is the syntax of the typeof operator β
typeof (operand);We can write the operand without parenthesis as follows β
typeof operand;Parameter
- operandβ It can be a value, variable or expression representing theobjectorprimitive. In JavaScript, primitives are data that are not object and have no methods or properties.
Return Value
- It returns the string value representing the data type of the operand.
Datatypes Returned by typeof Operator
Here is a list of the return values for thetypeofOperator.
| Type | String Returned by typeof |
|---|---|
| Number | βnumberβ |
| String | βstringβ |
| Boolean | βbooleanβ |
| Object | βobjectβ |
| Function | βfunctionβ |
| Undefined | βundefinedβ |
| Null | βobjectβ |
| Symbol | βsymbolβ |
| Bigint | βbigintβ |
There are seven primitive datatypes in JavaScript number, string, boolean, bigint, undefined, null, and symbol. The typeof operator is useful to identify these primitive or basic datatypes.
The typeof operator returns same datatype of the all primitive values except the null. It returns βobjectβ for the null values.
For object, date and array it returns βobjectβ as datatype.
For functions and classes, it returns βfunctionβ as datatype.
Letβs use the typeof operator to identify these datatypes one by one.
typeof 10; // returns 'number'
typeof 'Hello World'; // returns 'string'
typeof true; // returns 'boolean'
typeof {name:"CodesCompiler"}; // returns 'object'
typeof function foo(){};// returns 'function'
typeof undefined; // returns 'undefined'
typeof null; // returns 'object'
typeof Symbol(); // returns 'symbol'
typeof 10n; // returns 'bigint'JavaScript typeof Operator to Check Number Type
In JavaScript, number type represents numeric values. JavaScript uses a floating-point representation for all numbers. The JavaScript typeof operator returns βnumberβ for all types of numbers such as integers, floating points, zero, Infinity, NaN etc.
typeof 10; //returns "number";
typeof -10; //returns "number";
typeof 0; //returns "number";
typeof 10.20; //returns "number";
typeof Math.LN10; //returns "number";
typeof Infinity; //returns "number";
typeof NaN; //returns "number";
typeof Number('1'); //returns "number";
typeof Number('hello'); //returns "number";Example
The example below demonstrates how to use the typeof operator to check number data types.
Output
Using typeof operator to check number data type
number
Set the variable to different value and then try...JavaScript typeof Operator to Check String Type
Strings represent sequences of characters. The typeof operator helps identify string variables. The JavaScript typeof operator returns βstringβ for all type of strings, such as empty string, string of characters, string words, multiline string etc.
typeof "10"; //returns "string";
typeof ""; //returns "string";
typeof "Hello World"; //returns "string";
typeof String(10); //returns "string";
typeof typeof 2; //returns "string";Example
In the example below we use typeof operator to check string datatype.
Output
string
Set the variable to different value and then try...JavaScript typeof Operator to Check Boolean Type
The boolean values represent true or false. The tyepof operand returns boolean for boolean variables.
typeof true; //returns "boolean";
typeof false; //returns "boolean";
typeof Boolean(10); //returns "boolean";Example
In the example below, we use typeof operator to check boolean datatype.
Output
boolean
Set the variable to different value and then try...JavaScript typeof Operator to Check Symbol Type
Symbols were introduced in ES6 and provide a way to create unique identifiers. Using typeof operator with symbols returns βsymbolβ.
typeof Symbol(); //returns "symbol";
typeof Symbol("unique values"); //returns "symbol";Example
In the example below, we use typeof operator to check Symbol datatype.
Output
symbol
Set the variable to different value and then try...JavaScript typeof Operator to Check Undefined and Null
The βundefinedβ type represents a lack of a value. The βnullβ type represents the absence of any object value. When using typeof operator with an undefined variable, it returns βundefinedβ. Surprisingly, using typeof operator with null also returns βobjectβ, which is a known quirk in JavaScript.
typeof undefined; //returns "undefined";
typeof null; //returns "object";Please note typeof operator will return βundefinedβ for both undeclared variable and declared but unassigned variables.
Example
In the example below, we use typeof operator to check undefined datatype.
Output
undefined
Set the variable to different value and then try...JavaScript typeof Operator to Check Object Type
The JavaScript typeof operator returns βobjectβ for all types of object such as JavaScript objects, arrays, dates, regex, etc.
const obj = {age: 23};
typeof obj; //returns "object";
const arr = [1,2,3,4];
typeof arr; //returns "object";
typeof new Date(); //returns "object";
typeof new String("Hello World"); //returns "object";Example
In the example below, we use typeof operator to check object datatype.
Output
object
Set the variable to different value and then try...JavaScript typeof Operator to Check Function Type
Functions are first class citizens in JavaScript. The JavaScript typeof operator returns βfunctionβ for all types of functions. Interestingly it returns βfunctionβ for classes also.
const myFunc = function(){return "Hello world"};
typeof myFunc; //returns "function";
const func = new Function();
typeof func; //returns "function";
class myClass {constructor() { }}
typeof myClass; // returns "function";Example
In the example below, we use typeof operator to check function datatype.
Output
function
Set the variable to different value and then try...JavaScript typeof Operator to Check BigInt Type
The typeof operator returns βbigintβ for BigInt numbers. BigInt values are the numeric values that are too big to be represented by the number primitive.
typeof 100n; // returns "bigint"JavaScript typeof Operator in Real-Time Development
For example, the developer gets the data from API. If there is only a single string, API returns the string response, and for multiple strings, API returns the array of strings. In this scenario, developers require to check whether the type of the response is string or array, and if it is an array, they need to traverse each string of the array.
Example
In the example below, we check the type of the response variable and print its value accordingly.
Output
The response values are : Hello World! How are you?JavaScript Arrays and typeof Operator
Arrays, despite being a type of object in JavaScript, have a distinct behavior with the typeof operator.
let numbers = [1, 2, 3];
typeof numbers; // Output: 'object'Arrays return βobjectβ when using typeof operator, so for precise array detection, itβs often better to useArray.isArray().
Example
Output
true
Set the variable to different value and then try..