Node: Ternary operator scope

Created on 18 Mar 2020  路  3Comments  路  Source: nodejs/node

  • Version: 12.16.1, 12.9.1, 8.9.1 (as tested on-hand, it's probably in all versions)
  • Platform: Mac OSX

What steps will reproduce the bug?

console.log('comment out the scoped object below, and this will')
{}
(false) ? null : console.log('break');

Further:

console.log('remove the brackets from the ternary operator, and it will')
//{}
false ? null : console.log('work again');

How often does it reproduce?

Every time.

Most helpful comment

Without a semicolon, the first code is parsed as console.log('...')(false), so TypeError: console.log(...) is not a function is expected error.

All 3 comments

Without a semicolon, the first code is parsed as console.log('...')(false), so TypeError: console.log(...) is not a function is expected error.

Ok, thanks - that makes sense.

It's generally understood that forgoing the use of semi-colons, is accepted as a valid coding style in node/javascript (whether any particular person agrees it's for them or not...). As I previously understood it, the exception was the ternary operator, which must be terminated with a semicolon. But looking a little deeper it seems this is not the case, there are many exceptions.

This is therefore an issue with Javascript rather than node. Closing.

If this issue finds anyone else, this article was helpful, specifically at the end when it suggests the following rule when using the "no semi-colon" style:

  • never start a line with parentheses, those might be concatenated with the previous line to form a function call, or array element reference
Was this page helpful?
0 / 5 - 0 ratings

Related issues

Icemic picture Icemic  路  3Comments

willnwhite picture willnwhite  路  3Comments

mcollina picture mcollina  路  3Comments

fanjunzhi picture fanjunzhi  路  3Comments

seishun picture seishun  路  3Comments