question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Move away from relying on JS truthiness

See original GitHub issue

See 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:closed
  • Created 6 years ago
  • Comments:20 (20 by maintainers)

github_iconTop GitHub Comments

1reaction
iAmWillShepherdcommented, May 25, 2018

Closing this out since the team has started being explicit with null and undefined checks.

Read more comments on GitHub >

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

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found