When should I use an object over an array and vice versa?
javascript
Solution
Use an object when you want named keys (key:value pairs), otherwise use an array. You can even use array notation with objects:
object = {name:"James", age:17};
object['name']; // James
In your case, it looks like you're storing information in fields about a person. Here, it makes perfect sense to use an object because the key identifies the type of data you're storing in each field. An array is for a simple list of data.
Problem
I commonly use arrays to achieve tasks that might be better suited using objects. Rather than continuing to wonder if it would be better to use an object for a task, I've decided to pose the question on Stack Overflow. ``` array = ["James", 17]; object = {name:"James", age:17}; ``` They both seem similar in syntax, and to me they seem to achieve the same thing when used. When should I use an an object over an array and vice versa? Are there general rules as to when I should use one over the other?