Table of contents
No headings in the article.
Sometimes we need to convert Strings
to Numbers
. The easiest and fastest way to convert strings to numbers would be using the + (plus)
operator. Let's see.
let fiveAsString = '5'
let fiveAsNumber = +fiveAsString
// Now the fiveAsNumber var contains 5
You can also use the - (minus)
operator which type-converts the value into number but also negates it.
let fiveAsString = '5'
let negativeNumberFive = -fiveAsString
// Now the negativeNumberFive var contains. -5