-2

I have tried 2 conditions in JavaScript and output:

if(""){console.log("Called")} //No Output

if("_"){console.log("Called")} //Output: Called

What could be the possible reason for this?

2 Answers2

2

The empty string is considered as a 'falsy' value, and so it's equivalent to doing:

if(false){console.log("Called")} 
Colin Ricardo
  • 15,150
  • 9
  • 42
  • 74
0

The empty string is equal to False if you cast it to Boolean (Type Conversion).

console.log(Boolean("")) //output: false
console.log(Boolean("somestring")) //output: true
Tuğca Eker
  • 1,463
  • 13
  • 20