Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does the "condition" (? :) operator work in JavaScript as in C?

Example:

var x, y, z;
//...
x > 100 ? y = x : z = x ;
like image 396
Giffyguy Avatar asked Sep 10 '26 20:09

Giffyguy


1 Answers

Yes it does work the same although operator precedence is always tricky so I would recommend parenthesis to avoid confusion.

tangentially related..
You can also use || in JavaScript similar to the null coalescing operator ?? in C#

like image 180
Quintin Robinson Avatar answered Sep 14 '26 00:09

Quintin Robinson