Write your answer…
First, let's start with objects.
Hashnode is building a friendly and inclusive dev community. Come jump on the bandwagon!
💬 A beginner friendly place
🧠 Stay in the loop and grow your knowledge
🍕 >500K developers share programming wisdom here
❤️ Support the growing dev community!
Register ( 500k+ developers strong 👊)
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.
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)
Don't miss out!
Join the growing dev community
Get started (no password needed)
Or Sign in with: