All kinds of numbers in JavaScript belong to the Number data type. Any value that can be formed using the digits 0 to 9 is considered a number in JavaScript. The number type contains both the integers as well as the decimals (sometimes called 'float' in other languages).
Have you ever thought what would happen if you subtracted a string '20' from a string '2'? Let's find out what would happen in such a case. You can explicitly convert a Number to a String type and vice-versa. This is useful in many scenarios. Let's first look into how you can convert a string to its corresponding number.
So you learnt that if you do an arithmetic operation on string, it would be converted into Number if the string is a valid number.
You also learned about NaN. NaN denotes 'Not a Number' and this represents that the output is not a valid number. You can use isNaN() function to check if the given expression evaluates to a valid number or not. If this function returns true, then the expression or literal is not a valid number. If the result is false, the expression or literal is a valid number.
Well, that was quite interesting. You learnt about the interesting functions such as .toString(), which can come in quite handy on a lot of occasions, especially when you want to convert any data type into a string.
You also saw how to instruct JavaScript to convert a string to a number explicitly. The three methods you just learnt are:
The +() is the fastest way amongst all three.