JavaScript: typeof operator


The typeof operator is used to get the data type (returns a string) of its operand. The operand can be either a literal or a data structure such as a variable, a function, or an object. The operator returns the data type.


typeof operand
typeof (operand)

There are six possible values that typeof returns: object, boolean, function, number, string, and undefined. The following table summarizes possible values returned by the typeof operator.

Type of the operand Result
Object "object"
boolean "boolean"
function "function"
number "number"
string "string"
undefined "undefined"

Examples of typeof operator: string

typeof ""
typeof "abc"
typeof (typeof 1)

Examples of typeof operator: number

typeof 17
typeof -14.56
typeof 4E-3
typeof Infinity
typeof Math.LN2
typeof NaN

Examples of typeof operator: boolean

typeof false
typeof true

Examples of typeof operator: function

typeof Math.tan
typeof function(){}

Examples of typeof operator: object

typeof {a:1}
typeof new Date()
typeof null
typeof /a-z/
typeof Math
typeof JSON

Examples of typeof operator: undefined

typeof undefined
typeof abc

More examples on typeof operator

typeof(4+7);  //returns number
typeof("4"+"7"); //returns string
typeof(4*"7"); //returns number
typeof(4+"7"); //returns string

What is the difference between typeof myvar and typeof(myvar) in JavaScript?

There is absolutely no difference between typeof myvar and typeof(myvar). The output of the following codes will be same i.e. "undefined".

alert(typeof myvar);


How to detect an undefined object property in JavaScript?

The following method is the best way to detect an undefined object property in JavaScript.

if (typeof xyz === "undefined")
alert("xyz is undefined");

How to check whether a variable is defined in JavaScript or not?

The following method is the best way to detect an undefined object property in JavaScript.

if (typeof xyz === "undefined")
alert("Varaible is undefined");
alert("Variable is defined");

The following example tests the data type of variables.

JS Code

var index = 8;
  var result = (typeof index === 'number');
  // Output: true
var description = "w3resource";
  var result = (typeof description === 'string');
  // Output: true

Previous: JavaScript: this Operator
Next: JavaScript: void operator

Test your Programming skills with w3resource's quiz.

JavaScript: Tips of the Day

How to remove a specific item from an array?

Find the index of the array element you want to remove using indexOf, and then remove that index with splice.

The splice() method changes the contents of an array by removing existing elements and/or adding new elements.

const array = [2, 5, 9];


const index = array.indexOf(5);
if (index > -1) {
  array.splice(index, 1);

// array = [2, 9]

The second parameter of splice is the number of elements to remove. Note that splice modifies the array in place and returns a new array containing the elements that have been removed.

For the reason of completeness, here are functions. The first function removes only a single occurrence (i.e. removing the first match of 5 from [2,5,9,1,5,8,5]), while the second function removes all occurrences:

function removeItemOnce(arr, value) {
  var index = arr.indexOf(value);
  if (index > -1) {
    arr.splice(index, 1);
  return arr;

function removeItemAll(arr, value) {
  var i = 0;
  while (i < arr.length) {
    if (arr[i] === value) {
      arr.splice(i, 1);
    } else {
  return arr;
console.log(removeItemOnce([2,5,9,1,5,8,5], 5))
console.log(removeItemAll([2,5,9,1,5,8,5], 5))

Ref: https://bit.ly/2N9nKRp