Home > Mobile >  Why can't I call a variable "chrome" in javascript?
Why can't I call a variable "chrome" in javascript?

Time:08-05

In the browser console I am getting the error

Uncaught SyntaxError: Identifier 'chrome' has already been declared (at script.js:1:1)

This error only appears when the third variable is called chrome (like in the code snippet). I don't get this error, when I call the variable anything else (e.g. chromee, chrome1, etc.). Why is that? Is chrome a keyword?

This is the entire script.js:

let browserEngine = {
    name: "Blink",
    maintainer: "Chromium"
}

let jsEngine = {
    name: "V8",
    maintainer: "Chromium"
}

let chrome = {
    browserEngine: browserEngine,
    jsEngine: jsEngine
}

let brave = {
    browserEngine: browserEngine,
    jsEngine: jsEngine
}

console.log(chrome.browserEngine.name);
console.log(brave.jsEngine.name);```

CodePudding user response:

When using Google Chrome, the browser defines a variable called chrome, like how you're naming it there. In JavaScript, you can't re-declare variables.

So the variable chrome exists, and you're trying to declare your own variable called chrome which causes the error.

This is specific to Chromium-based browsers, however. If you name a variable chrome and run the code in other browsers, you'll get no errors, but since Chromium browsers declare a variable called chrome, you can't declare your own variable with the name chrome

  • Related