|| [] is not valid JavaScript [duplicate]

Clash Royale CLAN TAG#URR8PPP
up vote
17
down vote
favorite
This question already has an answer here:
When does JS interpret as an empty block instead of an empty object?
2 answers
Why is 0 === okay, but === 0 throws an error? [duplicate]
1 answer
Why is || not valid?
$ echo ' || ' | node # this works
$ echo ' || ' | node # but this doesn't
[stdin]:1
||
^^
SyntaxError: Unexpected token ||
at createScript (vm.js:80:10)
at Object.runInThisContext (vm.js:139:10)
at Object.<anonymous> ([stdin]-wrapper:6:22)
at Module._compile (module.js:652:30)
at evalScript (bootstrap_node.js:466:27)
at Socket.<anonymous> (bootstrap_node.js:237:15)
at emitNone (events.js:111:20)
at Socket.emit (events.js:208:7)
at endReadableNT (_stream_readable.js:1064:12)
at _combinedTickCallback (internal/process/next_tick.js:138:11)
$ echo '() || ' | node # unless you do this
javascript node.js
marked as duplicate by JJJ, jpmc26, rsjaffe, Rafael, user2357112 Sep 22 at 2:02
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |Â
up vote
17
down vote
favorite
This question already has an answer here:
When does JS interpret as an empty block instead of an empty object?
2 answers
Why is 0 === okay, but === 0 throws an error? [duplicate]
1 answer
Why is || not valid?
$ echo ' || ' | node # this works
$ echo ' || ' | node # but this doesn't
[stdin]:1
||
^^
SyntaxError: Unexpected token ||
at createScript (vm.js:80:10)
at Object.runInThisContext (vm.js:139:10)
at Object.<anonymous> ([stdin]-wrapper:6:22)
at Module._compile (module.js:652:30)
at evalScript (bootstrap_node.js:466:27)
at Socket.<anonymous> (bootstrap_node.js:237:15)
at emitNone (events.js:111:20)
at Socket.emit (events.js:208:7)
at endReadableNT (_stream_readable.js:1064:12)
at _combinedTickCallback (internal/process/next_tick.js:138:11)
$ echo '() || ' | node # unless you do this
javascript node.js
marked as duplicate by JJJ, jpmc26, rsjaffe, Rafael, user2357112 Sep 22 at 2:02
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
2
block statement developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/â¦
â epascarello
Sep 21 at 18:08
add a comment |Â
up vote
17
down vote
favorite
up vote
17
down vote
favorite
This question already has an answer here:
When does JS interpret as an empty block instead of an empty object?
2 answers
Why is 0 === okay, but === 0 throws an error? [duplicate]
1 answer
Why is || not valid?
$ echo ' || ' | node # this works
$ echo ' || ' | node # but this doesn't
[stdin]:1
||
^^
SyntaxError: Unexpected token ||
at createScript (vm.js:80:10)
at Object.runInThisContext (vm.js:139:10)
at Object.<anonymous> ([stdin]-wrapper:6:22)
at Module._compile (module.js:652:30)
at evalScript (bootstrap_node.js:466:27)
at Socket.<anonymous> (bootstrap_node.js:237:15)
at emitNone (events.js:111:20)
at Socket.emit (events.js:208:7)
at endReadableNT (_stream_readable.js:1064:12)
at _combinedTickCallback (internal/process/next_tick.js:138:11)
$ echo '() || ' | node # unless you do this
javascript node.js
This question already has an answer here:
When does JS interpret as an empty block instead of an empty object?
2 answers
Why is 0 === okay, but === 0 throws an error? [duplicate]
1 answer
Why is || not valid?
$ echo ' || ' | node # this works
$ echo ' || ' | node # but this doesn't
[stdin]:1
||
^^
SyntaxError: Unexpected token ||
at createScript (vm.js:80:10)
at Object.runInThisContext (vm.js:139:10)
at Object.<anonymous> ([stdin]-wrapper:6:22)
at Module._compile (module.js:652:30)
at evalScript (bootstrap_node.js:466:27)
at Socket.<anonymous> (bootstrap_node.js:237:15)
at emitNone (events.js:111:20)
at Socket.emit (events.js:208:7)
at endReadableNT (_stream_readable.js:1064:12)
at _combinedTickCallback (internal/process/next_tick.js:138:11)
$ echo '() || ' | node # unless you do this
This question already has an answer here:
When does JS interpret as an empty block instead of an empty object?
2 answers
Why is 0 === okay, but === 0 throws an error? [duplicate]
1 answer
javascript node.js
javascript node.js
edited Sep 21 at 18:08
asked Sep 21 at 18:07
charmoniumQ
1,90231727
1,90231727
marked as duplicate by JJJ, jpmc26, rsjaffe, Rafael, user2357112 Sep 22 at 2:02
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by JJJ, jpmc26, rsjaffe, Rafael, user2357112 Sep 22 at 2:02
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
2
block statement developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/â¦
â epascarello
Sep 21 at 18:08
add a comment |Â
2
block statement developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/â¦
â epascarello
Sep 21 at 18:08
2
2
block statement developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/â¦
â epascarello
Sep 21 at 18:08
block statement developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/â¦
â epascarello
Sep 21 at 18:08
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
21
down vote
accepted
When a statement starts with {, it's assumed by the parser to be the beginning of a block statement. In the case of , it's an empty block statement. So it's as if you had
// no code here
||
and || cannot start a statement.
The one that does work, || , is unambiguous because a statement whose first token is [ can only be an expression statement.
Wrapping in ( ) means that the first token of the statement is (, not {. The ( token cannot start any form of statement other than an expression (though it does have a little ambiguity, since it can start an anonymous "fat arrow" function; that's still an expression and the parser just has to disambiguate that later).
Note: the implementation of various debugging environments like your browser console and the Node command-line "console" have an effect on this sort of syntax. In order to keep things simple, such tools take the code you type in and one way or another they "wrap" it so that it can be parsed and evaluated interactively, statement by statement as you type. Unfortunately that process can introduce anomalies, such that something you try in the console may work fine there but not when you try it in a real block of code.
1
To me this doesn't explain why||is allowed.
â Explosion Pills
Sep 21 at 18:10
3
In node and Chrome, running||returns
â Explosion Pills
Sep 21 at 18:12
4
@ExplosionPills It works in the console but not as a propper script. Wrap||in a<script>tag and run it in the browser and you'll get the error. Node throws the same error too if run from a script. I guess the console works in mysterious ways so it can't be relied on to judge this behaviour.
â ibrahim mahrir
Sep 21 at 18:23
6
@ExplosionPills This is specific to how REPL works in Chrome and Node, not anything else.
â estus
Sep 21 at 18:31
1
@estus That is a hack in the devtools, not because it is valid syntax. And the hack only works one||but not on||.
â Bergi
Sep 21 at 21:42
 |Â
show 8 more comments
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
21
down vote
accepted
When a statement starts with {, it's assumed by the parser to be the beginning of a block statement. In the case of , it's an empty block statement. So it's as if you had
// no code here
||
and || cannot start a statement.
The one that does work, || , is unambiguous because a statement whose first token is [ can only be an expression statement.
Wrapping in ( ) means that the first token of the statement is (, not {. The ( token cannot start any form of statement other than an expression (though it does have a little ambiguity, since it can start an anonymous "fat arrow" function; that's still an expression and the parser just has to disambiguate that later).
Note: the implementation of various debugging environments like your browser console and the Node command-line "console" have an effect on this sort of syntax. In order to keep things simple, such tools take the code you type in and one way or another they "wrap" it so that it can be parsed and evaluated interactively, statement by statement as you type. Unfortunately that process can introduce anomalies, such that something you try in the console may work fine there but not when you try it in a real block of code.
1
To me this doesn't explain why||is allowed.
â Explosion Pills
Sep 21 at 18:10
3
In node and Chrome, running||returns
â Explosion Pills
Sep 21 at 18:12
4
@ExplosionPills It works in the console but not as a propper script. Wrap||in a<script>tag and run it in the browser and you'll get the error. Node throws the same error too if run from a script. I guess the console works in mysterious ways so it can't be relied on to judge this behaviour.
â ibrahim mahrir
Sep 21 at 18:23
6
@ExplosionPills This is specific to how REPL works in Chrome and Node, not anything else.
â estus
Sep 21 at 18:31
1
@estus That is a hack in the devtools, not because it is valid syntax. And the hack only works one||but not on||.
â Bergi
Sep 21 at 21:42
 |Â
show 8 more comments
up vote
21
down vote
accepted
When a statement starts with {, it's assumed by the parser to be the beginning of a block statement. In the case of , it's an empty block statement. So it's as if you had
// no code here
||
and || cannot start a statement.
The one that does work, || , is unambiguous because a statement whose first token is [ can only be an expression statement.
Wrapping in ( ) means that the first token of the statement is (, not {. The ( token cannot start any form of statement other than an expression (though it does have a little ambiguity, since it can start an anonymous "fat arrow" function; that's still an expression and the parser just has to disambiguate that later).
Note: the implementation of various debugging environments like your browser console and the Node command-line "console" have an effect on this sort of syntax. In order to keep things simple, such tools take the code you type in and one way or another they "wrap" it so that it can be parsed and evaluated interactively, statement by statement as you type. Unfortunately that process can introduce anomalies, such that something you try in the console may work fine there but not when you try it in a real block of code.
1
To me this doesn't explain why||is allowed.
â Explosion Pills
Sep 21 at 18:10
3
In node and Chrome, running||returns
â Explosion Pills
Sep 21 at 18:12
4
@ExplosionPills It works in the console but not as a propper script. Wrap||in a<script>tag and run it in the browser and you'll get the error. Node throws the same error too if run from a script. I guess the console works in mysterious ways so it can't be relied on to judge this behaviour.
â ibrahim mahrir
Sep 21 at 18:23
6
@ExplosionPills This is specific to how REPL works in Chrome and Node, not anything else.
â estus
Sep 21 at 18:31
1
@estus That is a hack in the devtools, not because it is valid syntax. And the hack only works one||but not on||.
â Bergi
Sep 21 at 21:42
 |Â
show 8 more comments
up vote
21
down vote
accepted
up vote
21
down vote
accepted
When a statement starts with {, it's assumed by the parser to be the beginning of a block statement. In the case of , it's an empty block statement. So it's as if you had
// no code here
||
and || cannot start a statement.
The one that does work, || , is unambiguous because a statement whose first token is [ can only be an expression statement.
Wrapping in ( ) means that the first token of the statement is (, not {. The ( token cannot start any form of statement other than an expression (though it does have a little ambiguity, since it can start an anonymous "fat arrow" function; that's still an expression and the parser just has to disambiguate that later).
Note: the implementation of various debugging environments like your browser console and the Node command-line "console" have an effect on this sort of syntax. In order to keep things simple, such tools take the code you type in and one way or another they "wrap" it so that it can be parsed and evaluated interactively, statement by statement as you type. Unfortunately that process can introduce anomalies, such that something you try in the console may work fine there but not when you try it in a real block of code.
When a statement starts with {, it's assumed by the parser to be the beginning of a block statement. In the case of , it's an empty block statement. So it's as if you had
// no code here
||
and || cannot start a statement.
The one that does work, || , is unambiguous because a statement whose first token is [ can only be an expression statement.
Wrapping in ( ) means that the first token of the statement is (, not {. The ( token cannot start any form of statement other than an expression (though it does have a little ambiguity, since it can start an anonymous "fat arrow" function; that's still an expression and the parser just has to disambiguate that later).
Note: the implementation of various debugging environments like your browser console and the Node command-line "console" have an effect on this sort of syntax. In order to keep things simple, such tools take the code you type in and one way or another they "wrap" it so that it can be parsed and evaluated interactively, statement by statement as you type. Unfortunately that process can introduce anomalies, such that something you try in the console may work fine there but not when you try it in a real block of code.
edited Sep 21 at 22:01
answered Sep 21 at 18:09
Pointy
308k43444509
308k43444509
1
To me this doesn't explain why||is allowed.
â Explosion Pills
Sep 21 at 18:10
3
In node and Chrome, running||returns
â Explosion Pills
Sep 21 at 18:12
4
@ExplosionPills It works in the console but not as a propper script. Wrap||in a<script>tag and run it in the browser and you'll get the error. Node throws the same error too if run from a script. I guess the console works in mysterious ways so it can't be relied on to judge this behaviour.
â ibrahim mahrir
Sep 21 at 18:23
6
@ExplosionPills This is specific to how REPL works in Chrome and Node, not anything else.
â estus
Sep 21 at 18:31
1
@estus That is a hack in the devtools, not because it is valid syntax. And the hack only works one||but not on||.
â Bergi
Sep 21 at 21:42
 |Â
show 8 more comments
1
To me this doesn't explain why||is allowed.
â Explosion Pills
Sep 21 at 18:10
3
In node and Chrome, running||returns
â Explosion Pills
Sep 21 at 18:12
4
@ExplosionPills It works in the console but not as a propper script. Wrap||in a<script>tag and run it in the browser and you'll get the error. Node throws the same error too if run from a script. I guess the console works in mysterious ways so it can't be relied on to judge this behaviour.
â ibrahim mahrir
Sep 21 at 18:23
6
@ExplosionPills This is specific to how REPL works in Chrome and Node, not anything else.
â estus
Sep 21 at 18:31
1
@estus That is a hack in the devtools, not because it is valid syntax. And the hack only works one||but not on||.
â Bergi
Sep 21 at 21:42
1
1
To me this doesn't explain why
|| is allowed.â Explosion Pills
Sep 21 at 18:10
To me this doesn't explain why
|| is allowed.â Explosion Pills
Sep 21 at 18:10
3
3
In node and Chrome, running
|| returns â Explosion Pills
Sep 21 at 18:12
In node and Chrome, running
|| returns â Explosion Pills
Sep 21 at 18:12
4
4
@ExplosionPills It works in the console but not as a propper script. Wrap
|| in a <script> tag and run it in the browser and you'll get the error. Node throws the same error too if run from a script. I guess the console works in mysterious ways so it can't be relied on to judge this behaviour.â ibrahim mahrir
Sep 21 at 18:23
@ExplosionPills It works in the console but not as a propper script. Wrap
|| in a <script> tag and run it in the browser and you'll get the error. Node throws the same error too if run from a script. I guess the console works in mysterious ways so it can't be relied on to judge this behaviour.â ibrahim mahrir
Sep 21 at 18:23
6
6
@ExplosionPills This is specific to how REPL works in Chrome and Node, not anything else.
â estus
Sep 21 at 18:31
@ExplosionPills This is specific to how REPL works in Chrome and Node, not anything else.
â estus
Sep 21 at 18:31
1
1
@estus That is a hack in the devtools, not because it is valid syntax. And the hack only works one
|| but not on || .â Bergi
Sep 21 at 21:42
@estus That is a hack in the devtools, not because it is valid syntax. And the hack only works one
|| but not on || .â Bergi
Sep 21 at 21:42
 |Â
show 8 more comments
2
block statement developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/â¦
â epascarello
Sep 21 at 18:08