A Short List of Methods Every JavaScript Developer Must Know

A List of String, Number, Math and Array Methods

Muhammad Rabiul Alam
4 min readMay 5, 2021

--

String Methods: includes(), replace(), slice(), split()

includes(): This method is used to determine whether a search string is present in a given string and returns true if found and false if not. The includes() is a case sensitive method.

var str = “Hello world, from the medium blog.”;
var n = str.includes(“medium”);
console.log(n) // the output is 'true'
var n = str.includes(“Medium”);
console.log(n) // the output is 'false' due to case sensitivity

replace(): This method searches for a given string or a regular expression and returns the output string with replaced values. If the searching value is not a regular expression with global modifier (/g), then only the first instance of the search value will be replaced.

var str = "This method searches for a given string.";
var res = str.replace("method", "function");
console.log(res) // This function searches for a given string.

slice(): To get a portion of a string use this method with the starting index and the ending index (optional) as input parameters. The result will be a new string starting from the start parameter to the end parameter (or to the end of the string if end is not specified). Negative values can be used to specify the index from the end of the string.

var str = "Hello world!";
var res = str.slice(3, 8);
console.log(res); // lo wo
var res = str.slice(3);
console.log(res); // lo world!
var res = str.slice(-4);
console.log(res); // rld!

split(): Sometimes its very helpful to breakdown the string as small as possible and this split() method comes in rescue. It takes a split separator as parameter on which it will split the string and return an array of substrings, if the split separator is omitted then an array of a single string is returned.

var str = "How are you?";
var res = str.split("");
console.log(res); // ['H', 'o', 'w', ' ', 'a', 'r', 'e', ' ', 'y', 'o', 'u', '?']
var res = str.split();
console.log(res); // ['How are you?']
var res = str.split(" ");
console.log(res); // ['How', 'are', 'you?']

Number Methods: isNaN(), parseInt(), random()

isNaN(): JavaScript Not-a-Number (NaN) is a type of number. The isNaN() method is used to check, if a number is NaN. If a value is of type ‘Number’ and equates to ‘NaN’, then the output is true, and false otherwise.

console.log(Number.isNaN(NaN)) //true
console.log(Number.isNaN(0 / 0)) //true
console.log(Number.isNaN(123)) //false
console.log(Number.isNaN(true)) //false

random(): The Math.random() method returns values from 0 to less than 1. Following code returns a random number between 1 and 100:

Math.floor((Math.random() * 100) + 1);

parseInt(): This method parses a string and returns an integer based on the radix (or, base) in second parameter. If the starting character can not be converted to a number, then the output is NaN.

console.log(parseInt("10.33")) // 10
console.log(parseInt("10", 8)) // 8
console.log(parseInt("He was 40")) // NaN

Array Methods: filter(), map(), reduce(), slice(), splice()

filter(): Need to filter out some values from an array based on some conditions? filter() method comes in action. It returns an array with the values remains after filtering. It doesn’t modify the original array.

const nums = [2,4,1,8,6];
// get the even numbers
const newNums = nums.filter(num => num % 2 === 0);
console.log(newNums) // [2,4,8,6]

map(): To apply some function on every element of an array in an orderly way, we can use map() method. It results in a new array with the expected changes, though map() does not apply the function on elements without values.

const nums = [2,4,1,8,6];
const newNums = nums.map(num => num * num);
console.log(newNums) // [4, 16, 1, 64, 36]

reduce(): This method works on every single element of an array and provides single value result not an array unlike map(), filter() or other array methods, thus it is named so.

const nums = [2,4,1,8,6];
const total = nums.reduce((sum, num)=> {
return sum + num;
}, 0);
console.log(total) // 21

slice(): This method is used to get a part of an array with a start index and an end index (though the sliced array contains value from start to one index before the end index). If the end index is not provided, then the output array contains values from start index to the end of the array.

var chars = ["A", "B", "C", "D", "E"];
console.log(chars.slice(1, 3)); // ["B", "C"]
console.log(chars.slice(2)); // ["C", "D", "E"]

splice(): The splice() method adds/removes items to/from an array, and returns the removed item(s). This method changes the original array.

// At position 1, add the new items, and remove 3 item
var chars = ["A", "B", "C", "D", "E"];
console.log(chars.splice(1, 3, "F", "G")); // ["A", "F", "G", "E"]

There are lots of other methods that are very useful to the developers. I just tried to put some of most important methods above. For further look into the JavaScript methods please goto the following links:

MDN:

W3Schools:

--

--

Muhammad Rabiul Alam

I am a Front-End Developer. I love to work with JavaScript, ReactJS, NodeJS, React Native and other front-end and/or JavaScript based technologies.