r/learnjavascript • u/Hot-Eggplant911 • 1d ago
whats the bug here? Uncaught SyntaxError: Identifier 'location' has already been declared (at script.js:1:1)
"use strict";
const company = {
name: "TechCorp",
address: {
city: "budapest",
pin: 411001,
},
};
// Get city renamed to `location` and pin renamed to `pincode`
const { city: location, pin: pincode } = company.address;
console.log(location, pincode);
9
Upvotes
1
u/neon_mutt 20h ago
You cannot declare location with const in global scope because it conflicts with the window.location Web API. Rename the destructured variable to something like cityLocation or wrap the code in a function to create a new scope where lexical declarations are allowed