The JavaScript typeof statement is useful 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 a couple of ways:
To specifically check if a value is an array, you can use the Array.isArray()
method, which returns true
if the provided value is an array, and false
otherwise:
var isArr = data instanceof Array;
var isArr = Array.isArray(data);
Using Array.isArray()
is a more reliable way to determine if a value is an array compared to using typeof
.
JavaScript typeof array
A 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:
Read: Check if object is Array JavaScript
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