JS objects and properties


var myObject {
    prop1: {
        innerprop1: innerval1,
        innerprop1: innerval2   // I want to access this value
    prop2: val2

function myfun(object, outerprop, innerprop, value){
    object.outerprop.innerprop = value;     //      ...(1)
    object[outerprop][innerprop] = value;   //      ...(2) 

In the above program, I am unable to store the value in the statement (1) using dot notation but in case of the statement (2), I have used bracket notation and it worked.
I am new to Javascript and still learning. It would be great if someone could help me understand this. Thank You.

This is looking for a property named “innerprop” on a property named “outerprop”. Dot notation requires the literal name of the property.

This evaluates the values assigned to outerprop and innerprop and looks for those.

1 Like

I appreciate your help so much. :blush: :pray:

I’m glad I could help. Happy coding!

1 Like