Main Differences Between JavaScript's Object and Array (English)

JavaScript's "Object" and "Array" are two fundamental object types closely related to each other. When comparing these two, the following similarities and differences can be observed.

Commonalities

  • An "Array" is a subclass or sub prototype of an "Object." In other words, an "Array" is not a new data type; it shares the same data type, "object," with "Object."
  • The typeof operator for an "Array" object results in "object."

Differences

  • When using the instanceof operator to check an "Array" object, it matches both "Array" and "Object." Since "Array" is a subclass of "Object," it also matches "Object."
  • Objects created with "Object" can have properties that can be accessed using the same syntax as "Array" objects.
  • Objects created with "Object" are not "Array" objects. In other words, instances of "Object" are not arrays.
  • "Array" objects are also "Object" objects. This means arrays are a type of object.

Understanding the differences between objects and arrays can be illustrated through examples. Objects are collections of key-value pairs, while arrays are collections of elements with order. Moreover, arrays being a type of object inherit all attributes and methods of objects.

JavaScript Object와 Array의 핵심 차이점

// Object 예시
let person = {
    name: "John",
    age: 30,
    address: {
        city: "New York",
        zip: "10001"
    }
};

// Array 예시
let colors = ["red", "green", "blue"];

console.log(typeof person); // "object"
console.log(typeof colors); // "object"

console.log(person instanceof Object); // true
console.log(person instanceof Array); // false
console.log(colors instanceof Object); // true
console.log(colors instanceof Array); // true

console.log(person.address.city); // "New York"
console.log(colors[0]); // "red"