obj instanceof Array can be false, even if
obj is an
In modern browsers you can do:
You could also try using the
myArray instanceof Array
isArray() utility function returns
value is an array.
<!DOCTYPE html> <html> <body> <script> const array = [1, 2, 3]; console.log(Array.isArray(array)) </script> </body> </html>
Value instanceof Array
<script> const arr = [1, 2, 3]; console.log(arr instanceof Array) </script>
Checking the constructor property of the variable
Another method to check a variable is an array by checking its constructor with Array.
<script> const arr = [1, 2, 3]; console.log(arr.constructor === Array) </script>
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
Degree in Computer Science and Engineer: App Developer and has multiple Programming languages experience. Enthusiasm for technology & like learning technical.