Skip to content

Latest commit

 

History

History
252 lines (228 loc) · 24.6 KB

javascript.md

File metadata and controls

252 lines (228 loc) · 24.6 KB

JavaScript

Map and filter an array at the same time, using reduce():

const foo = [1, 2, 3, 4, 5, 6];
const bar = foo.reduce((accumulator, current) => {
  if (current % 2 === 0) {
    accumulator.push(current * 2);
  }

  return accumulator;
}, []);

bar; // [4, 8, 12]

Get a random number in a range (inclusive):

Math.floor(Math.random() * (max - min + 1)) + min
Math.floor(Math.random() * (10 - 2 + 1)) + 2 // random number between 2 and 10

Create a bookmarklet to clear browser storage (i.e. session storage and local storage):

javascript:(function(){sessionStorage.clear();localStorage.clear()}());

ES5

ES2015

ES2016

ES2017

ES2018

ES2019

ES2020

Links