Check if a Variable is an Array in JavaScript



To check if a variable is an array, use “instanceof. The following is the syntax −

variable instanceof Array

Example

Let’s seen an example to check if the variable “sports” is an array or not?

<xmp>
<html>
   <body>  
      <script>
 
        var sports = [ "tennis", "football", "cricket" ];
     
        if (sports instanceof Array) {
          alert('Array!');
        } else {
          alert('Not an array');
        }
      </script>
     
   </body>
</html>
</xmp>  
Updated on: 2020-06-13T13:24:23+05:30

184 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements