-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?

1
  • 5
    Empty string is a falsey value. Commented Apr 25, 2018 at 10:35

2 Answers 2

2

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

if(false){console.log("Called")} 
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.