Search code examples
javascriptreferencepass-by-referencevariable-declaration

Declare JavaScript variable as reference


I recall there being a feature of Javascript, such that the something resembling the following is possible:

let foo = { bar: "bar" };
let { baz } = foo.bar;

baz = "baz";
console.log(foo.bar); // "baz"

Does such a feature exist? If so, what is the correct syntax to get this to work?


Solution

  • Maybe you are looking for destructuring assignment

    let foo = { bar: "baz" };
    let { bar } = foo;
    
    console.log(bar); // "baz"