In ES6/ES2015 I can use Object destructuring to pull fields from objects passed as function parameters:
function userId({id}) {
return id;
}
var user = {
id: 42,
};
console.log("userId: " + userId(user)); // "userId: 42"
In the above example, is the id variable set in the userId function equivalent to setting it via var, const, or let ?
So which of the below is the equivalent function:
Var
function userId(user) {
var id = user.id
return id;
}
Let
function userId(user) {
let id = user.id
return id;
}
Const
function userId(user) {
const id = user.id
return id;
}
(Example function from MDN page linked above, jump to "Pulling fields from objects passed as function parameter)
const, because within the function you can assign another value toid.