This commit is contained in:
Rico Sta. Cruz 2017-03-07 16:03:11 +08:00
parent 541187c3ea
commit b8b48d5e44
No known key found for this signature in database
GPG Key ID: CAAD38AE2962619A
1 changed files with 22 additions and 0 deletions

22
extras/js-lazy.md Normal file
View File

@ -0,0 +1,22 @@
---
title: JavaScript lazy shortcuts
category: JavaScript
---
| What | Lazy mode | "The right way" |
| --- | --- | --- |
| String to number | `+str` | `parseInt(str, 10)` or `parseFloat()` |
| Math floor | `num | 0` | `Math.floor(num)` |
| Number to string | `'' + num` | `num.toString()` |
| Date to UNIX timestamp | `+new Date()` | `new Date().getTime()` |
| Any to boolean | `!!value` | `Boolean(value)` |
| Check array contents | `if (~arr.indexOf(v))` | `if (arr.contains(v))` \* |
> * = `.contains` is ES6-only, otherwise use `.indexOf(val) !== -1`
## Examples
```js
n = +'4096' // n === 4096
s = '' + 200 // s === '200'
```