Show Output
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Check If Object is an Array in JavaScript</title> </head> <body> <script> // Creating some variables var v1 = {name: "John", age: 18}; var v2 = ["red", "green", "blue", "yellow"]; var v3 = [1, 2, 3, 4, 5]; var v4 = null; // Testing the variables data type document.write(typeof(v1) + "<br>"); // Prints: "object" document.write(typeof(v2) + "<br>"); // Prints: "object" document.write(typeof(v3) + "<br>"); // Prints: "object" document.write(typeof(v3) + "<hr>"); // Prints: "object" // Testing if the variable is an array document.write(Array.isArray(v1) + "<br>"); // Prints: false document.write(Array.isArray(v2) + "<br>"); // Prints: true document.write(Array.isArray(v3) + "<br>"); // Prints: true document.write(Array.isArray(v4)); // Prints: false </script> </body> </html>