Jump to content

JavaScript: Difference between revisions

447 bytes added ,  22 January 2020
no edit summary
No edit summary
Line 3: Line 3:


==Usage==
==Usage==
===Arrays===
<syntaxhighlight lang="javascript">
let arr = [1,2,3];
// Map
arr.map(x => x > 2); // [false, false, true]
// Reduce or Fold
// Note that if you do not provide an initial accumulator,
// then the first element will be your accumulator
// I.e. the first call to your function will be (arr[0], arr[1])
arr.reduce((acc, x) => acc + x, 0); // 6
</syntaxhighlight>
===Canvas===
===Canvas===
===Video===
===Video===
Line 88: Line 74:
</syntaxhighlight>
</syntaxhighlight>


==Data Structures==
JavaScript traditionally has arrays and objects (hashmap) data structures.<br>
ES2015 (ES6) adds several collections including: Map, Set, WeakMap, WeakSet
===Arrays===
<syntaxhighlight lang="javascript">
let arr = [1,2,3];
// Map
arr.map(x => x > 2); // [false, false, true]
// Reduce or Fold
// Note that if you do not provide an initial accumulator,
// then the first element will be your accumulator
// I.e. the first call to your function will be (arr[0], arr[1])
arr.reduce((acc, x) => acc + x, 0); // 6
</syntaxhighlight>
===Objects===
Objects are maps in JavaScript. They are typically implemented as hashmaps by the JS engine.<br>
Note that you can only use numbers and strings as keys.
<syntaxhighlight lang="javascript">
let my_map = {};
my_map["my_key"] = "my_value";
</syntaxhighlight>


==Modules==
==Modules==