JavaScript typeof Array | Check for an array

  • by

The JavaScript typeof statement is useful in for data validation. JavaScript typeof Array return "object" because array is technically a type of object.

typeof [] === "Object" 
typeof 123 === "number"

In JS everything but primitives are objects. Primitives are : Numbers , Booleans , Null , Undefined , String , Symbol

The rest are objects (arrays, objects, maps, sets…)

Check if the variable is an array in couple of ways:

var isArr = data instanceof Array;
var isArr = Array.isArray(data);

JavaScript typeof array

Simple example code typeof array is an object.

<!DOCTYPE html>
<html>
<body>

  <script>
    const arr = [2,4,6,8]
    const obj = { type: 'Gear', valid: true }
    console.log(typeof arr)
    console.log(typeof obj)

  </script>

</body>
</html> 

Output:

JavaScript typeof Array

Read: Check if object is Array JavaScript

Do comment if you have any doubts or suggestions on this Js Array topic.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.