Skip to content

WebDevHubs

  • Home
  • HTML
  • CSS
  • JavaScript
  • Web Technologies
  • Web Templates
  • Toggle search form

How Do I Check if a Variable Is an Array in JavaScript?

Posted on December 18, 2024February 1, 2025 By Admin No Comments on How Do I Check if a Variable Is an Array in JavaScript?

There are several methods to check if a variable is an array or not. In this article, we will explore all methods with code examples and explanations.

1. Using Array.isArray() Method

The basic method to check if a variable is an array or not is by using the Array.isArray() method. It is a built-in JavaScript method used to check the variable is an array or not.

let arr = [10, 20, 30, 40, 50];
let num = 42;

console.log(Array.isArray(arr)); // Output: true
console.log(Array.isArray(num)); // Output: false

2. Using instanceof Array

The instanceof operator is used to check whether a variable is an instance of a specific constructor or not (in this case, it will be Array).

let arr = [10, 20, 30, 40, 50];
let obj = {};

console.log(arr instanceof Array); // Output: true
console.log(obj instanceof Array);    // Output: false

3. Checking the Constructor Property

You can also check the constructor property of a variable to check the given variable is an Array or not.

let arr = [10, 20, 30, 40, 50];
let obj = {};

console.log(arr.constructor === Array); // Output: true
console.log(obj.constructor === Array);    // Output: false
JavaScript, Web Technologies Tags:JavaScript-Array, JavaScript-Questions

Post navigation

Previous Post: Can We Use a break in forEach JavaScript?
Next Post: Get the Last Item of an Array in JavaScript

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Archives

  • June 2025
  • May 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024

Categories

  • CSS
  • HTML
  • JavaScript
  • Lodash
  • PHP
  • Python
  • Web Technologies
  • Web Templates

Recent Posts

  • JavaScript Array isArray() Method
  • JavaScript Array forEach() Method
  • JavaScript Array includes() Method
  • JavaScript Array keys() Method
  • JavaScript Array lastIndexOf() Method

Recent Comments

No comments to show.

Copyright © 2025 WebDevHubs.

Powered by PressBook Green WordPress theme