From the course: JavaScript: Best Practices for Data

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Rely on implicit Boolean values in conditionals

Rely on implicit Boolean values in conditionals - JavaScript Tutorial

From the course: JavaScript: Best Practices for Data

Start my 1-month free trial

Rely on implicit Boolean values in conditionals

- [Instructor] When you're working with a variable whose value is a Boolean in a conditional expression, it can be tempting to explicitly typecast the variable like you might with a value of a different data type to get its Boolean equivalent. My code for this video is the very beginning outline of a user interface. I'm setting a loggedIn variable to store whether the user is logged in, and then checking the value of this to determine my next step, which I'm simply logging into the console to start. Now in my condition I'm typecasting loggedIn to check whether loggedIn is equal to true. But my condition just needs a true or false value, and my loggedIn variable is definitely going to give it one or the other. So this extra step of typecasting isn't necessary. ESLint has a rule to test for this, no-extra-boolean-cast. It's enabled in the ESLint recommended rules, and by default it's flexible, watching only for Boolean…

Contents