The function toString method?from Object.prototype is the best approach to determine whether an object is an instance of a given class or not.

var arrayList = [1 , 2, 3];

When we utilize method overloading in JavaScript, type checking an object is one of the greatest applications. Let's assume we have a method named greet that can accept both a single string and a list of strings in order to better comprehend this. We need to know what sort of parameter is being supplied in order to make our greet method functional in both scenarios: is it a single value or a list of values?

function greet(param) {
  if() {
    // here have to check whether param is array or not
  else {

However, given the method above, it might not be essential to determine the type of the array. Instead, we can determine whether the array has a single value string and then place the array logic code in the else block.

function greet(param) {
   if(typeof param === 'string') {
   else {
     // If param is of type array then this block of code would execute

Right now, using the two previous implementations is fine, but when a parameter can be an object type, an array, or a single value, things get tricky.

Resuming our discussion of determining an object's type, we can do so by using Object.prototype.toString.

if( === '[object Array]') {

You can use the jQuery isArray method if you're already using jQuery:

if($.isArray(arrayList)) {
} else {
  console.log('Not an array');

FYI To determine if an object is an array or not, jQuery internally uses the method.

You can also use a modern browser to:

Array.isArray(arrayList); Chrome 5, Firefox 4.0, Internet Explorer 9, Opera 10.5 and Safari 5 all support array.

Recommended Posts

View All

Learn JavaScript Closures with Code Examples

Master JavaScript closures with code examples! Our comprehensive guide explains how closures work and how to use them in your code. Start learning now...

Top 10 ES6 Features Every JavaScript Developer Must Know

The specification for JavaScript implementation is provided by ES6. Learn about its recently added ES6 features that make writing JavaScript easier!

What is JSON? Know how it Works with Examples

Learn what JSON is and how it works with examples in this comprehensive guide. Discover the basics of JSON syntax, its applications, and why it has be...

JavaScript Template Literals Explained

JavaScript template literals are one of JavaScript's most powerful capabilities. They enable us to generate dynamic strings without the need for conca...

Calculate the length of an associative array using JavaScript

Associative arrays are regular arrays in JavaScript where an element is present at a specific index.