Is there a difference between `(i === true)` and `(i)`?

Viewed 58

I'm messing with code that someone else wrote and I saw that he writes like this

if(i === true)

Is there a difference if I do it this way?

if(i)
2 Answers

The first if statement is checking for exact equality, and will only run if i was exactly equal to true.

The second one is checking for whether i is truthy, which means the if statement would still run if i was other things such as null, undefined, or an empty string.

https://developer.mozilla.org/en-US/docs/Glossary/Truthy

Using the comparison operator will return true if i is boolean true, but just having i will return true for any truthy value I.E: positive integers, non-empty strings, etc

Related