undefined ? "print":"not print" what is output...?
Full-Stack Developer | JavaScript, React, Next.js, MongoDB Specialist | Building Dynamic & High-Performance Web Applications
undefined ? "print" : "not print"
is a ternary operator in JavaScript. The ternary operator works as follows:
condition ? expressionIfTrue : expressionIfFalse
If the
conditionevaluates totrue, it returnsexpressionIfTrue.If the
conditionevaluates 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.
