JavaScript Article

Towhidul S Islam
3 min readMay 5, 2021

JavaScript Article:

1. floor: JavaScript floor method is very useful for a programmer. If we have a decimal number, it can remove decimal number after the integer. Such as, If we divided into a number 3 and 2, the result is 1.5. But if we use floor method, the result would be 1. Because floor method removes the elements which is decimal. In the number 1.5 has .5 is decimal number and .5 was removed by the floor method. And answer is 1.

Example: console.log(3 / 2); Output: 1.5, not 1

console.log(Math.floor(3 / 2)); Output: 1

2. parseInt: ParseInt is a method which we can use for convert a string to integer number. Sush as, “150” is a string number but we want to get a integer number. How can we solve this problem? Solution is parseInt. We can use parseInt for convert string to integer number. You should provide 2nd argument always.

Example: parseInt(‘150’, 10); Output: 150

parseInt(‘010’, 10); Output: 10

3. NaN : Nan is a toxic like 0 x. When we use 0 x 2 then we get the result 0. NaN is similar to this. If we use NaN + 5, the output is NaN. Not only this but also if we use NaN x 10, we get the result as NaN. So we can called the NaN is toxic.

Example: NaN + 5; Output: NaN

NaN — 5; Output: NaN

NaN * 5; Output : NaN

NaN / 5; Output: NaN

4. Strings: We use strings so many reasons. It is very important for a programmer. If we use a strings and we need to know how long the strings is. How is it possible? Yes, it is simple. We can use length method for find how many letter use the strings. If a strings use 5 letter, output is 5

Example: ‘What’.length; Output: 4

We can also find specific letter of strings by declaring the letter position, replace one word to other word and we can do Uppercase to Lowercase.

Example: ‘What’.charAt(1); Output : “h”

‘hello, world’.replace(‘world’, ‘mars’); Output: “hello, mars”

‘hello’.toUpperCase(); Output: “HELLO”

5. trim: This is very useful things in coding. If we have enough space in our strings and we want to avoid this space then we can use trim. trim avoid the space in the strings.

Example: const myjob = ‘ Development ‘;

console.log(myjob);

Output: “ Development “;

And now we use trim to avoid the space using trim.

Example: const myjob = ‘ Development ‘;

console.log(myjob.trim); Output: “Development”;

When we use trim then we can avoid the space.

6. indexOf: We can use indexOf for search the specific word in a strings. If a strings is so long, no tention it can do work properly. You can know where the word is set on the strings.

Example: const str = “Hello world, welcome to the universe.”;

const n = str.indexOf(“welcome”);

console.log(n) Output: 13.

When we see the output we understands that after 13th word count is “welcome”.

7. forEach: forEach is used to divided a strings in the array. In a array many data has included step by step and this data is separated by forEach array. We can find the difference data in array by using forEach.

Example: const array1 = [‘x’, ‘y’, ‘z’];

array1.forEach(element => console.log(element));

Output: ‘a’

Output: ‘b” and Output is : ‘c’

8. sort: sort is very useful of a programmer’s life. We can see the sort the code many more. So we should know about sort. sort is a array which is decorate the array data in gradually.

Example: const months = [‘March’, ‘Jan’, ‘Feb’, ‘Dec’];

months.sort();

console.log(months);

Output: Array [“Dec”, “Feb”, “Jan”, “March”]

9. map: map is most useful method in programming. A map is used to separate data into the array. I can separate an array data and add them many things. If you want to add a array data in others you can do this. You can add two data gradually in array by using map. It is very useful to us.

Example: const array1 = [3, 5, 8, 32];

const map1 = array1.map(x => x * 2);

console.log(map1);

output: Array [6, 10, 16, 64]

10. slice method copies a given part of an array and returns that copied part as a new array. It doesn’t change the original array. The slice method selects the elements starting at the given start argument, and ends at, but does not include, the given end argument. slice remove data from the direction.

Example: constfruits = [“Banana”, “Orange”, “Lemon”, “Apple”, “Mango”];

const citrus = fruits.slice(1, 3);

console.log(citrus);

Output: [[ ‘Orange’, ‘Lemon’ ]

--

--

Towhidul S Islam
0 Followers

Hi! I am a professional Web Developer. I am working with JavaScrpt, React, Node , Express.js and MongoDB.