undefined ? "print":"not print" what is output...?

Anand ShindeAnand Shinde
1 min read
undefined ? "print" : "not print"

is a ternary operator in JavaScript. The ternary operator works as follows:

condition ? expressionIfTrue : expressionIfFalse
  • If the condition evaluates to true, it returns expressionIfTrue.

  • If the condition evaluates to false, it returns expressionIfFalse

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.

0
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