Write your answer…

9 responses

First, let's start with objects.

Object is a morty. Every universe have their own version of morty. You have hammerhead morty, lawyer morty, fat morty, evil morty and so on. Now, each of these morties have their own unique trait and personalities. So, an object is nothing but just a container which can have its own defined trait or "property". Also, object can have its own "method" of doing things. So if evil morty is an object then it has its own defined properties like an evil eye patch for an eye, and his own method for dealing with rick. This is how object works in javascript.

Now, suppose Rick decided to make a robot morty maker machine. Now the possibilities you can have for the kind of morty reduces drastically from infinite to a fixed set because Rick's machine can only possibly make robot morties of certain type. Each morty must have their eye at proper position, their heads at their designated place, their tails between their legs. So, basically every thing must go to their properly indexed position for the machine to build up a morty. Also, they do not have free will because they are robots and their methods of performing things are defined by Rick. This is what an array is in javascript. It is a special object whose properties are defined as indexes. You can put anything in these indexes and can work with it using the methods it already provides.


For Arrays, you could use Skyscrapers and explain them how each floor is indexed one after another using the floor number.

For Objects, you could use something like a bunch of students and how each student has a name, an age, an address, etc., and how you would store that data in form of objects.

That is how I got it when I was a kid. 🤔

  • Both are entities to store any type of value (primitives (string,boolean,number), function, object, null and undefined).
  • Both store the value with 'keys'
  • In objects and arrays keys are (coerced to) strings
  • The order of keys are not important on objects, one should not rely on key order
  • Arrays are specialized kind of objects where keys are Numbers (coerced to strings) and the order of keys are important and preserved.
  • Therefore, for the above reason there is something called sparse Arrays (missing keys). But there is no equivalent sparse "Object".
  • Array constructor is an instance of Object constructor but not vice versa (all arrays are objects, but not all objects are arrays)
  • For the above reason Arrays have special methods down the prototype chain such as reduce,map,forEach etc..

These are some rough headlines that comes to mind which I think can help.

Show all replies

Thank you. Sooner or later they will bump into this anyway :)

Reply to this…

Arrays store a set of values; and let you access those values by their index/position in the set.

Objects store a set of values; let you name the values; and let you access the values by name.

...this leaves out lots of details, but you did say they're new to programming ;)

Personally, since excel and tables I use very often, I like to compare array with a table column (each line number refers to an value. Sorting, filtering and iterating is also easy to understand then). Object is a table row (but including table header too, which contains column names aka object keys)

Show more responses