Home > Net >  Is "" ( empty-string ) equivalent to boolean false or a false(y) value?
Is "" ( empty-string ) equivalent to boolean false or a false(y) value?

Time:04-12

Please explain a little bit on this topic. I have went through some articles but I have not satisfied with their explanations.

CodePudding user response:

The empty string ("") returns falsy. An easy way to understand this is by using the logical AND operator

The logical AND operator

If the first object is falsy, it returns that object

console.log('' && 'hello') // falsy && 'hello' --> returns falsy empty string ('')
console.log('hi' && 'hello') // truthy && 'hello' --> returns 'hello'

  1. In the first console.log() the empty string is a falsy value and so it returns the empty string.

  2. In the second console.log() the non-empty string is a truthy value and so it returns the second string hello

  • Related