Given
let obj = {name: 1};
console.log(typeof obj.name, obj.name); // `"number"`, `1`
Why is name
identifier cast to string when using var
at object destructuring assignment?
let obj = {name: 1};
var {name} = obj;
console.log(name, typeof name); // `1` `string`
But not using let
or const
?
let obj = {name: 1};
let {name} = obj;
console.log(name, typeof name);
We can avoid this unexpected result by defining a different identifier
let obj = {name: 1};
var {name:_name} = obj;
console.log(_name, typeof _name);
though curious as to the significance of var
returning different results than let
or const
for the name
identifier at a browser environment?