What happens when JavaScript variable name and function name is the same?

前端 未结 5 903
南方客
南方客 2020-11-29 03:45

I have the following code, where I declare a function and after it, a variable with the same name as the function:

function a(x) {
    return x * 2;
}

var a         


        
5条回答
  •  情歌与酒
    2020-11-29 04:01

    ES6 comes with a better solution by defining SyntaxError: Identifier (?) has already been declared when using let / const instead of var.

    let

    function foo () {}
    let foo;
    // => SyntaxError: Identifier 'foo' has already been declared
    

    const

    function foo () {}
    const foo = 1;
    // => SyntaxError: Identifier 'foo' has already been declared
    

    Note that const foo; does not work. It will cause SyntaxError: Missing initializer in const declaration

提交回复
热议问题