5. Why use string and not dot notation?


#1


https://www.codecademy.com/courses/objects-ii/0/5?curriculum_id=506324b3a7dffd00020bf661#

The code works fine!

I wanted to know why instead of

var aProperty = james.job;

just a string called "job" is required.

I don't understand why just a string of "job" links back to the property that is james.job. I thought "job" would just be "job".


var james = {
    job: "programmer",
    married: false
};

// set to the first property name of "james"
var aProperty = "job";

// print the value of the first property of "james" 
// using the variable "aProperty"
console.log(james[aProperty]);


#2

The point that they seem to try to make is that the key can be a value, doesn't have to be written in the code. That means it can be obtained from input, generated during runtime, randomised etc


#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.