0

If ("") results false, so why isn't (true && "") false instead of "" (empty string).

And if("string") results true, so why isn't (true && "string") true instead of 'string'.

Same goes for objects,

For example (true && {key: 'val'}) is not true, it's {key: 'val'}

Aziz.G
  • 3,599
  • 2
  • 17
  • 35
dementrix
  • 1
  • 1

1 Answers1

-2

Because it's the same as if (true) return "string" if (condition) return result.

Never matter what the result ( String, Object, Boolean ...) it will be returned if the condition is true.

also you can use that as a short hand for if (condition) return res could be

condition && res

Logical AND (&&) expr1 && expr2 If expr1 can be converted to true, returns expr2; else, returns expr1.

(() => {

  console.log(true && "string")

  if (true) {
    return "string"
  }

})()
Aziz.G
  • 3,599
  • 2
  • 17
  • 35