Learn Javascript In Fun Way

Nazmul Huda
3 min readMay 5, 2021

Javascript is a scripting language that allows us to do lot’s of features in “Web Page”.

Javascript is the most used programming language in recent years. Many of us have a great fear of some javascript functions and methods. Let’s talk about some of them….

01.Difference Between Math.floor and Math.round?

Although Math.floor function and Math.round looks like they are mostly doing similar jobs but there’s a little bit of difference between them.

First try to understand Math.floor();

Let’s consider we took a variable

var equation = 3/2;

console.log(equation); //returns 1.5

The result we will get by it is 1.5.

But when we will do the floor function …

console.log(Math.floor(equation)); // returns 1

It will not gonna give us a floating-point value, it will give us a round value like 1.

Ok now we understood Math.floor() function.

Now try to understand Math.round() with the same equation.

console.log(Math.round(equation)); //returns 2

As the same of Math.floor() function Math.round() is also not giving us floating-point value. It’s giving us an integer number.

So the difference we got between Math.floor() & Math.round() is floor always rounds down to the nearest integer & Math.round() always rounds up depending on which side of the 0.5 number falls on.

2. charAt() function

Firstly let’s simplify the charAt word. “Charis an abbreviation of “Character”. Now if we read the function like characterAt there’s no problem seen to me. This function will find the character using the index number.

Let’s consider

var x = “hello world”;

console.log(x.charAt(4)); // returns 0

This function returns ‘o’ because on x variable the index of 4 is character o and it returns the character. This is how this charAt() function works.

3.concat() method

In javascript concat() is a kind of guy who loves to get connected to everyone. Using concat() we can join two or more strings. It doesn’t changes the existing string.

Let’s see an example of it:

var array1 = [‘a’, ‘b’, ‘c’];

var array2 = [‘d’, ‘e’, ‘f’];

var array3 = array1.concat(array2);

console.log(array3);

// expected output: Array [“a”, “b”, “c”, “d”, “e”, “f”]

4.trim() function

trim() function in javascript simply removes the white spaces from the right and left side of a string.

Let’s consider

var x= ‘ hello world ’;

console.log(x.trim()); //returns ; ‘hello world’

Here it returns ‘hello world’ because trim() function removes all the white spaces from the right and left part of the string.

5.indexOf() function

This function simply gives us the index of array elements or strings or which index we want to know. If we want to know something but the indexOf() function didn’t get what we wants to know than it will return -1.

var colors = [“green”,”blue”,”yellow”];

console.log(colors.indexOf(“blue”); //returns 1

Because blue is the first index value of the colors array.

6.replace()

This method replace a part of value in a string and returns a complete new string.

Let’s consider :

var x = “hello world”

var replace = x.replace(“world”,”mars”);// returns “hello mars”

7.filter()

The filter() method creates a new array with all elements that pass the test implemented by the provided function.

Example of Filter:

var words = [‘spray’, ‘limit’, ‘elite’, ‘exuberant’, ‘destruction’, ‘present’];

var result = words.filter(word => word.length > 6);

console.log(result); //returns [“exuberant”, “destruction”, “present”]

8. find()

find() method just returns the first value of provided array from which are passed in the testing function.If it get no passed value then it will return undefined.

var array1 = [5, 12, 8, 130, 44]; var found = array1.find(element => element > 10);

console.log(found) //returns 12

Because 12 is the only first element that matches with the condition and passed the testing function.

9. toLowerCase()

This method converts the string value to lowercase letters. And returns the string to lowercase letter.

Let’s consider:

var name = ‘RIMON’;

console.log(name.toLowerCase());//returns ‘rimon’

10.toUpperCase()

This method converts the string value to uppercase letters and finally returns the string to uppercase letter.

Let’s consider :

var x = ‘rimon’;

console.log(x.toUpperCase()); //returns ‘RIMON’

--

--