DEV Community

Paramanantham Harrison for JS Mates

Posted on • Originally published at jsmates.com on

1

Check whether a JS variable an array or not

Let's consider a JS array,

const authors = ['Param', 'Joshua'];
Enter fullscreen mode Exit fullscreen mode

This is obviously an array, but if you need to check whether the data is an array or not. How do you do that check?

Using the Array.isArray method, it is easy to find out whether the value of a variable is an array or not.

console.log(Array.isArray(authors)); // true
Enter fullscreen mode Exit fullscreen mode

If you pass a non-array value to the method, then it will return false.

console.log(Array.isArray(true)); // false
console.log(
  Array.isArray({
    firstName: 'Param',
    lastName: 'Harrison'
  })
); // false
Enter fullscreen mode Exit fullscreen mode

Sentry blog image

How I fixed 20 seconds of lag for every user in just 20 minutes.

Our AI agent was running 10-20 seconds slower than it should, impacting both our own developers and our early adopters. See how I used Sentry Profiling to fix it in record time.

Read more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay