1 minute read

Higher-order functions

console.log(String(6).padStart(3, "0")); // → 006

You can split a string on every occurrence of another string with split and join it again with join.

Advertisement

let sentence = "Secretarybirds specialize in stomping"; let words = sentence.split(" "); console.log(words); // → ["Secretarybirds", "specialize", "in", "stomping"] console.log(words.join(". ")); // → Secretarybirds. specialize. in. stomping

A string can be repeated with the repeat method, which creates a new string containing multiple copies of the original string, glued together.

console.log("LA".repeat(3)); // → LALALA

We have already seen the string type’s length property. Accessing the individual characters in a string looks like accessing array elements (with a caveat that we’ll discuss in Chapter 5).

let string = "abc"; console.log(string.length); // → 3 console.log(string[1]); // → b

Rest parameters

It can be useful for a function to accept any number of arguments. For example, Math.max computes the maximum of al l the arguments it is given.

To write such a function, you put three dots before the function’s last parameter, like this:

76

This article is from: