JavaScript: Difference between revisions
Line 14: | Line 14: | ||
// then the first element will be your accumulator | // then the first element will be your accumulator | ||
// I.e. the first call to your function will be (arr[0], arr[1]) | // I.e. the first call to your function will be (arr[0], arr[1]) | ||
arr.reduce((acc, x) => acc + x, 0); //6 | arr.reduce((acc, x) => acc + x, 0); // 6 | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Revision as of 18:33, 17 December 2019
This page is a mostly about browser-based JavaScript or ECMAScript usage and interaction with the HTML DOM (window). For server and desktop application JavaScript usage, please see the NodeJS page.
Usage
Arrays
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
Canvas
Video
Regular Expressions (Regex)
var myRegex = /(\d+),(\d+)/;
var myStr = "124,52";
var match = myStr.match(myRegex);
// Captures
console.log(match[1], match[2]);
console.table(match);