Guess What? Your JavaScript Array is an Object! 🤔
Jaydeep Pipaliya

Jaydeep Pipaliya @itsjp

Joined:
Mar 12, 2024

Guess What? Your JavaScript Array is an Object! 🤔

Publish Date: May 9 '24
1 1

In the quirky world of JavaScript, where the unexpected often becomes the rule, arrays are actually objects. If you're new to programming or coming from other languages, this might seem a bit odd. So, let's dive into why JavaScript treats arrays this way and what it means for you as a developer.

What's in a Type?

JavaScript is known for its loose typing system. Unlike more rigid languages that require explicit declarations, JavaScript uses dynamic typing. This flexibility is part of what makes JavaScript both powerful and perplexing.

When you use the typeof operator on an array, JavaScript returns object. Here's a quick example:

let fruits = ["apple", "banana", "cherry"];
console.log(typeof fruits); // Output: "object"
Enter fullscreen mode Exit fullscreen mode

Arrays are Special Objects

The reason arrays are considered objects in JavaScript is due to how the language is designed. In JavaScript, arrays are essentially objects with extra capabilities and a special internal property known as length. This length property automatically updates as the array grows or shrinks, something typical objects don't do.

What Does This Mean for You?

Understanding that arrays are objects clarifies several JavaScript behaviors:

  • Property Access: Like objects, you can access array elements using bracket notation (array[index]).

  • Flexibility: Arrays can have properties added to them, just like objects. However, it's best practice to use arrays for numerically indexed data and objects for named properties.

  • Methods and Prototypes: Arrays inherit from Array.prototype, allowing them to use methods like .push(), .pop(), and .map() which are not available in ordinary objects unless explicitly added.

Conclusion

The classification of arrays as objects in JavaScript might seem like a small detail, but it has significant implications on how you write and understand code. By embracing JavaScript's flexibility and understanding its nature, you can write more effective and efficient code.

Next time you declare an array, remember, you're technically working with a specialized object!

Comments 1 total

  • Taufik Nurrohman
    Taufik NurrohmanMay 9, 2024

    If you are also a PHP developer, you will realize that array in PHP is not an object (a class instance) but simply a built-in data storage. That’s why $a === $b equals to true in PHP but not in JavaScript:

    JS

    let $a = [1, 2, 3]; // This is an instance of `Array` class
    let $b = [1, 2, 3]; // This is an instance of `Array` class
    
    console.log($a === $b); // `false`
    
    Enter fullscreen mode Exit fullscreen mode

    PHP

    $a = [1, 2, 3]; // Nothing but a data storage
    $b = [1, 2, 3]; // Nothing but a data storage
    
    var_export($a === $b); // `true`
    
    Enter fullscreen mode Exit fullscreen mode
Add comment