DEV Community

Cover image for 5 Extremely useful javascript methods that You might not know
Rafael Sant'Ana
Rafael Sant'Ana

Posted on

5 Extremely useful javascript methods that You might not know

Hi ! Recently I've been studying more about some Javascript methods and I found some really interesting ones, I hope it can help you to code faster and easier ! Soo, Let's begin:

PadStart()

Padstart demo

By looking to the picture You probably understood but if You didn't, let me explain it better:
padStart is a string method, it means it can only be used with strings. Having set that, it works at the following way: the string on the second parameter will be added at the beggining of the string if its length is lower than the number in first parameter.
In other words, in the first parameter You tell the max length and in the second one, what the method will fill the string with.

padEnd()

PadEnd example

It works just like padStart, but the difference is that instead of adding a text at the beggining of the string, it adds a text at the end.

reduce()

Reduce example

Reduce is an Array manipulation method, it needs a function as the first parameter, this function has two parameters, commonly named 'accumulator' and 'currentValue', and returning a value inside this function will make 'accumulator' have this value. The second parameter defines the start value of accumulator.

forEach()

ForEach Example

forEach will execute a function for each value in the array. In this example, we sum 3 numbers to each element in the array.

toFixed()

toFixed example

This function removes the decimal part of a number, but it can also limit how many numbers there are in the decimal part of a number, as in the second example.

toLocaleString()

toLocaleString example

This function is a little more complex, but it basically converts a number to a formatted string. In the first parameter it takes a locale (For example, en-US). The second parameter is opcional but it takes format options, you can check them out here.

Soo, that's it! I hope it helped you guys!

Top comments (0)