Skip to content

WebDevHubs

  • Home
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • Java
  • Selenium
  • PHP
  • Python
  • Programs
  • 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 *

Categories

  • CSS
  • HTML
  • Interview Experience
  • Java
  • JavaScript
  • Lodash
  • PHP
  • Programs
  • Python
  • Selenium
  • Software Testing
  • Web Technologies
  • Web Templates

Recent Posts

  • Java ArrayList trimToSize() Method
  • Java ArrayList toArray() Method
  • Java ArrayList subList() Method
  • Java ArrayList spliterator() Method
  • Java ArrayList sort() Method

Recent Comments

No comments to show.

Important Pages

  • About Us
  • Contact Us
  • Terms of Use
  • Privacy Policy

Web Development

  • HTML
  • CSS
  • JavaScript
  • PHP

Programming Languages

  • Java
  • Python
  • PHP
  • Programs

Others

  • Selenium
  • Lodash
  • Java ArrayList
  • JavaScript Array Methods

Copyright © 2025 WebDevHubs.

Powered by PressBook Green WordPress theme