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 - JavaScript Tutorial
From the course: 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…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
-
-
(Locked)
Type cast strings with the String wrapper2m 58s
-
(Locked)
Type cast numbers with the Number wrapper1m 40s
-
(Locked)
Type cast Booleans using double negation2m 14s
-
(Locked)
Rely on implicit Boolean values in conditionals1m 45s
-
(Locked)
Challenge: Type cast1m 7s
-
(Locked)
Solution: Type cast2m 39s
-
(Locked)
-
-