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 strings with the String wrapper

Type cast strings with the String wrapper - JavaScript Tutorial

From the course: JavaScript: Best Practices for Data

Start my 1-month free trial

Type cast strings with the String wrapper

- [Instructor] I'm modeling working with data whose data type I can't be certain of. My array has a single element right now with the numeric value 55. If I want to work with that data as a string, there are a few approaches I can take. I have a forEach statement here that loops through my array, just that one element for now, and tries out three of those approaches. new String using the new keyword and the string wrapper, then the toString method, and then the string wrapper on its own. I'm using the console.table method here which can take complex data, like this set of nested objects, and display in the console in the table format which can make it easier to understand. I'm running my code using Live Server so in the browser console, there's that table, and it shows a label for each of the three approaches followed by the value and the resulting data type. With the new String statement, I get a data type of…

Contents