Skip to content

Instantly share code, notes, and snippets.

@aloisdg
Last active December 19, 2019 08:43
Show Gist options
  • Save aloisdg/930425653ef02f232c48418f46cec4ae to your computer and use it in GitHub Desktop.
Save aloisdg/930425653ef02f232c48418f46cec4ae to your computer and use it in GitHub Desktop.

JS Oddities

Map take all

[[]].map(Array)

// ['10','10','10','10','10'].map(parseInt)
// [10, NaN, 2, 3, 4]

https://stackoverflow.com/q/14528397/1248177

Indent

function a() {
  return {
    "name": "foo"
  }
}

function b()
{
  return
  {
    "name": "foo"
  }
}

We all float down there

3.toString() => ';' attendu
(3).toString() => "3"
3..toString() => "3"

JavaScript “new Array(n)” and “Array.prototype.map” weirdness

 >>> x = new Array(3)
[undefined, undefined, undefined]
>>> y = [undefined, undefined, undefined]
[undefined, undefined, undefined]

>>> x.constructor == y.constructor // ou ===
true

>>> x.map(() => 0)
[undefined, undefined, undefined]
>>> y.map(() => 0)
[0, 0, 0]

https://stackoverflow.com/q/5501581/1248177

null >= 0

null > 0 false
null == 0 false
null >= 0 true

autocast string

'2' + '2' - '2' = 20
- '2' + '2' + '2' = -222
'2' - '2' + '2' = 02

autocast array

output of ([true, true] + [1]).length ? expected 3 (true,true,1]).length got 10 because "true,true1".length ...

multi line

var str = function(){/* Is it madness? */}.toString().slice(14,-4)

https://www.reddit.com/r/ProgrammerHumor/comments/9nrkbm/a_beautifully_hacky_way_to_do_a_multiline_string/

https://stackoverflow.com/questions/805107/creating-multiline-strings-in-javascript/15558082#15558082

https://github.com/npm/cli/blob/4c65cd952bc8627811735bea76b9b110cc4fc80e/test/need-npm5-update/peer-deps-invalid.js

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment