Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to declare a variable in a ternary expression?

I need to set i depending on a condition:

let i = null
nightmode === true ? i = 1 : i = 0

Is it possible to declare i within the ternary expression, or does it have to be in outside of it (to handle scoping)?

like image 989
WoJ Avatar asked Oct 26 '25 07:10

WoJ


1 Answers

You could use the ternary directly as assignment for the value.

let i = nightmode === true ? 1 : 0;
like image 65
Nina Scholz Avatar answered Oct 28 '25 20:10

Nina Scholz