Search code examples
javascriptclassdeclare

Error when redeclaring class in Firefox console but not in Chrome console


class Picture {
    constructor(icon) {
        this.icon = icon

    }
}

When I run this code twice in the Chrome console it doesn't throw any errors. But when I try running it twice in the Firefox console it says Uncaught SyntaxError: redeclaration of let Picture. Why do the browsers behave differently?


Solution

  • A class can't be redefined. It is actually a block scoped variable.

    If you're experimenting with code in a web browser, such as the Firefox Web Console (Tools > Web Developer > Web Console) and you 'Run' a definition of a class with the same name twice, you'll get a SyntaxError: redeclaration of let ClassName;

    MDN docs

    Redeclaration of let/const is allowed in chrome console. Run the below statement twice:

    let x = 2;
    

    Check this SO answer and this link from Chrome blog

    Redeclaration within the same console line is not allowed though.