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

Type cast numbers with the Number wrapper - JavaScript Tutorial

From the course: JavaScript: Best Practices for Data

Start my 1-month free trial

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…

Contents