Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

JavaScript Breakdown of this code? Questions

alvie666

New Coder
Hello everyone. I am relatively new to js and I have this bit of code I am trying to understand.
1) Why is the first line var for test_obj curly braces instead of brackets?
2) Why does the second line exist at all? Didn't we already declare it in line one? Why is it ".number"?
2) Why does line three follow with .number.number, and again, why isn't declaring this value on line one suffice?
Thanks for your help!

let test_obj = {};
test_obj.number = {};
test_obj.number.number = 8;
let new_number = test_obj.number;
new_number.number = new_number.number + 5;
console.log(test_obj.number.number);
 
Last edited:
JavaScript:
// Initializes test_obj. With [] it would be a array.
let test_obj = {};

// Adds a number property
test_obj.number = {};

// Adds a nested number property.
test_obj.number.number = 8;

// Creates a reference to the nested object
let new_number = test_obj.number;

// Updates nested number value.
new_number.number = new_number.number + 5;

// Output of the value
console.log(test_obj.number.number);

More fantastic help for JS adventures W3Schools.com
 
JavaScript:
// Initializes test_obj. With [] it would be a array.
let test_obj = {};

// Adds a number property
test_obj.number = {};

// Adds a nested number property.
test_obj.number.number = 8;

// Creates a reference to the nested object
let new_number = test_obj.number;

// Updates nested number value.
new_number.number = new_number.number + 5;

// Output of the value
console.log(test_obj.number.number);

More fantastic help for JS adventures W3Schools.com
Thank you! So initializing test_obj, using curly braces allows it to be undefined at first, then the property is set later?
 

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom