Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Why does ESLint throw a Parsing error: Unexpected token , after a variable declaration on this line of code?

const a, b, c = 1;

My .eslintrc.json looks like this;

{

    "env": {
        "browser": true,
        "es6": true,
        "jquery": true,
        "commonjs": true
    },
    "extends": [
        "airbnb-base",
        "prettier"
    ],
    "parserOptions": {
        "ecmaVersion": 6,
        "sourceType": "script"
    },
    "plugins": ["prettier"],
    "rules": {
        "prettier/prettier": "error",
        "semi": ["error", "always"],
        "quotes": ["error", "double"]
    }

}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
792 views
Welcome To Ask or Share your Answers For Others

1 Answer

You cannot do that in JS.

If multiple variables listed in a variable declaration (either var, let or const), each value has its own initializer, so it's impossible to assign a single value to multiple variables in a single declaration statement, without repetition.

If an initializer is missing (no = value part present), the variable will be set to undefined.

So if you used let, then c would become 1, while a and b would be undefined:

let a, b, c = 1;

console.log(a, b, c) //undefined undefined 1

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...