JS Typeof

JS Tutorial

JS Version

JS Objects

JS Function

JS Classes

JS Async

JS HTML DOM

JS Browser BOM

JS Web API

JS AJAX

JS JSON

JS vs JQUERY

JS Graphics

JavaScript typeof

The typeof Operator

The typeof operator is used to search the data type of a JavaScript variable.

Example

<!DOCTYPE html>

<html>

<body>

<h2>The JavaScript typeof Operator</h2>

<p>The typeof operator returns the type of a variable, object, function or expression.</p>

<p id=”demo”></p>

<script>

document.getElementById(“demo”).innerHTML =

  typeof “john” + “<br>” +

  typeof 3.14 + “<br>” +

  typeof NaN + “<br>” +

  typeof false + “<br>” +

  typeof [1,2,3,4] + “<br>” +

  typeof {name:’john’, age:34} + “<br>” +

  typeof new Date() + “<br>” +

  typeof function () {} + “<br>” +

  typeof myCar + “<br>” +

  typeof null;

</script>

</body>

</html>

Output

The JavaScript typeof Operator

The typeof operator returns the type of a variable, object, function or expression.

Primitive Data

A primitive data value is defined as a single simple data value having no additional properties and methods.

The typeof operator returns one of these primitive types:

  • string
  • number
  • boolean
  • undefined

Example

<!DOCTYPE html>

<html>

<body>

<h2>JavaScript typeof</h2>

<p>The typeof operator returns the type of a variable or an expression.</p>

<p id=”demo”></p>

<script>

document.getElementById(“demo”).innerHTML =

typeof “john” + “<br>” +

typeof 3.14 + “<br>” +

typeof true + “<br>” +

typeof false + “<br>” +

typeof x;

</script>

</body>

</html>

Output

JavaScript typeof

The typeof operator returns the type of a variable or an expression.

Complex Data

The typeof operator returns one of two complex types:

  • function
  • object

The typeof operator returns “object” for objects, arrays, and null.

The typeof operator does not return “object” for functions.

Example

<!DOCTYPE html>

<html>

<body>

<h2>JavaScript typeof</h2>

<p>The typeof operator returns object for both objects, arrays, and null.</p>

<p>The typeof operator does not return object for functions.</p>

<p id=”demo”></p>

<script>

document.getElementById(“demo”).innerHTML =

typeof {name:’john’, age:34} + “<br>” +

typeof [1,2,3,4] + “<br>” +

typeof null + “<br>” +

typeof function myFunc(){};

</script>

</body>

</html>

Output

JavaScript typeof

The typeof operator returns object for both objects, arrays, and null.

The typeof operator does not return object for functions.

The Data Type of typeof

The typeofoperator is not a variable but an operator. Operators ( + – * / ) having no data type.

The typeof operator is used to returns a string (containing the type of the operand).

The constructor Property

The constructor property is used to return the constructor function for all JavaScript variables.

Example

<!DOCTYPE html>

<html>

<body>

<h2>The JavaScript constructor Property</h2>

<p>The constructor property returns the constructor function for a variable or an

object.</p>

<p id=”demo”></p>

<script>

document.getElementById(“demo”).innerHTML =

  “john”.constructor + “<br>” +

  (3.14).constructor + “<br>” +

  false.constructor + “<br>” +

  [1,2,3,4].constructor + “<br>” +

  {name:’john’, age:34}.constructor + “<br>” +

  new Date().constructor + “<br>” +

  function () {}.constructor;

</script>

</body>

</html>

Output

The JavaScript constructor Property

The constructor property returns the constructor function for a variable or an object.

The constructor property can be checked that is used to find out if an object is an Array (contains the word “Array”):

Example

<!DOCTYPE html>

<html>

<body>

<h2>JavaScript Arrays</h2>

<p>This “home made” isArray() function returns true when used on an array:</p>

<p id=”demo”></p>

<script>

const fruits = [“Banana”, “Orange”, “Apple”];

document.getElementById(“demo”).innerHTML = isArray(fruits);

function isArray(myArray) {

  return myArray.constructor.toString().indexOf(“Array”) > -1;

}

</script>

</body>

</html>

Output

JavaScript Arrays

This "home made" isArray() function returns true when used on an array:

true

Example

<!DOCTYPE html>

<html>

<body>

<h2>JavaScript Arrays</h2>

<p>This “home made” isArray() function returns true when used on an array:</p>

<p id=”demo”></p>

<script>

const fruits = [“Banana”, “Orange”, “Apple”];

document.getElementById(“demo”).innerHTML = isArray(fruits);

function isArray(myArray) {

  return myArray.constructor.toString().indexOf(“Array”) > -1;

}

</script>

</body>

</html>

Output

JavaScript Arrays

This "home made" isArray() function returns true when used on an array:

true

Checking if the object is an Array function:

Example

<!DOCTYPE html>

<html>

<body>

<h2>JavaScript Array Object</h2>

<p>This “home made” isArray() function returns true when used on an array:</p>

<p id=”demo”></p>

<script>

const fruits = [“Banana”, “Orange”, “Apple”, “Mango”];

document.getElementById(“demo”).innerHTML = isArray(fruits);

function isArray(myArray) {

  return myArray.constructor === Array;

}

</script>

</body>

</html>

Output

JavaScript Array Object

This "home made" isArray() function returns true when used on an array:

true

You can check the constructor property to find out if an object is a Date (contains the word “Date”):

Example

<!DOCTYPE html>

<html>

<body>

<h2>JavaScript Date Object</h2>

<p>This “home made” isDate() function returns true when used on an date:</p>

<p id=”demo”></p>

<script>

const myDate = new Date();

document.getElementById(“demo”).innerHTML = isDate(myDate);

function isDate(myDate) {

  return myDate.constructor.toString().indexOf(“Date”) > -1;

}

</script>

</body>

</html>

Output

JavaScript Date Object

This "home made" isDate() function returns true when used on an date:

true

Checking if the object is a Date function:

Example

<!DOCTYPE html>

<html>

<body>

<h2>JavaScript Date Object</h2>

<p>This “home made” isDate() function returns true when used on an date:</p>

<p id=”demo”></p>

<script>

const myDate = new Date();

document.getElementById(“demo”).innerHTML = isDate(myDate);

function isDate(myDate) {

  return myDate.constructor === Date;

}

</script>

</body>

</html>

Output

JavaScript Date Object

This "home made" isDate() function returns true when used on an date:

true

Undefined

In JavaScript, a variable having no value, has the value undefined. The type is also undefined.

Example

<!DOCTYPE html>

<html>

<body>

<h2>JavaScript</h2>

<p>The value (and the data type) of a variable with no value is <b>undefined</b>.</p>

<p id=”demo”></p>

<script>

let car;

document.getElementById(“demo”).innerHTML =

car + “<br>” + typeof car;

</script>

</body>

</html>

Output

JavaScript

The value (and the data type) of a variable with no value is undefined.

undefined

undefined

Any variable can be empty, by setting the value to undefined. The type will also be undefined.

Example

<!DOCTYPE html>

<html>

<body>

<h2>JavaScript</h2>

<p>Variables can be emptied if you set the value to <b>undefined</b>.</p>

<p id=”demo”></p>

<script>

let car = “Volvo”;

car = undefined;

document.getElementById(“demo”).innerHTML = car + “<br>” + typeof car;

</script>

</body>

</html>

Output

JavaScript

Variables can be emptied if you set the value to undefined.

undefined

undefined

Empty Values

An empty value has no relation with undefined.

An empty string consists of both a legal value and a type.

Example

<!DOCTYPE html>

<html>

<body>

<h2>JavaScript</h2>

<p>An empty string has both a legal value and a type:</p>

<p id=”demo”></p>

<script>

let car = “”;

document.getElementById(“demo”).innerHTML =

“The value is: ” +

car + “<br>” +

“The type is: ” + typeof car;

</script>

</body>

</html>

Output

JavaScript

An empty string has both a legal value and a type:

The value is:
The type is: string

Null

In JavaScript null is “nothing”. It is something that doesn’t exist.

Unfortunately, in JavaScript, the data type of null is an object.

Consider it a bug in JavaScript that typeof null is an object. It must be null.

Empty an object by setting it to null:

Example

<!DOCTYPE html>

<html>

<body>

<h2>JavaScript</h2>

<p>Objects can be emptied by setting the value to <b>null</b>.</p>

<p id=”demo”></p>

<script>

let person = {firstName:”John”, lastName:”Doe”, age:50, eyeColor:”blue”};

person = null;

document.getElementById(“demo”).innerHTML = typeof person;

</script>

</body>

</html>

Output

JavaScript

Objects can be emptied by setting the value to null.

object

Difference Between Undefined and Null

undefined and null are equal in value but different in type:

Example

<!DOCTYPE html>

<html>

<body>

<h2>JavaScript</h2>

<p>Undefined and null are equal in value but different in type:</p>

<p id=”demo”></p>

<script>

document.getElementById(“demo”).innerHTML =

typeof undefined + “<br>” +

typeof null + “<br><br>” +

(null === undefined) + “<br>” +

(null == undefined);

</script>

</body>

</html>

Output

JavaScript

Undefined and null are equal in value but different in type:

undefined
object

false
true