Numbers within strings (#20969)

* Numbers within strings

Added a short entry on how numbers function inside of strings as well as how to convert a number inside of a string into a number which you can perform operations on in a normal fashion.

* Change a string in the example to a number
This commit is contained in:
absentMindedDeveloper
2018-11-05 15:03:52 -08:00
committed by Huyen Nguyen
parent 857628fa23
commit 7530f98750

View File

@ -68,6 +68,18 @@ foo.toFixed(2); // "47.69"
```
>Type `Number.prototype` in your browser and see other available methods yourself.
Numbers in strings are treated differently than normal numbers.
```javascript
var foo = "12" + 18; // "1218"
```
In order to convert a string into a number you must run it through a ```Number()``` function.
```javascript
var foo = "12";
var bar = Number(foo) + 18; // "30"
```
#### More Information:
1. <a href='https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type' target='_blank' rel='nofollow'>MDN</a>
2. <a href='https://www.w3schools.com/js/js_numbers.asp' target='_blank' rel='nofollow'>JavaScript Numbers</a>