JavaScript typeof

By | August 26, 2022

In JavaScript there are 5 different data types that can contain values:

  • string
  • number
  • boolean
  • object
  • function

There are 6 types of objects:

  • Object
  • Date
  • Array
  • String
  • Number
  • Boolean

And 2 data types that cannot contain values:

  • null
  • undefined

The typeof Operator

You can use the typeof operator to find 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>

Result:

The JavaScript typeof Operator

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

string
number
number
boolean
object
object
object
function
undefined
object

Please observe:

  • The data type of NaN is number
  • The data type of an array is object
  • The data type of a date is object
  • The data type of null is object
  • The data type of an undefined variable is undefined *
  • The data type of a variable that has not been assigned a value is also undefined *

You cannot use typeof to determine if a JavaScript object is an array (or a date).

Primitive Data

A primitive data value is a single simple data value with no additional properties and methods.

The typeof operator can return 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>

Result:

JavaScript typeof

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

string
number
boolean
boolean
undefined

Complex Data

The typeof operator can return 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>

Result:

JavaScript typeof

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

The typeof operator does not return object for functions.

object
object
object
function

The typeof operator returns “object” for arrays because in JavaScript arrays are objects.

The Data Type of typeof

The typeofoperator is not a variable. It is an operator. Operators ( + – * / ) do not have any data type.

But, the typeof operator always returns a string (containing the type of the operand).

The constructor Property

The constructor property returns 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>

Result:

The JavaScript constructor Property

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

function String() { [native code] }
function Number() { [native code] }
function Boolean() { [native code] }
function Array() { [native code] }
function Object() { [native code] }
function Date() { [native code] }
function Function() { [native code] }

You can check the constructor property 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>

Result:

JavaScript Arrays

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

true

Or even simpler, you can check 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>

Result:

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>

Result:

JavaScript Date Object

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

true

Or even simpler, you can check 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>

Result:

Undefined

In JavaScript, a variable without a 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> 

Result:

JavaScript

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

undefined
undefined

Any variable can be emptied, 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> 

Result:

JavaScript

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

undefined
undefined

Empty Values

An empty value has nothing to do with undefined.

An empty string has 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>

Result:

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 supposed to be something that doesn’t exist.

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

You can consider it a bug in JavaScript that typeof null is an object. It should be null.

You can 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> 

Result:

JavaScript

Objects can be emptied by setting the value to null.

object

You can also empty an object by setting it to undefined:

Example:

<!DOCTYPE html>
<html>
<body>
<h2>JavaScript</h2>
<p>Objects can be emptied by setting the value to <b>undefined</b>.</p>
<p id="demo"></p>
<script>
let person = {firstName:"John", lastName:"Doe", age:50, eyeColor:"blue"};
person = undefined;
document.getElementById("demo").innerHTML = person;
</script>
</body>
</html> 

Result:

JavaScript

Objects can be emptied by setting the value to undefined.

undefined

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> 

Result:

JavaScript

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

undefined
object

false
true

Leave a Reply

Your email address will not be published. Required fields are marked *