From the course: JavaScript: Best Practices for Data

Unlock the full course today

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

Type cast Booleans using double negation

Type cast Booleans using double negation - JavaScript Tutorial

From the course: JavaScript: Best Practices for Data

Start my 1-month free trial

Type cast Booleans using double negation

- [Instructor] When you're working with data that needs to be a Boolean, you can typecast it a few different ways. As with typecasting in general in JavaScript, not all approaches are equal. I have an array with three elements whose values are different data types, and I have a for each statement that loops through the array and uses three different methods to typecast. New Boolean uses the new keyword and the Boolean wrapper, then the Boolean wrapper on its own, and finally, double negation, which determines whether a value is truthy or falsy, gets its opposite, and then gets the opposite of that, which is the Boolean equivalent of the original value. Now, I have some error squiggles on lines nine and 10, and that's because my .eslintrc file already contains the no new wrappers rule. There's probably a reason that's suspect, but it pays to check, so I'll test it. I have the code running in Live Server. So over in my…

Contents