Move away from relying on JS truthiness
See original GitHub issueSee PR #3535 for more context.
I think we should be explicit in our conditional statements instead of relying on truthiness. This should limit gotchas such as the one referenced above.
let a: ReadonlyArray<number> | null = null
let s: string | undefined = undefined
if (a) { }
if (a != null) { } //preferred
a = [0, 1, 2, 3]
if (a.length) { }
if (a.length > 0) { } //preferred
Issue Analytics
- State:
- Created 6 years ago
- Comments:20 (20 by maintainers)
Top Results From Across the Web
Truthiness in JavaScript | Go Make Things
In JavaScript, truthiness is whether something returns true or false in an if statement. ... You can check truthiness a bunch of different...
Read more >Advanced Javascript: Logical Operators and truthy / falsy
When javascript is expecting a boolean and it's given something else, it decides whether the something else is “truthy” or “falsy”. An empty...
Read more >Is !! a best practice to check a truthy value in an if statement
No, !! is totally useless in a if condition and only confuses the reader. Values which are translated to true in !! value...
Read more >If Statements, Function Returns, Truthy and Falsy - Wes Bos
First let's go over the mechanics of if statements using greater or less than operators, and then we will dive deeper into the...
Read more >Truthy and Falsy Values: When All is Not Equal in JavaScript
Anything in JavaScript can be considered truthy or falsy. Learn what these values are and the rules that apply when they're compared.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Aha! https://palantir.github.io/tslint/rules/strict-boolean-expressions/
Closing this out since the team has started being explicit with
null
andundefined
checks.