There are lots of helpful javascript functions, too many to list here. You'll need to learn to google and find functions you need. I recommend Mozilla's developer docs over W3Schools for built-in functions. lodash has many functions as well.
Here's a quick primer on some particularly useful ones:
.length
.length
is built in for arrays and strings.
Examples:
[1, 2, 3].length
=> // 3
"susan".length
=> // 5
array.includes(element)
and "string".includes("i")
.includes(
is a built-in function on arrays. It returns true
if the array includes that element.
It works in a similar way for Strings
Examples:
[1, 2, 3].includes(2)
=> // true
[1, 2, 3].includes(7)
=> // false
"Hello".includes("l")
=> // true
"Hello".includes("z")
=> // false
"Hello".includes("ello")
=> // true
array.join("separator")
.join(
is built-in on array and takes a parameter called: 'separator' which is a string.
.join(
creates a string of the elements in the array. It puts the 'separator' between each of the elements.
Examples:
["Eggs", "Milk", "Apples"].join(", ")
=> // "Eggs, Milk, Apples"
["Eggs", "Milk", "Apples"].join(" and ")
=> // "Eggs and Milk and Apples"
_.isEqual(x, y)
_.isEqual(
takes two objects and returns true if they're the same. With arrays and objects ===
sometimes doesn't work - use this.
{hello: 'world'} === {hello: 'world'}
// => false
_.isEqual({hello: 'world'}, {hello: 'world'})
// => true
"string".startsWith("s")
.startsWith(
is built-in on strings. It returns true if the string starts with the parameter.
"hello".startsWith("he")
// => true`
"string".endsWith("s")
.endsWith(
is built-in on strings. It returns true if the string ends with the parameter.
"hello".endsWith("lo")
// => true`
_.flatten(array)
and _.flattenDeep(array)
.flatten(
takes arrays within arrays and puts all the elements into a top level array.
_.flatten([1, 2, 3, [4]])
// => [1, 2, 3, 4]
_.flatten([1, [2, 3, 4], 3, [4]])
// => [1, 2, 3, 4, 3, 4]
_.flatten([1, [2, [1, 2, 3], 4], 3, [4]])
// => [1, 2, [1, 2, 3], 4, 3, 4]
_.flattenDeep([1, [2, [1, 2, 3], 4], 3, [4]])
// => [1, 2, 1, 2, 3, 4, 3, 4]
_.compact(array)
_.compact(
takes an array and removes any elements which are falsey.
This is very useful for removing null
and undefined
elements from arrays.
_.compact([1, 2, 3])
// => [1, 2, 3]
_.compact([1, false, 3])
// => [1, 3]
_.compact([1, null, 3])
// => [1, 3]
_.compact([1, undefined, 3])
// => [1, 3]
_.isNil(x)
_.isNil(
returns true
if the parameter is null
or undefined
Examples:
_.isNil(undefined)
// => true
_.isNil(null)
// => true
_.isNil("string")
// => false
"string".split("separator")
"string".split(
is built-in on strings. It 'splits' a string up by some kind of 'separator'.
Examples:
"one".split("n")
// => [ "o", "e"]
"banana".split("n")
// => [ "ba", "a", "a" ]
"Eggs, Milk, Apple".split(", ")
// => [ "Eggs", "Milk", "Apple" ]
array.slice(0, 2)
Slice clones the array and returns elements between the elements.
const animals = ['ant', 'bison', 'camel', 'duck', 'elephant'];
console.log(animals.slice(2));
// => ["camel", "duck", "elephant"]
console.log(animals.slice(2, 4));
// => ["camel", "duck"]
console.log(animals.slice(1, 5));
// => ["bison", "camel", "duck", "elephant"]