How do you check if a variable is an array in JavaScript?

JavascriptWeb DevelopmentFront End Technology

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>  
raja
Published on 18-Jan-2018 12:35:51
Advertisements