Some important method of Javascript

Tanjin Ahamed
2 min readMay 6, 2021

What is JavaScript?

JavaScript is a scripting or programming language that allows you to implement complex features on web pages. JavaScript can update and change both HTML and CSS. JavaScript can calculate, manipulate and validate data.

Math.random()

Math.random() method to create a function that will return a random integer between two values.

Example:
const randomNumber = Math.random() * 100;

const output = Math.round(randomNumber);

console.log(“The random number is:”, output);

Math.round()

Math.round() is a function that is used to return a number rounded to the nearest integer value.

Example:
const roundNumber = Math.round(25.8);

console.log(“The round number is:”,roundNumber)

Filter()

The filter() array method creates a new array with elements that fall under a given criteria from an existing array.

Example:
const numbers = [1,3,5,7,9,11,13,15];

const filterNumber = numbers.filter(number => number > 3);

console.log(“The filter number is: “,filterNumber);

Map()

The map() is a collection of elements where each element is stored as a key, value pairs.

Example:
const number = [4, 8, 12, 24, 48];

const newNumber = number.map(x => x * 2);

console.log(“New Number is: “,newNumber);

Reduce()

The reduce() method executes a reducer function on each element of the array, resulting in a single output value.

Example:
const number = [2, 4, 8, 16];

const reduce = (oldNumber, currentNumber) => oldNumber + currentNumber;

console.log(“Total value: “,number.reduce(reduce));

console.log(“New value: “,number.reduce(reduce, 10));

Push()

The push() method allows us to add new items to the last position in an array.

Example:
const numbers = [3, 5, 7, 9, 11, 13];

console.log(“Old numbers: “,numbers)

numbers.push(15, 17, 19, 21);

console.log(“New numbers: “,numbers);

isNaN(parseFloat(“123”));
Output(false)

String repeat() method

The repeat() method returns a string that has been repeated a desired number of times.

const str = “Maria”

colsole.log(str.repeat(3))

Output: MariaMariaMaria

String trim() method

The trim() method removes whitespace from a string. if any strings have whitespace at start or end then this method removes both whitespaces.

const str = “Maria”

colsole.log(str.trim(3))

Output: Ma

String slice() method

The slice() method is used to fetch the part of the string and return the new string. Use the start and end parameters to specify the part of the string you want to fetch. here the first character has the position 0, the second has position 1, and so on.

const str = “Maria”

colsole.log(str.slice(0,3))

Output: Mar

--

--