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.
Type cast numbers with the Number wrapper - JavaScript Tutorial
From the course: JavaScript: Best Practices for Data
Type cast numbers with the Number wrapper
- [Instructor] When you're working with data that needs to be a number, JavaScript offers a couple approaches to typecast to a number. But these approaches aren't equal. I have an array with three elements whose values are different data types and I have a forEach statement that loops through the array and uses two different methods to typecast. First, I'm using the new keyword and the Number wrapper, and then I'm using the Number wrapper all on its own. Now I have some error squiggles on lines nine and 10 and that's because my ESLint already contains the no new wrappers rule. So I can start off looking at that part with suspicion. I have the code open in my browser and so I'll check out the console. Scrolling to the top of that output my for Each statement is logging a table with the label followed by the value and the resulting data type. For the first array element, a string containing digits, new with the Number…
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)
-
-