25 super useful methods in JavaScript
โฉ
๐ญ Why this thread?
โ If you have just started learning JavaScript, you might have felt exhausted by seeing so many methods.
โ If you are simply going through those methods without knowing their use cases, it's going to be tough for you later.
โ Let's learn use cases.
๐ Table of Contents
โ Array.prototype's
โ forEach()
โ map()
โ reduce()
โ every()
โ filter()
โ find()
โ slice()
โ splice()
โ push()
โ pop()
โโ shift()
โโ unshift()
โโ indexOf()
โโ findIndex()
โโ sort()
โ String.prototype's
โโ toLowerCase()
โโ toUpperCase()
โโ substring()
โโ indexOf()
20. charAt()
โโ trim()
โ Math's
โโ floor()
โโ random()
โ Global
โโ setTimeout()
โโ setInterval()
โ Array.prototype.forEach()
โ Use Case
โ To iterate through each element in the array
โ Perform a task on each element
โ Not suitable if task is supposed to return some value
โฉโฉ forEach() is also available in Set.prototype and Map.prototype
โ Array.prototype. map()
โ Use Case
โ To iterate through each element in the array
โ Perform a task on each element which returns a value
โ Returns a new array with all returned values
โ Not suitable if task doesn't return any value
โ Array.prototype.reduce()
โ Use Case
โ To iterate through each element in the array
โ Perform a task on each element which accumulates the previous returned value with current element to return a new value
โ Example
โ Sum of all items
โ Max of all items
โ Array.prototype.every()
โ Use Case
โ To iterate through elements in the array until a certain condition is not met
โ The task performed on each element must return a boolean value
โ Stops iterating when condition is not met. Hence, it's not suitable for skipping.
โ Array.prototype.filter()
โ Array.prototype.find()
โ Use Case
โ To find element(s) which match a criteria
โ Use find() to find the first matched element
โ Use filter() to find all matched elements
โฉโฉ filter() returns a new array of matched elements
โ Array.prototype.slice()
โ Use Case
โ To fetch a sub-array from a larger array
โฉโฉ slice() returns a new array, doesn't modify the existing array
โ Array.prototype.splice()
โ Use Case
โ To replace a portion of the array with new elements
โฉโฉ splice() modifies the existing array
Array.prototype's
โ push()
โ pop()
โโ shift()
โโ unshift()
โ Use Case
โ Use push() and pop() to insert/remove elements from the end
โ Use shift() and unshift() to insert/remove elements from the start
โฉโฉ These methods modify the existing array
Array.prototype's
โโ indexOf()
โโ findIndex()
โ Use Case
โ To find index of the first matched element
โ indexOf() does exact match (===)
โ findIndex() allows for custom match
โโ Array.prototype.sort()
โ Use Case
โ To sort all elements of an array in some order
โ Provide a comparator to define custom order
โ By default sorting order is lexical
โฉโฉ This method modifies the existing array
String.prototype's
โโ toLowerCase()
โโ toUpperCase()
โ Use Case
โ To convert entire string to lowercase alphabets, use toLowerCase()
โ To convert entire string to uppercase alphabets, use toLowerCase()
โฉโฉ These methods returns a new string
โโ String.prototype.substring()
โ Use Case
โ To fetch a part of the string between 2 indexes
โฉโฉ substring() returns a new string
โโ String.prototype.indexOf()
โ Use Case
โ To find the very first occurrence of a "substring" in the original string
โ We can also mention from which index the occurrence should be checked
20. String.prototype.charAt()
โ Use Case
โ To fetch the character at a specific position of a string.
โ The character fetched is in UTF-16 and returned as a string
โโ String.prototype.trim()
โ Use Case
โ To remove whitespace from both ends of a string
โฉโฉ trim() returns a new string
โโ Math.floor()
โ Use Case
โ To get the largest integer less than or, equals to the given number
โโ Math.random()
โ Use Case
โ To get a floating point pseudo random number in the range of 0 to less than 1
โ It can be multiplied by any number to make a random number being generated in the range of 0 to less than that number
โโ setTimeout()
โ Use Case
โ To execute a function or, piece of code after a timer expires
โ The code is executed only for once.
โ It is an asynchronous function. It shouldn't be used where pausing of execution is intended.
โโ setInterval()
โ Use Case
โ To execute a function or, piece of code repeatedly with a fix time delay
โ The code is ensured to be executed each time after the time delay. But not "exactly" after the time delay.
โฉโฉ Cancel further execution using clearInterval()
๐ญ Feedbacks
โง I have picked 25 out of 100s of methods available. My focus was to show you a "learning method" which you may apply wherever you wish.
โง Share your feedbacks on these. It would definitely help me to share better contents with you.
End of ๐งต
If you are a new learner or, someone who finds difficulties in learning JavaScript, you have reached to right person here.
You may like to go through "JavaScript" & "JavaScript Infographics" moments to see what I share regularly.
To never miss any, Follow Me โ
Share this Scrolly Tale with your friends.
A Scrolly Tale is a new way to read Twitter threads with a more visually immersive experience.
Discover more beautiful Scrolly Tales like this.