课程: JavaScript: Best Practices for Data

今天就学习课程吧!

今天就开通帐号,24,700 门业界名师课程任您挑!

Rely on implicit Boolean values in conditionals

Rely on implicit Boolean values in conditionals - JavaScript教程

课程: JavaScript: Best Practices for Data

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…

内容