Top 10 useful Function you Should Learn javascript in 2021

Hb Hridoy
2 min readMay 5, 2021

Most useful Javascript User-define function

1.Math.round()

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Math.ceil()</h2>

<p>Math.round() rounds a number <strong>up</strong> to its nearest integer:</p>

<p id=”math”></p>

<script>
document.getElementById(“math”).innerHTML = Math.round(99.99);
</script>

</body>
</html>

Output:-

JavaScript Math.round()

Math.round() rounds a number up to its nearest integer:

100

2. Math.floor()

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Math.floor()</h2>

<p>Math.floor(x) returns the value of x rounded <strong>down</strong> to its nearest integer:</p>

<p id=”math”></p>

<script>
document.getElementById(“math”).innerHTML = Math.floor(9.9);
</script>

</body>
</html>

Output:-

JavaScript Math.floor()

Math.floor(x) returns the value of x rounded down to its nearest integer:

9

3.Math.sign()

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Math.sign()</h2>

<p>Math.sign(x) returns if x is negative, null or positive:</p>

<p id=”math”></p>

<script>
document.getElementById(“math”).innerHTML = Math.sign(8);
</script>

</body>
</html>

Output: -

JavaScript Math.sign()

Math.sign(x) returns if x is negative, null or positive:

1

3.Math.random()

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Math.random()</h2>

<p>Math.random() returns a random number between 0 (included) and 1 (excluded):</p>

<p id=”math”></p>

<script>
document.getElementById(“math”).innerHTML = Math.random();
</script>

</body>
</html>

Output:-

JavaScript Math.random()

Math.random() returns a random number between 0 (included) and 1 (excluded):

0.6157665775209555

4. Finding a String in a String

var str = “Please locate where ‘locate’ occurs!”;
var pos = str.indexOf(“locate”);

Output

7

5. Searching for a String in a String

var str = “Please locate where ‘locate’ occurs!”;
var pos = str.search(“locate”);

Output

7

6. The slice() Method

var str = “Apple, Banana, Kiwi”;
var res = str.slice(-12, -6);

Output

Banana

7. Replacing String Content

str = “Please visit Microsoft!”;
var n = str.replace(“Microsoft”, “W3Schools”);

Output

Please visit w3School

8. Converting to Upper

var text1 = “Hello World!”;
var text2 = text1.toUpperCase();

Output

HELLO WORLD!

9.Converting to Lower Case

var text1 = “Hello World!”;
var text2 = text1.toLowerCase();

Output

hello world!

10. String.trim()

This function help to remove to the array.

var str = “ Hello World! “;
alert(str.trim());

Output

Hello World!

--

--