Search code examples
javascriptobjectcurly-braces

Is object inside object or object equal object the same in JavaScript?


Is this piece of code:

const a = { aProp: 1 };
const c = {
    b: { a }
};

the same as this piece of code:

const a = { aProp: 1 };
const c = {
    b: a
};

?

I mean will c have exactly the same properties in both cases? And what technical differences are there in the code?

Thank you.


Solution

  • In a brief: no.

    b: { a } is a shorthand for b: { a: a } so it will result in a nested property:

    const c = {
       b: { a: { aProp: 1 } },
    };
    

    In your second case:

    b: a,
    

    it's just a normal assignment, which will result in:

    const c = {
        b: { aProp: 1 },
    };