Curated collection of useful Javascript snippets that you can understand in 30 seconds or less.
- Use Ctrl + F or command + F to search for a snippet.
- Contributions welcome, please read the contribution guide.
- Snippets are written in ES6, use the Babel transpiler to ensure backwards-compatibility.
- Array concatenation
- Array difference
- Array intersection
- Array union
- Average of array of numbers
- Chunk array
- Compact
- Count occurrences of a value in array
- Deep flatten array
- Drop elements in array
- Fill array
- Filter out non unique values in an array
- Flatten array up to depth
- Flatten array
- Get max value from array
- Get min value from array
- Group by
- Head of list
- Initial of list
- Initialize array with range
- Initialize array with values
- Last of list
- Median of array of numbers
- Nth element of array
- Pick
- Shuffle array
- Similarity between arrays
- Sum of array of numbers
- Tail of list
- Take
- Unique values of array
- Bottom visible
- Current URL
- Element is visible in viewport
- Get scroll position
- Redirect to URL
- Scroll to top
- Collatz algorithm
- Distance between two points
- Divisible by number
- Even or odd number
- Factorial
- Fibonacci array generator
- Greatest common divisor (GCD)
- Hamming distance
- Percentile
- Powerset
- Round number to n digits
- Standard deviation
- Anagrams of string (with duplicates)
- Capitalize first letter of every word
- Capitalize first letter
- Check for palindrome
- Reverse a string
- Sort characters in string (alphabetical)
- Truncate a string
- Escape regular expression
- Get native type of value
- Is array
- Is boolean
- Is function
- Is number
- Is string
- Is symbol
- Measure time taken by function
- Number to array of digits
- Ordinal suffix of number
- Random integer in range
- Random number in range
- RGB to hexadecimal
- Swap values of two variables
- URL parameters
- UUID generator
- Validate email
- Validate number
- Value or default
- Use
Array.concat()
to concatenate an array with any additional arrays and/or values, specified inargs
. - ES6
const arrayConcat = (arr, ...args) => arr.concat(...args);
// arrayConcat([1], 2, [3], [[4]]) -> [1,2,3,[4]]
- ES5
var arrayConcat = function arrayConcat(arr) {
for (var _len = arguments.length, args = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
args[_key - 1] = arguments[_key];
}
return arr.concat.apply(arr, args);
};
// arrayConcat([1], 2, [3], [[4]]) -> [1,2,3,[4]]
- Create a
Set
fromb
, then useArray.filter()
ona
to only keep values not contained inb
. - ES6
const difference = (a, b) => { const s = new Set(b); return a.filter(x => !s.has(x)); };
// difference([1,2,3], [1,2]) -> [3]
- ES5
var difference = function difference(a, b) {
var s = new Set(b);return a.filter(function (x) {
return !s.has(x);
});
};
// difference([1,2,3], [1,2]) -> [3]
- Create a
Set
fromb
, then useArray.filter()
ona
to only keep values contained inb
. - ES6
const intersection = (a, b) => { const s = new Set(b); return a.filter(x => s.has(x)); };
// intersection([1,2,3], [4,3,2]) -> [2,3]
- ES5
var intersection = function intersection(a, b) {
var s = new Set(b);return a.filter(function (x) {
return s.has(x);
});
};
// intersection([1,2,3], [4,3,2]) -> [2,3]
-
Create a
Set
with all values ofa
andb
and convert to an array. -
ES6
const union = (a, b) => Array.from(new Set([...a, ...b]));
// union([1,2,3], [4,3,2]) -> [1,2,3,4]
- ES5
function _toConsumableArray(arr) {
if (Array.isArray(arr)) {
for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) {
arr2[i] = arr[i];
}
return arr2;
} else {
return Array.from(arr);
}
}
var union = function union(a, b) {
return Array.from(
new Set([].concat(_toConsumableArray(a), _toConsumableArray(b)))
);
};
// union([1,2,3], [4,3,2]) -> [1,2,3,4]
-
Use
Array.reduce()
to add each value to an accumulator, initialized with a value of0
, divide by thelength
of the array. -
ES6
const average = arr => arr.reduce((acc, val) => acc + val, 0) / arr.length;
// average([1,2,3]) -> 2
- ES5
var average = function average(arr) {
return (
arr.reduce(function(acc, val) {
return acc + val;
}, 0) / arr.length
);
};
// average([1,2,3]) -> 2
-
Use
Array.from()
to create a new array, that fits the number of chunks that will be produced. UseArray.slice()
to map each element of the new array to a chunk the length ofsize
. If the original array can't be split evenly, the final chunk will contain the remaining elements. -
ES6
const chunk = (arr, size) =>
Array.from({length: Math.ceil(arr.length / size)}, (v, i) => arr.slice(i * size, i * size + size));
// chunk([1,2,3,4,5], 2) -> [[1,2],[3,4],5]
- ES5
var chunk = function chunk(arr, size) {
return Array.from({ length: Math.ceil(arr.length / size) }, function(v, i) {
return arr.slice(i * size, i * size + size);
});
};
// chunk([1,2,3,4,5], 2) -> [[1,2],[3,4],5]
-
Use
Array.filter()
to filter out falsey values (false
,null
,0
,""
,undefined
, andNaN
). -
ES6
const compact = (arr) => arr.filter(v => v);
// compact([0, 1, false, 2, '', 3, 'a', 'e'*23, NaN, 's', 34]) -> [ 1, 2, 3, 'a', 's', 34 ]
- ES5
var compact = function compact(arr) {
return arr.filter(function(v) {
return v;
});
};
// compact([0, 1, false, 2, '', 3, 'a', 'e'*23, NaN, 's', 34]) -> [ 1, 2, 3, 'a', 's', 34 ]
-
Use
Array.reduce()
to increment a counter each time you encounter the specific value inside the array. -
ES6
const countOccurrences = (arr, value) => arr.reduce((a, v) => v === value ? a + 1 : a + 0, 0);
// countOccurrences([1,1,2,1,2,3], 1) -> 3
- ES5
const countOccurrences = (arr, value) => arr.reduce((a, v) => v === value ? a + 1 : a + 0, 0);
// countOccurrences([1,1,2,1,2,3], 1) -> 3
-
Use recursion. Use
Array.reduce()
to get all elements that are not arrays, flatten each element that is an array. -
ES6
const deepFlatten = arr =>
arr.reduce((a, v) => a.concat(Array.isArray(v) ? deepFlatten(v) : v), []);
// deepFlatten([1,[2],[[3],4],5]) -> [1,2,3,4,5]
- ES5
var deepFlatten = function deepFlatten(arr) {
return arr.reduce(function(a, v) {
return a.concat(Array.isArray(v) ? deepFlatten(v) : v);
}, []);
};
// deepFlatten([1,[2],[[3],4],5]) -> [1,2,3,4,5]
-
Loop through the array, using
Array.shift()
to drop the first element of the array until the returned value from the function istrue
. Returns the remaining elements. -
ES6
const dropElements = (arr, func) => {
while (arr.length > 0 && !func(arr[0])) arr.shift();
return arr;
};
// dropElements([1, 2, 3, 4], n => n >= 3) -> [3,4]
- ES5
var dropElements = function dropElements(arr, func) {
while (arr.length > 0 && !func(arr[0])) {
arr.shift();
}
return arr;
};
// dropElements([1, 2, 3, 4], n => n >= 3) -> [3,4]
Use Array.map()
to map values between start
(inclusive) and end
(exclusive) to value
.
Omit start
to start at the first element and/or end
to finish at the last.
- ES6
const fillArray = (arr, value, start = 0, end = arr.length) =>
arr.map((v, i) => i >= start && i < end ? value : v);
// fillArray([1,2,3,4],'8',1,3) -> [1,'8','8',4]
- ES5
var fillArray = function fillArray(arr, value) {
var start =
arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 0;
var end =
arguments.length > 3 && arguments[3] !== undefined
? arguments[3]
: arr.length;
return arr.map(function(v, i) {
return i >= start && i < end ? value : v;
});
};
// fillArray([1,2,3,4],'8',1,3) -> [1,'8','8',4]
Use Array.filter()
for an array containing only the unique values.
- ES6
const filterNonUnique = arr => arr.filter(i => arr.indexOf(i) === arr.lastIndexOf(i));
// filterNonUnique([1,2,2,3,4,4,5]) -> [1,3,5]
- ES5
var filterNonUnique = function filterNonUnique(arr) {
return arr.filter(function(i) {
return arr.indexOf(i) === arr.lastIndexOf(i);
});
};
// filterNonUnique([1,2,2,3,4,4,5]) -> [1,3,5]
Use recursion, decrementing depth
by 1 for each level of depth.
Use Array.reduce()
and Array.concat()
to merge elements or arrays.
Base case, for depth
equal to 1
stops recursion.
Omit the second element, depth
to flatten only to a depth of 1
(single flatten).
- ES6
const flattenDepth = (arr, depth = 1) =>
depth != 1 ? arr.reduce((a, v) => a.concat(Array.isArray(v) ? flattenDepth(v, depth - 1) : v), [])
: arr.reduce((a, v) => a.concat(v), []);
// flattenDepth([1,[2],[[[3],4],5]], 2) -> [1,2,[3],4,5]
- ES5
var flattenDepth = function flattenDepth(arr) {
var depth =
arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1;
return depth != 1
? arr.reduce(function(a, v) {
return a.concat(Array.isArray(v) ? flattenDepth(v, depth - 1) : v);
}, [])
: arr.reduce(function(a, v) {
return a.concat(v);
}, []);
};
// flattenDepth([1,[2],[[[3],4],5]], 2) -> [1,2,[3],4,5]
Use Array.reduce()
to get all elements inside the array and concat()
to flatten them.
- ES6
const flatten = arr => arr.reduce((a, v) => a.concat(v), []);
// flatten([1,[2],3,4]) -> [1,2,3,4]
- ES5
var flatten = function flatten(arr) {
return arr.reduce(function(a, v) {
return a.concat(v);
}, []);
};
// flatten([1,[2],3,4]) -> [1,2,3,4]
Use Math.max()
combined with the spread operator (...
) to get the maximum value in the array.
- ES6
const arrayMax = arr => Math.max(...arr);
// arrayMax([10, 1, 5]) -> 10
- ES5
function _toConsumableArray(arr) {
if (Array.isArray(arr)) {
for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) {
arr2[i] = arr[i];
}
return arr2;
} else {
return Array.from(arr);
}
}
var arrayMax = function arrayMax(arr) {
return Math.max.apply(Math, _toConsumableArray(arr));
};
// arrayMax([10, 1, 5]) -> 10
Use Math.min()
combined with the spread operator (...
) to get the minimum value in the array.
- ES6
const arrayMin = arr => Math.min(...arr);
// arrayMin([10, 1, 5]) -> 1
- ES5
function _toConsumableArray(arr) {
if (Array.isArray(arr)) {
for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) {
arr2[i] = arr[i];
}
return arr2;
} else {
return Array.from(arr);
}
}
var arrayMin = function arrayMin(arr) {
return Math.min.apply(Math, _toConsumableArray(arr));
};
// arrayMin([10, 1, 5]) -> 1
Use Array.map()
to map the values of an array to a function or property name.
Use Array.reduce()
to create an object, where the keys are produced from the mapped results.
- ES6
const groupBy = (arr, func) =>
arr.map(typeof func === 'function' ? func : val => val[func]<
F438
span class="pl-kos">)
.reduce((acc, val, i) => { acc[val] = (acc[val] || []).concat(arr[i]); return acc; }, {});
// groupBy([6.1, 4.2, 6.3], Math.floor) -> {4: [4.2], 6: [6.1, 6.3]}
// groupBy(['one', 'two', 'three'], 'length') -> {3: ['one', 'two'], 5: ['three']}
- ES5
var groupBy = function groupBy(arr, func) {
return arr
.map(
typeof func === "function"
? func
: function(val) {
return val[func];
}
)
.reduce(function(acc, val, i) {
acc[val] = (acc[val] || []).concat(arr[i]);
return acc;
}, {});
};
// groupBy([6.1, 4.2, 6.3], Math.floor) -> {4: [4.2], 6: [6.1, 6.3]}
// groupBy(['one', 'two', 'three'], 'length') -> {3: ['one', 'two'], 5: ['three']}
Use arr[0]
to return the first element of the passed array.
- ES6
const head = arr => arr[0];
// head([1,2,3]) -> 1
- ES5
var head = function head(arr) {
return arr[0];
};
// head([1,2,3]) -> 1
Use arr.slice(0,-1)
to return all but the last element of the array.
- ES6
const initial = arr => arr.slice(0, -1);
// initial([1,2,3]) -> [1,2]
- ES5
var initial = function initial(arr) {
return arr.slice(0, -1);
};
// initial([1,2,3]) -> [1,2]
Use Array(end-start)
to create an array of the desired length, Array.map()
to fill with the desired values in a range.
You can omit start
to use a default value of 0
.
- ES6
const initializeArrayRange = (end, start = 0) =>
Array.apply(null, Array(end - start)).map((v, i) => i + start);
// initializeArrayRange(5) -> [0,1,2,3,4]
- ES5
var initializeArrayRange = function initializeArrayRange(end) {
var start =
arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
return Array.apply(null, Array(end - start)).map(function(v, i) {
return i + start;
});
};
// initializeArrayRange(5) -> [0,1,2,3,4]
Use Array(n)
to create an array of the desired length, fill(v)
to fill it with the desired values.
You can omit value
to use a default value of 0
.
- ES6
const initializeArray = (n, value = 0) => Array(n).fill(value);
// initializeArray(5, 2) -> [2,2,2,2,2]
- ES5
var initializeArray = function initializeArray(n) {
var value =
arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
return Array(n).fill(value);
};
// initializeArray(5, 2) -> [2,2,2,2,2]
Use arr.slice(-1)[0]
to get the last element of the given array.
- ES6
const last = arr => arr.slice(-1)[0];
// last([1,2,3]) -> 3
- ES5
var last = function last(arr) {
return arr.slice(-1)[0];
};
// last([1,2,3]) -> 3
Find the middle of the array, use Array.sort()
to sort the values.
Return the number at the midpoint if length
is odd, otherwise the average of the two middle numbers.
- ES6
const median = arr => {
const mid = Math.floor(arr.length / 2), nums = arr.sort((a, b) => a - b);
return arr.length % 2 !== 0 ? nums[mid] : (nums[mid - 1] + nums[mid]) / 2;
};
// median([5,6,50,1,-5]) -> 5
// median([0,10,-2,7]) -> 3.5
- ES5
var median = function median(arr) {
var mid = Math.floor(arr.length / 2),
nums = arr.sort(function(a, b) {
return a - b;
});
return arr.length % 2 !== 0 ? nums[mid] : (nums[mid - 1] + nums[mid]) / 2;
};
// median([5,6,50,1,-5]) -> 5
// median([0,10,-2,7]) -> 3.5
Use Array.slice()
to get an array containing the nth element at the first place.
If the index is out of bounds, return []
.
Omit the second argument, n
, to get the first element of the array.
- ES6
const nth = (arr, n=0) => (n>0? arr.slice(n,n+1) : arr.slice(n))[0];
// nth(['a','b','c'],1) -> 'b'
// nth(['a','b','b']-2) -> 'a'
- ES5
var nth = function nth(arr) {
var n = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
return (n > 0 ? arr.slice(n, n + 1) : arr.slice(n))[0];
};
// nth(['a','b','c'],1) -> 'b'
// nth(['a','b','b']-2) -> 'a'
Use Array.reduce()
to convert the filtered/picked keys back to a object with the corresponding key:value pair if the key exist in the obj.
- ES6
const pick = (obj, arr) =>
arr.reduce((acc, curr) => (curr in obj && (acc[curr] = obj[curr]), acc), {});
// pick({ 'a': 1, 'b': '2', 'c': 3 }, ['a', 'c']) -> { 'a': 1, 'c': 3 }
// pick(object, ['a', 'c'])['a'] -> 1
- ES5
var pick = function pick(obj, arr) {
return arr.reduce(function(acc, curr) {
return curr in obj && (acc[curr] = obj[curr]), acc;
}, {});
};
// pick({ 'a': 1, 'b': '2', 'c': 3 }, ['a', 'c']) -> { 'a': 1, 'c': 3 }
// pick(object, ['a', 'c'])['a'] -> 1
Use Array.sort()
to reorder elements, using Math.random()
in the comparator.
- ES6
const shuffle = arr => arr.sort(() => Math.random() - 0.5);
// shuffle([1,2,3]) -> [2,3,1]
- ES5
var shuffle = function shuffle(arr) {
return arr.sort(function() {
return Math.random() - 0.5;
});
};
// shuffle([1,2,3]) -> [2,3,1]
Use filter()
to remove values that are not part of values
, determined using includes()
.
- ES6
const similarity = (arr, values) => arr.filter(v => values.includes(v));
// similarity([1,2,3], [1,2,4]) -> [1,2]
- ES5
var similarity = function similarity(arr, values) {
return arr.filter(function(v) {
return values.includes(v);
});
};
// similarity([1,2,3], [1,2,4]) -> [1,2]
Use Array.reduce()
to add each value to an accumulator, initialized with a value of 0
.
- ES6
const sum = arr => arr.reduce((acc, val) => acc + val, 0);
// sum([1,2,3,4]) -> 10
- ES5
var sum = function sum(arr) {
return arr.reduce(function(acc, val) {
return acc + val;
}, 0);
};
// sum([1,2,3,4]) -> 10
Return arr.slice(1)
if the array's length
is more than 1
, otherwise return the whole array.
- ES6
const tail = arr => arr.length > 1 ? arr.slice(1) : arr;
// tail([1,2,3]) -> [2,3]
// tail([1]) -> [1]
- ES5
var tail = function tail(arr) {
return arr.length > 1 ? arr.slice(1) : arr;
};
// tail([1,2,3]) -> [2,3]
// tail([1]) -> [1]
Use Array.slice()
to create a slice of the array with n
elements taken from the beginning.
- ES6
const take = (arr, n = 1) => arr.slice(0, n);
// take([1, 2, 3], 5) -> [1, 2, 3]
// take([1, 2, 3], 0) -> []
- ES5
var take = function take(arr) {
var n = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1;
return arr.slice(0, n);
};
// take([1, 2, 3], 5) -> [1, 2, 3]
// take([1, 2, 3], 0) -> []
Use ES6 Set
and the ...rest
operator to discard all duplicated values.
- ES6
const unique = arr => [...new Set(arr)];
// unique([1,2,2,3,4,4,5]) -> [1,2,3,4,5]
- ES5
function _toConsumableArray(arr) {
if (Array.isArray(arr)) {
for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) {
arr2[i] = arr[i];
}
return arr2;
} else {
return Array.from(arr);
}
}
var unique = function unique(arr) {
return [].concat(_toConsumableArray(new Set(
10000
arr)));
};
// unique([1,2,2,3,4,4,5]) -> [1,2,3,4,5]
Use scrollY
, scrollHeight
and clientHeight
to determine if the bottom of the page is visible.
- ES6
const bottomVisible = _ =>
document.documentElement.clientHeight + window.scrollY >= document.documentElement.scrollHeight || document.documentElement.clientHeight;
// bottomVisible() -> true
- ES5
var bottomVisible = function bottomVisible(_) {
return (
document.documentElement.clientHeight + window.scrollY >=
document.documentElement.scrollHeight ||
document.documentElement.clientHeight
);
};
// bottomVisible() -> true
Use window.location.href
to get current URL.
- ES6
const currentUrl = _ => window.location.href;
// currentUrl() -> 'https://google.com'
- ES5
var currentUrl = function currentUrl(_) {
return window.location.href;
};
// currentUrl() -> 'https://google.com'
Use Element.getBoundingClientRect()
and the window.inner(Width|Height)
values
to determine if a given element is visible in the viewport.
Omit the second argument to determine if the element is entirely visible, or specify true
to determine if
it is partially visible.
- ES6
const elementIsVisibleInViewport = (el, partiallyVisible = false) => {
const { top, left, bottom, right } = el.getBoundingClientRect();
return partiallyVisible
? ((top > 0 && top < innerHeight) || (bottom > 0 && bottom < innerHeight)) &&
((left > 0 && left < innerWidth) || (right > 0 && right < innerWidth))
: top >= 0 && left >= 0 && bottom <= innerHeight && right <= innerWidth;
};
// e.g. 100x100 viewport and a 10x10px element at position {top: -1, left: 0, bottom: 9, right: 10}
// elementIsVisibleInViewport(el) -> false (not fully visible)
// elementIsVisibleInViewport(el, true) -> true (partially visible)
- ES5
var elementIsVisibleInViewport = function elementIsVisibleInViewport(el) {
var partiallyVisible =
arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
var _el$getBoundingClient = el.getBoundingClientRect(),
top = _el$getBoundingClient.top,
left = _el$getBoundingClient.left,
bottom = _el$getBoundingClient.bottom,
right = _el$getBoundingClient.right;
return partiallyVisible
? ((top > 0 && top < innerHeight) ||
(bottom > 0 && bottom < innerHeight)) &&
((left > 0 && left < innerWidth) || (right > 0 && right < innerWidth))
: top >= 0 && left >= 0 && bottom <= innerHeight && right <= innerWidth;
};
// e.g. 100x100 viewport and a 10x10px element at position {top: -1, left: 0, bottom: 9, right: 10}
// elementIsVisibleInViewport(el) -> false (not fully visible)
// elementIsVisibleInViewport(el, true) -> true (partially visible)
Use pageXOffset
and pageYOffset
if they are defined, otherwise scrollLeft
and scrollTop
.
You can omit el
to use a default value of window
.
- ES6
const getScrollPos = (el = window) =>
({x: (el.pageXOffset !== undefined) ? el.pageXOffset : el.scrollLeft,
y: (el.pageYOffset !== undefined) ? el.pageYOffset : el.scrollTop});
// getScrollPos() -> {x: 0, y: 200}
- ES5
var getScrollPos = function getScrollPos() {
var el =
arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : window;
return {
x: el.pageXOffset !== undefined ? el.pageXOffset : el.scrollLeft,
y: el.pageYOffset !== undefined ? el.pageYOffset : el.scrollTop
};
};
// getScrollPos() -> {x: 0, y: 200}
Use window.location.href
or window.location.replace()
to redirect to url
.
Pass a second argument to simulate a link click (true
- default) or an HTTP redirect (false
).
- ES6
const redirect = (url, asLink = true) =>
asLink ? window.location.href = url : window.location.replace(url);
// redirect('https://google.com')
- ES5
var redirect = function redirect(url) {
var asLink =
arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
return asLink ? (window.location.href = url) : window.location.replace(url);
};
// redirect('https://google.com')
Get distance from top using document.documentElement.scrollTop
or document.body.scrollTop
.
Scroll by a fraction of the distance from top. Use window.requestAnimationFrame()
to animate the scrolling.
- ES6
const scrollToTop = _ => {
const c = document.documentElement.scrollTop || document.body.scrollTop;
if (c > 0) {
window.requestAnimationFrame(scrollToTop);
window.scrollTo(0, c - c / 8);
}
};
// scrollToTop()
- ES5
var scrollToTop = function scrollToTop(_) {
var c = document.documentElement.scrollTop || document.body.scrollTop;
if (c > 0) {
window.requestAnimationFrame(scrollToTop);
window.scrollTo(0, c - c / 8);
}
};
// scrollToTop()
Calculate the difference (in days) between to Date
objects.
- ES6
const getDaysDiffBetweenDates = (dateInitial, dateFinal) => (dateFinal - dateInitial) / (1000 * 3600 * 24);
// getDaysDiffBetweenDates(new Date("2017-12-13"), new Date("2017-12-22")) -> 9
- ES5
var getDaysDiffBetweenDates = function getDaysDiffBetweenDates(
dateInitial,
dateFinal
) {
return (dateFinal - dateInitial) / (1000 * 3600 * 24);
};
// getDaysDiffBetweenDates(new Date("2017-12-13"), new Date("2017-12-22")) -> 9
Loop through an array of functions containing asynchronous events, calling next
when each asynchronous event has completed.
- ES6
const chainAsync = fns => { let curr = 0; const next = () => fns[curr++](next); next(); };
/*
chainAsync([
next => { console.log('0 seconds'); setTimeout(next, 1000); },
next => { console.log('1 second'); setTimeout(next, 1000); },
next => { console.log('2 seconds'); }
])
*/
- ES5
var chainAsync = function chainAsync(fns) {
var curr = 0;
var next = function next() {
return fns[curr++](next);
};
next();
};
/*
chainAsync([
next => { console.log('0 seconds'); setTimeout(next, 1000); },
next => { console.log('1 second'); setTimeout(next, 1000); },
next => { console.log('2 seconds'); }
])
*/
Use recursion.
If the number of provided arguments (args
) is sufficient, call the passed function f
.
Otherwise return a curried function f
that expects the rest of the arguments.
If you want to curry a function that accepts a variable number of arguments (a variadic function, e.g. Math.min()
), you can optionally pass the number of arguments to the second parameter arity
.
- ES6
const curry = (fn, arity = fn.length, ...args) =>
arity <= args.length
? fn(...args)
: curry.bind(null, fn, arity, ...args);
// curry(Math.pow)(2)(10) -> 1024
// curry(Math.min, 3)(10)(50)(2) -> 2
- ES5
var curry = function curry(fn) {
for (
var _len = arguments.length,
args = Array(_len > 2 ? _len - 2 : 0),
_key = 2;
_key < _len;
_key++
) {
args[_key - 2] = arguments[_key];
}
var arity =
arguments.length > 1 && arguments[1] !== undefined
? arguments[1]
: fn.length;
return arity <= args.length
? fn.apply(undefined, args)
: curry.bind.apply(curry, [null, fn, arity].concat(args));
};
// curry(Math.pow)(2)(10) -> 1024
// curry(Math.min, 3)(10)(50)(2) -> 2
Use Array.reduce()
to pass value through functions.
- ES6
const pipe = (...funcs) => arg => funcs.reduce((acc, func) => func(acc), arg);
// pipe(btoa, x => x.toUpperCase())("Test") -> "VGVZDA=="
- ES5
var pipe = function pipe() {
for (
var _len = arguments.length, funcs = Array(_len), _key = 0;
_key < _len;
_key++
) {
funcs[_key] = arguments[_key];
}
return function(arg) {
return funcs.reduce(function(acc, func) {
return func(acc);
}, arg);
};
};
// pipe(btoa, x => x.toUpperCase())("Test") -> "VGVZDA=="
Use currying to return a function returning a Promise
that calls the original function.
Use the ...rest
operator to pass in all the parameters.
In Node 8+, you can use util.promisify
- ES6
const promisify = func =>
(...args) =>
new Promise((resolve, reject) =>
func(...args, (err, result) =>
err ? reject(err) : resolve(result))
);
// const delay = promisify((d, cb) => setTimeout(cb, d))
// delay(2000).then(() => console.log('Hi!')) -> Promise resolves after 2s
- ES5
var promisify = function promisify(func) {
return function() {
for (
var _len = arguments.length, args = Array(_len), _key = 0;
_key < _len;
_key++
) {
args[_key] = arguments[_key];
}
return new Promise(function(resolve, reject) {
return func.apply(
undefined,
args.concat([
function(err, result) {
return err ? <
10000
span class="pl-s1">reject(err) : resolve(result);
}
])
);
});
};
};
// const delay = promisify((d, cb) => setTimeout(cb, d))
// delay(2000).then(() => console.log('Hi!')) -> Promise resolves after 2s
Run an array of promises in series using Array.reduce()
by creating a promise chain, where each promise returns the next promise when resolved.
- ES6
const series = ps => ps.reduce((p, next) => p.then(next), Promise.resolve());
// const delay = (d) => new Promise(r => setTimeout(r, d))
// series([() => delay(1000), () => delay(2000)]) -> executes each promise sequentially, taking a total of 3 seconds to complete
- ES5
var series = function series(ps) {
return ps.reduce(function(p, next) {
return p.then(next);
}, Promise.resolve());
};
// const delay = (d) => new Promise(r => setTimeout(r, d))
// series([() => delay(1000), () => delay(2000)]) -> executes each promise sequentially, taking a total of 3 seconds to complete
Delay executing part of an async
function, by putting it to sleep, returning a Promise
.
- ES6
const sleep = ms => new Promise(resolve => setTimeout(resolve, ms));
/*
async function sleepyWork() {
console.log('I\'m going to sleep for 1 second.');
await sleep(1000);
console.log('I woke up after 1 second.');
}
*/
- ES5
var sleep = function sleep(ms) {
return new Promise(function(resolve) {
return setTimeout(resolve, ms);
});
};
/*
async function sleepyWork() {
console.log('I\'m going to sleep for 1 second.');
await sleep(1000);
console.log('I woke up after 1 second.');
}
*/
If n
is even, return n/2
. Otherwise return 3n+1
.
- ES6
const collatz = n => (n % 2 == 0) ? (n / 2) : (3 * n + 1);
// collatz(8) --> 4
// collatz(5) --> 16
- ES5
var collatz = function collatz(n) {
return n % 2 == 0 ? n / 2 : 3 * n + 1;
};
// collatz(8) --> 4
// collatz(5) --> 16
Use Math.hypot()
to calculate the Euclidean distance between two points.
- ES6
const distance = (x0, y0, x1, y1) => Math.hypot(x1 - x0, y1 - y0);
// distance(1,1, 2,3) -> 2.23606797749979
- ES5
var distance = function distance(x0, y0, x1, y1) {
return Math.hypot(x1 - x0, y1 - y0);
};
// distance(1,1, 2,3) -> 2.23606797749979
Use the modulo operator (%
) to check if the remainder is equal to 0
.
- ES6
const isDivisible = (dividend, divisor) => dividend % divisor === 0;
// isDivisible(6,3) -> true
- ES5
var isDivisible = function isDivisible(dividend, divisor) {
return dividend % divisor === 0;
};
// isDivisible(6,3) -> true
Checks whether a number is odd or even using the modulo (%
) operator.
Returns true
if the number is even, false
if the number is odd.
- ES6
const isEven = num => num % 2 === 0;
// isEven(3) -> false
- ES5
var isEven = function isEven(num) {
return num % 2 === 0;
};
// isEven(3) -> false
Use recursion.
If n
is less than or equal to 1
, return 1
.
Otherwise, return the product of n
and the factorial of n - 1
.
- ES6
const factorial = n => n <= 1 ? 1 : n * factorial(n - 1);
// factorial(6) -> 720
- ES5
var factorial = function factorial(n) {
return n <= 1 ? 1 : n * factorial(n - 1);
};
// factorial(6) -> 720
Create an empty array of the specific length, initializing the first two values (0
and 1
).
Use Array.reduce()
to add values into the array, using the sum of the last two values, except for the first two.
- ES6
const fibonacci = n =>
Array(n).fill(0).reduce((acc, val, i) => acc.concat(i > 1 ? acc[i - 1] + acc[i - 2] : i), []);
// fibonacci(5) -> [0,1,1,2,3]
- ES5
var fibonacci = function fibonacci(n) {
return Array(n)
.fill(0)
.reduce(function(acc, val, i) {
return acc.concat(i > 1 ? acc[i - 1] + acc[i - 2] : i);
}, []);
};
// fibonacci(5) -> [0,1,1,2,3]
Use recursion.
Base case is when y
equals 0
. In this case, return x
.
Otherwise, return the GCD of y
and the remainder of the division x/y
.
- ES6
const gcd = (x, y) => !y ? x : gcd(y, x % y);
// gcd (8, 36) -> 4
- ES5
var gcd = function gcd(x, y) {
return !y ? x : gcd(y, x % y);
};
// gcd (8, 36) -> 4
Use XOR operator (^
) to find the bit difference between the two numbers, convert to binary string using toString(2)
.
Count and return the number of 1
s in the string, using match(/1/g)
.
- ES6
const hammingDistance = (num1, num2) =>
((num1 ^ num2).toString(2).match(/1/g) || '').length;
// hammingDistance(2,3) -> 1
- ES5
var hammingDistance = function hammingDistance(num1, num2) {
return ((num1 ^ num2).toString(2).match(/1/g) || "").length;
};
// hammingDistance(2,3) -> 1
Use Array.reduce()
to calculate how many numbers are below the value and how many are the same value and
apply the percentile formula.
- ES6
const percentile = (arr, val) =>
100 * arr.reduce((acc,v) => acc + (v < val ? 1 : 0) + (v === val ? 0.5 : 0), 0) / arr.length;
// percentile([1,2,3,4,5,6,7,8,9,10], 6) -> 55
- ES5
var percentile = function percentile(arr, val) {
return (
100 *
arr.reduce(function(acc, v) {
return acc + (v < val ? 1 : 0) + (v === val ? 0.5 : 0);
}, 0) /
arr.length
);
};
// percentile([1,2,3,4,5,6,7,8,9,10], 6) -> 55
Use Array.reduce()
combined with Array.map()
to iterate over elements and combine into an array containing all combinations.
- ES6
const powerset = arr =>
arr.reduce((a, v) => a.concat(a.map(r => [v].concat(r))), [[]]);
// powerset([1,2]) -> [[], [1], [2], [2,1]]
- ES5
var powerset = function powerset(arr) {
return arr.reduce(
function(a, v) {
return a.concat(
a.map(function(r) {
return [v].concat(r);
})
);
},
[[]]
);
};
// powerset([1,2]) -> [[], [1], [2], [2,1]]
Use Math.round()
and template literals to round the number to the specified number of digits.
Omit the second argument, decimals
to round to an integer.
- ES6
const round = (n, decimals=0) => Number(`${Math.round(`${n}e${decimals}`)}e-${decimals}`);
// round(1.005, 2) -> 1.01
- ES5
var round = function round(n) {
var decimals =
arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
return Number(Math.round(n + "e" + decimals) + "e-" + decimals);
};
// round(1.005, 2) -> 1.01
Use Array.reduce()
to calculate the mean, variance and the sum of the variance of the values, the variance of the values, then
determine the standard deviation.
You can omit the second argument to get the sample standard deviation or set it to true
to get the population standard deviation.
- ES6
const standardDeviation = (arr, usePopulation = false) => {
const mean = arr.reduce((acc, val) => acc + val, 0) / arr.length;
return Math.sqrt(
arr.reduce((acc, val) => acc.concat(Math.pow(val - mean, 2)), [])
.reduce((acc, val) => acc + val, 0) / (arr.length - (usePopulation ? 0 : 1))
);
};
// standardDeviation([10,2,38,23,38,23,21]) -> 13.284434142114991 (sample)
// standardDeviation([10,2,38,23,38,23,21], true) -> 12.29899614287479 (population)
- ES5
var standardDeviation = function standardDeviation(arr) {
var usePopulation =
arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
var mean =
arr.reduce(function(acc, val) {
return acc + val;
}, 0) / arr.length;
return Math.sqrt(
arr
.reduce(function(acc, val) {
return acc.concat(Math.pow(val - mean, 2));
}, [])
.reduce(function(acc, val) {
return acc + val;
}, 0) /
(arr.length - (usePopulation ? 0 : 1))
);
};
// standardDeviation([10,2,38,23,38,23,21]) -> 13.284434142114991 (sample)
// standardDeviation([10,2,38,23,38,23,21], true) -> 12.29899614287479 (population)
Use SpeechSynthesisUtterance.voice
and indow.speechSynthesis.getVoices()
to convert a message to speech.
Use window.speechSynthesis.speak()
to play the message.
Learn more about the SpeechSynthesisUtterance interface of the Web Speech API.
- ES6
const speak = message => {
const msg = new SpeechSynthesisUtterance(message);
msg.voice = window.speechSynthesis.getVoices()[0];
window.speechSynthesis.speak(msg);
};
// speak('Hello, World') -> plays the message
- ES5
var speak = function speak(message) {
var msg = new SpeechSynthesisUtterance(message);
msg.voice = window.speechSynthesis.getVoices()[0];
window.speechSynthesis.speak(msg);
};
// speak('Hello, World') -> plays the message
Use Array.reduce()
to create and combine key-value pairs.
- ES6
const objectFromPairs = arr => arr.reduce((a, v) => (a[v[0]] = v[1], a), {});
// objectFromPairs([['a',1],['b',2]]) -> {a: 1, b: 2}
- ES5
var objectFromPairs = function objectFromPairs(arr) {
return arr.reduce(function(a, v) {
return (a[v[0]] = v[1]), a;
}, {});
};
// objectFromPairs([['a',1],['b',2]]) -> {a: 1, b: 2}
Use Object.keys()
and Array.map()
to iterate over the object's keys and produce an array with key-value pairs.
- ES6
const objectToPairs = obj => Object.keys(obj).map(k => [k, obj[k]]);
// objectToPairs({a: 1, b: 2}) -> [['a',1],['b',2]])
- ES5
var objectToPairs = function objectToPairs(obj) {
return Object.keys(obj).map(function(k) {
return [k, obj[k]];
});
};
// objectToPairs({a: 1, b: 2}) -> [['a',1],['b',2]])
Use the object ...spread
operator to spread the properties of the target object into the clone.
- ES6
const shallowClone = obj => ({ ...obj });
/*
const a = { x: true, y: 1 };
const b = shallowClone(a);
a === b -> false
*/
Use recursion.
For each letter in the given string, create all the partial anagrams for the rest of its letters.
Use Array.map()
to combine the letter with each partial anagram, then Array.reduce()
to combine all anagrams in one array.
Base cases are for string length
equal to 2
or 1
.
- ES6
const anagrams = str => {
if (str.length <= 2) return str.length === 2 ? [str, str[1] + str[0]] : [str];
return str.split('').reduce((acc, letter, i) =>
acc.concat(anagrams(str.slice(0, i) + str.slice(i + 1)).map(val => letter + val)), []);
};
// anagrams('abc') -> ['abc','acb','bac','bca','cab','cba']
- ES5
var anagrams = function anagrams(str) {
if (str.length <= 2) return str.length === 2 ? [str, str[1] + str[0]] : [str];
return str.split("").reduce(function(acc, letter, i) {
return acc.concat(
anagrams(str.slice(0, i) + str.slice(i + 1)).map(function(val) {
return letter + val;
})
);
}, []);
};
// anagrams('abc') -> ['abc','acb','bac','bca','cab','cba']
Use replace()
to match the first character of each word and toUpperCase()
to capitalize it.
- ES6
const capitalizeEveryWord = str => str.replace(/\b[a-z]/g, char => char.toUpperCase());
// capitalizeEveryWord('hello world!') -> 'Hello World!'
- ES5
var capitalizeEveryWord = function capitalizeEveryWord(str) {
return str.replace(/\b[a-z]/g, function(char) {
return char.toUpperCase();
});
};
// capitalizeEveryWord('hello world!') -> 'Hello World!'
Use slice(0,1)
and toUpperCase()
to capitalize first letter, slice(1)
to get the rest of the string.
Omit the lowerRest
parameter to keep the rest of the string intact, or set it to true
to convert to lower case.
- ES6
const capitalize = (str, lowerRest = false) =>
str.slice(0, 1).toUpperCase() + (lowerRest ? str.slice(1).toLowerCase() : str.slice(1));
// capitalize('myName', true) -> 'Myname'
- ES5
var capitalize = function capitalize(str) {
var lowerRest =
arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
return (
str.slice(0, 1).toUpperCase() +
(lowerRest ? str.slice(1).toLowerCase() : str.slice(1))
);
};
// capitalize('myName', true) -> 'Myname'
Convert string toLowerCase()
and use replace()
to remove non-alphanumeric characters from it.
Then, split('')
into individual characters, reverse()
, join('')
and compare to the original, unreversed string, after converting it tolowerCase()
.
- ES6
const palindrome = str => {
const s = str.toLowerCase().replace(/[\W_]/g,'');
return s === s.split('').reverse().join('');
}
// palindrome('taco cat') -> true
- ES5
var palindrome = function palindrome(str) {
var s = str.toLowerCase().replace(/[\W_]/g, "");
return (
s ===
s
.split("")
.reverse()
.join("")
);
};
// palindrome('taco cat') -> true
Use array destructuring and Array.reverse()
to reverse the order of the characters in the string.
Combine characters to get a string using join('')
.
- ES6
const reverseString = str => [...str].reverse().join('');
// reverseString('foobar') -> 'raboof'
- ES5
function _toConsumableArray(arr) {
if (Array.isArray(arr)) {
for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) {
arr2[i] = arr[i];
}
return arr2;
} else {
return Array.from(arr);
}
}
var reverseString = function reverseString(str) {
return []
.concat(_toConsumableArray(str))
.reverse()
.join("");
};
// reverseString('foobar') -> 'raboof'
Split the string using split('')
, Array.sort()
utilizing localeCompare()
, recombine using join('')
.
- ES6
const sortCharactersInString = str =>
str.split('').sort((a, b) => a.localeCompare(b)).join('');
// sortCharactersInString('cabbage') -> 'aabbceg'
- ES5
var sortCharactersInString = function sortCharactersInString(str) {
return str
.split("")
.sort(function(a, b) {
return a.localeCompare(b);
})
.join("");
};
// sortCharactersInString('cabbage') -> 'aabbceg'
Determine if the string's length
is greater than num
.
Return the string truncated to the desired length, with ...
appended to the end or the original string.
- ES6
const truncate = (str, num) =>
str.length > num ? str.slice(0, num > 3 ? num - 3 : num) + '...' : str;
// truncate('boomerang', 7) -> 'boom...'
- ES5
var truncate = function truncate(str, num) {
return str.length > num ? str.slice(0, num > 3 ? num - 3 : num) + "..." : str;
};
// truncate('boomerang', 7) -> 'boom...'
Use replace()
to escape special characters.
- ES6
const escapeRegExp = str => str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
// escapeRegExp('(test)') -> \\(test\\)
- ES5
var escapeRegExp = function escapeRegExp(str) {
return str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
};
// escapeRegExp('(test)') -> \\(test\\)
Returns lower-cased constructor name of value, "undefined" or "null" if value is undefined or null
- ES6
const getType = v =>
v === undefined ? 'undefined' : v === null ? 'null' : v.constructor.name.toLowerCase();
// getType(new Set([1,2,3])) -> "set"
- ES5
var getType = function getType(v) {
return v === undefined
? "undefined"
: v === null ? "null" : v.constructor.name.toLowerCase();
};
// getType(new Set([1,2,3])) -> "set"
Use Array.isArray()
to check if a value is classified as an array.
- ES6
const isArray = val => !!val && Array.isArray(val);
// isArray(null) -> false
// isArray([1]) -> true
- ES5
var isArray = function isArray(val) {
return !!val && Array.isArray(val);
};
// isArray(null) -> false
// isArray([1]) -> true
Use typeof
to check if a value is classified as a boolean primitive.
- ES6
const isBoolean = val => typeof val === 'boolean';
// isBoolean(null) -> false
// isBoolean(false) -> true
- ES5
var isBoolean = function isBoolean(val) {
return typeof val === "boolean";
};
// isBoolean(null) -> false
// isBoolean(false) -> true
Use typeof
to check if a value is classified as a function primitive.
- ES6
const isFunction = val => val && typeof val === 'function';
// isFunction('x') -> false
// isFunction(x => x) -> true
- ES5
var isFunction = function isFunction(val) {
return val && typeof val === "function";
};
// isFunction('x') -> false
// isFunction(x => x) -> true
Use typeof
to check if a value is classified as a number primitive.
- ES6
const isNumber = val => typeof val === 'number';
// isNumber('1') -> false
// isNumber(1) -> true
- ES5
var isNumber = function isNumber(val) {
return typeof val === "number";
};
// isNumber('1') -> false
// isNumber(1) -> true
Use typeof
to check if a value is classified as a string primitive.
- ES6
const isString = val => typeof val === 'string';
// isString(10) -> false
// isString('10') -> true
- ES5
var isString = function isString(val) {
return typeof val === "string";
};
// isString(10) -> false
// isString('10') -> true
Use typeof
to check if a value is classified as a symbol primitive.
- ES6
const isSymbol = val => typeof val === 'symbol';
// isSymbol('x') -> false
// isSymbol(Symbol('x')) -> true
- ES5
var _typeof =
typeof Symbol === "function" && typeof Symbol.iterator === "symbol"
? function(obj) {
return typeof obj;
}
: function(obj) {
return obj &&
typeof Symbol === "function" &&
obj.constructor === Symbol &&
obj !== Symbol.prototype
? "symbol"
: typeof obj;
};
var isSymbol = function isSymbol(val) {
return (typeof val === "undefined" ? "undefined" : _typeof(val)) === "symbol";
};
// isSymbol('x') -> false
// isSymbol(Symbol('x')) -> true
Use console.time()
and console.timeEnd()
to measure the difference between the start and end times to determine how long the callback took to execute.
- ES6
const timeTaken = callback => {
console.time('timeTaken');
const r = callback();
console.timeEnd('timeTaken');
return r;
};
// timeTaken(() => Math.pow(2, 10)) -> 1024
// (logged): timeTaken: 0.02099609375ms
- ES5
var timeTaken = function timeTaken(callback) {
console.time("timeTaken");
var r = callback();
console.timeEnd("timeTaken");
return r;
};
// timeTaken(() => Math.pow(2, 10)) -> 1024
// (logged): timeTaken: 0.02099609375ms
Convert the number to a string, use split()
to convert build an array.
Use Array.map()
and parseInt()
to transform each value to an integer.
- ES6
const digitize = n => (''+n).split('').map(i => parseInt(i));
// digitize(2334) -> [2, 3, 3, 4]
- ES5
var digitize = function digitize(n) {
return ("" + n).split("").map(function(i) {
return parseInt(i);
});
};
// digitize(2334) -> [2, 3, 3, 4]
Use the modulo operator (%
) to find values of single and tens digits.
Find which ordinal pattern digits match.
If digit is found in teens pattern, use teens ordinal.
- ES6
const toOrdinalSuffix = num => {
const int = parseInt(num), digits = [(int % 10), (int % 100)],
ordinals = ['st', 'nd', 'rd', 'th'], oPattern = [1, 2, 3, 4],
tPattern = [11, 12, 13, 14, 15, 16, 17, 18, 19];
return oPattern.includes(digits[0]) && !tPattern.includes(digits[1]) ? int + ordinals[digits[0] - 1] : int + ordinals[3];
};
// toOrdinalSuffix("123") -> "123rd"
- ES5
var toOrdinalSuffix = function toOrdinalSuffix(num) {
var int = parseInt(num),
digits = [int % 10, int % 100],
ordinals = ["st", "nd", "rd", "th"],
oPattern = [1, 2, 3, 4],
tPattern = [11, 12, 13, 14, 15, 16, 17, 18, 19];
return oPattern.includes(digits[0]) && !tPattern.includes(digits[1])
? int + ordinals[digits[0] - 1]
: int + ordinals[3];
};
// toOrdinalSuffix("123") -> "123rd"
Use Math.random()
to generate a random number and map it to the desired range, using Math.floor()
to make it an integer.
- ES6
const randomIntegerInRange = (min, max) => Math.floor(Math.random() * (max - min + 1)) + min;
// randomIntegerInRange(0, 5) -> 2
- ES5
var randomIntegerInRange = function randomIntegerInRange(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
};
// randomIntegerInRange(0, 5) -> 2
Use Math.random()
to generate a random value, map it to the desired range using multiplication.
- ES6
const randomInRange = (min, max) => Math.random() * (max - min) + min;
// randomInRange(2,10) -> 6.0211363285087005
- ES5
var randomInRange = function randomInRange(min, max) {
return Math.random() * (max - min) + min;
};
// randomInRange(2,10) -> 6.0211363285087005
Convert given RGB parameters to hexadecimal string using bitwise left-shift operator (<<
) and toString(16)
, then padStart(6,'0')
to get a 6-digit hexadecimal value.
- ES6
const rgbToHex = (r, g, b) => ((r << 16) + (g << 8) + b).toString(16).padStart(6, '0');
// rgbToHex(255, 165, 1) -> 'ffa501'
- ES5
var rgbToHex = function rgbToHex(r, g, b) {
return ((r << 16) + (g << 8) + b).toString(16).padStart(6, "0");
};
// rgbToHex(255, 165, 1) -> 'ffa501'
Use array destructuring to swap values between two variables.
- ES6
[varA, varB] = [varB, varA];
// [x, y] = [y, x]
Use match()
with an appropriate regular expression to get all key-value pairs, Array.reduce()
to map and combine them into a single object.
Pass location.search
as the argument to apply to the current url
.
- ES6
const getUrlParameters = url =>
url.match(/([^?=&]+)(=([^&]*))/g).reduce(
(a, v) => (a[v.slice(0, v.indexOf('='))] = v.slice(v.indexOf('=') + 1), a), {}
);
// getUrlParameters('http://url.com/page?name=Adam&surname=Smith') -> {name: 'Adam', surname: 'Smith'}
- ES5
var getUrlParameters = function getUrlParameters(url) {
return url.match(/([^?=&]+)(=([^&]*))/g).reduce(function(a, v) {
return (a[v.slice(0, v.indexOf("="))] = v.slice(v.indexOf("=") + 1)), a;
}, {});
};
// getUrlParameters('http://url.com/page?name=Adam&surname=Smith') -> {name: 'Adam', surname: 'Smith'}
Use crypto
API to generate a UUID, compliant with RFC4122 version 4.
- ES6
const uuid = _ =>
([1e7] + -1e3 + -4e3 + -8e3 + -1e11).replace(/[018]/g, c =>
(c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16)
);
// uuid() -> '7982fcfe-5721-4632-bede-6000885be57d'
- ES5
var uuid = function uuid(_) {
return ([1e7] + -1e3 + -4e3 + -8e3 + -1e11).replace(/[018]/g, function(c) {
return (c ^
(crypto.getRandomValues(new Uint8Array(1))[0] &
(15 >> (c / 4)))).toString(16);
});
};
// uuid() -> '7982fcfe-5721-4632-bede-6000885be57d'
Use a regular experssion to check if the email is valid.
Returns true
if email is valid, false
if not.
- ES6
const validateEmail = str =>
/^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/.test(str);
// validateEmail(mymail@gmail.com) -> true
- ES5
var validateEmail = function validateEmail(str) {
return /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/.test(
str
);
};
// validateEmail(mymail@gmail.com) -> true
Use !isNaN
in combination with parseFloat()
to check if the argument is a number.
Use isFinite()
to check if the number is finite.
Use Number()
to check if the coercion holds.
- ES6
const validateNumber = n => !isNaN(parseFloat(n)) && isFinite(n) && Number(n) == n;
// validateNumber('10') -> true
- ES5
var validateNumber = function validateNumber(n) {
return !isNaN(parseFloat(n)) && isFinite(n) && Number(n) == n;
};
// validateNumber('10') -> true
Returns value, or default value if passed value is falsy
.
- ES6
const valueOrDefault = (value, d) => value || d;
// valueOrDefault(NaN, 30) -> 30
- ES5
var valueOrDefault = function valueOrDefault(value, d) {
return value || d;
};
// valueOrDefault(NaN, 30) -> 30