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.
Subscribe to my newsletter
Read articles from Anand Shinde directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Anand Shinde
Anand Shinde
Full-Stack Developer | JavaScript, React, Next.js, MongoDB Specialist | Building Dynamic & High-Performance Web Applications