Skip to content

Commit

Permalink
Update parse-string.md
Browse files Browse the repository at this point in the history
  • Loading branch information
Kikobeats committed Jun 6, 2017
1 parent a31dea8 commit fbb510d
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions docs/number/parse-string.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

There is a lot of ways to convert a `String` into a `Number`:

| | '1000' | '10.9' | '1,000.9' | '011' | '10c' | '$10' |
|-------------------|--------|--------|-----------|-------|-------|-------|
| | '1000' | '10.9' | '1,000.9' | '011' | '10c' | '$10' |
|---------------------|--------|--------|-----------|-------|-------|-------|
| `parseInt(num)`     | 1000   | 10     | 1         | 11   | 10   | NaN   |
| `parseInt(num, 10)` | 1000   | 10     | 1         | 11   | 10   | NaN   |
| `parseFloat(num)`   | 1000   | 10.9   | 1         | 11   | 10   | NaN   |
| `Number(num)`       | 1000   | 10.9   | NaN       | 11   | NaN   | NaN   |
| `~~num`            | 1000   | 10     | 0         | 11   | 0     | 0     |
| `~~num`            | 1000   | 10     | 0         | 11   | 0     | 0     |
| `num / 1`           | 1000   | 10.9   | NaN       | 11   | NaN   | NaN   |
| `num * 1`           | 1000   | 10.9   | NaN       | 11   | NaN   | NaN   |
| `num - 0`           | 1000   | 10.9   | NaN       | 11   | NaN   | NaN   |
Expand All @@ -18,14 +18,16 @@ Despite the differences of the results, it does not matter too much the method u

`+num` is the shortest way and probably used in JavaScript minification tools.

Probably use `Number` constructor is enough.
Use `Number(num)` constructor should be enough evidence of your intention.

If you wan to use one of the tricky ways, keep in mind that the intention is not always obvious.
If you want to use one of the tricky ways, keep in mind that the intention is not always obvious.

A solution could be involve them in a named function to make it evident:
A solution could be wrap them into a named function:

```js
const toNumber = num => ~~num
console.log(toNumber('10.9')
// => 10.9
```
but now you are not earning anything you can not do already with `Number(num)`.

0 comments on commit fbb510d

Please sign in to comment.