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)?
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)?
-
2
Your ternary is using a side effect instead of how it was designed to be used.
let i = nightmode === true ? 1 : 0
– mplungjan Commented Dec 16, 2018 at 14:59
2 Answers
Reset to default 5You could use the ternary directly as assignment for the value.
let i = nightmode === true ? 1 : 0;
I think your variable i
needs to be outside of it, although it is possible to set i
in the following manner:
let nightmode = true;
let i = (nightmode === true) ? 1 : 0
console.log(i);