Yonle on Nostr: Well, in switch, if you are declaring it without bracket, that equals to declaring it ...
Well, in switch, if you are declaring it without bracket, that equals to declaring it <globally> 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.
Published at
2024-05-14 01:55:37Event JSON
{
"id": "b657dcbbf21736fedb6a5b27f48d4defe5c997faedc9b3f63ec5e3746956b67a",
"pubkey": "347a2370900d19b4e4756221594e8bda706ae5c785de09e59e4605f91a03f49c",
"created_at": 1715644537,
"kind": 1,
"tags": [
[
"e",
"1e86f5e215c61fa1f468228ef5bf33c294acdd197206d33b4c32abbd8768961f",
"",
"root"
],
[
"e",
"0d1d3331d22dfa02aa8710f09643d3156c5b1e59b826c85653ba24550094de23",
"",
"reply"
],
[
"p",
"347a2370900d19b4e4756221594e8bda706ae5c785de09e59e4605f91a03f49c"
],
[
"p",
"69c8971d2ad8c30a72bfc719a9941383205e7e0b274365dac90bdcccdf20ed5d"
]
],
"content": "Well, in switch, if you are declaring it without bracket, that equals to declaring it \u003cglobally\u003e into your function, which then creates an error of \"Identifies \"...\" has already been declared\".\n\nBecause doing this:\n```\nfunction () {\n switch (...) {\n case \"1\":\n let a = 1;\n // ....\n break;\n }\n}\n```\n\nIs technically almost the same as the following:\n\n```\nfunction () {\n let a = 1;\n}\n```\n\nWhile it may seems normal, The problem will happen with a code that similar like the following:\n\n```\nfunction () {\n switch (...) {\n case \"1\":\n let a = 1;\n // ....\n break;\n case \"2\":\n let a = 2;\n // ....\n break;\n }\n}\n```\n\nYou will then get the error `Uncaught SyntaxError: Identifier 'a' has already been declared`.\n\nTo fix it, simply put bracket.",
"sig": "24ab05db19160e10989e06dee78ee017847c72b5572749c180ecdd3a5560c38f7e45f363863039887c10e6b48f9c600e2ce0f9c2a39adfb06e4f2d690800cb2f"
}