Swapna Kumar Panda Profile picture
| Tech Writer, Educator | Python, Java, JavaScript, SQL | DSA, Development | Free Resources, AI Tools | Other Version: @therealswapna | Building @JabardastDEV |

Mar 13, 2022, 25 tweets

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.

Keep scrolling