I am currently learning JavaScript and I'm trying to translate a code I made, from Python to Javascript.
I would like to know how I can translate an instruction like this:
my_function(arg1, arg2=True if condition else False)
I can't find this specific case on Google.
Many thanks!
Your are looking for ternary operator
, it looks like this:
const condition = true;
function(arg1, condition ? "true" : "false")
The logic behind it:
First argument (before ?) is the condition, e.g:
const age = 18
age >= 18 ? ....
The second argument (after ?) is the True
statement and after the :
is the False
statement
? "Above 18" : "Under 18"
Final result:
const age = 18;
console.log(age >= 18 ? "Above 18" : "Under 18"); // prints "Above 18"