undefined ? "print":"not print" what is output...?
undefined ? "print" : "not print"
is a ternary operator in JavaScript. The ternary operator works as follows:
condition ? expressionIfTrue : expressionIfFalse
If the
condition
evaluates totrue
, it returnsexpressionIfTrue
.If the
condition
evaluates tofalse
, it returnsexpressionIfFalse
Explanation:
The condition is undefined
:
In JavaScript, undefined
is a falsy value. This means it is treated as false
in boolean contexts.
What happens in the ternary operator?:
The condition undefined
evaluates to false
.
Since the condition is false
, the operator will return the second expression, which is "not print"
Output:
"not print"
This is because undefined
is falsy, and the ternary operator selects the expression after the colon (:
) when the condition is false.