Jslint uses '||' operator instead of conditional operator

I have jslint complaining about using the || for the code below

 query = ['browser' + (ieVersion ? ieVersion : 'UNKNOWN')] 

I tried using the || operator but the result is a wrong result

 query = ['browser' + ieVersion || 'UNKNOWN'] // => ['browserundefined'] 
+6
source share
3 answers

Operator priority is incorrect, try the following:

 query = ['browser' + (ieVersion || 'UNKNOWN')] 

without additional brackets, the + operator is stronger, and the JavaScript engine evaluates it as:

 query = [('browser' + ieVersion) || 'UNKNOWN'] 

Note that 'browser' + ieVersion never false, so you will never see 'UNKNOWN' .

+6
source

parentheses?

 query = ['browser' + (ieVersion || 'UNKNOWN')] 
+2
source

You need to enclose the expression in parentheses:

 query = ['browser' + (ieVersion || 'UNKNOWN')] 
+2
source

All Articles