Replying to Avatar Yonle

Well, in switch, if you are declaring it without bracket, that equals to declaring it into your function, which then creates an error of "Identifies "..." has already been declared".

Because doing this:

```

function () {

switch (...) {

case "1":

let a = 1;

// ....

break;

}

}

```

Is technically almost the same as the following:

```

function () {

let a = 1;

}

```

While it may seems normal, The problem will happen with a code that similar like the following:

```

function () {

switch (...) {

case "1":

let a = 1;

// ....

break;

case "2":

let a = 2;

// ....

break;

}

}

```

You will then get the error `Uncaught SyntaxError: Identifier 'a' has already been declared`.

To fix it, simply put bracket.

Avatar
Lamp 1y ago

nostr:npub1x3azxuysp5vmfer4vgs4jn5tmfcx4ew8sh0qnev7gczljxsr7jwqa3g4el switch itself isn't a block?

well, i changed it to var.

Reply to this note

Please Login to reply.

Discussion

Avatar
Yonle 1y ago

Still a block. But doing without bracket is quite similar to this:

```

switch {

let a = 1;

let a = 2; // the error arrives here

case "1":

// ....

}

```

Does not matter if you use let, var, or const tho.

Avatar
Yonle 1y ago

This is basically how JavaScript object works. This is common.

Thread collapsed
Thread collapsed