Some JavaScript Methods

Shaikh Jamil AL-Razi
3 min readMay 5, 2021

1. In JavaScript what is the use of the charAt() method…?

Ok let us say that a name variable jamli is given to you and given you the instruction to show the “l” from that name so you can use charAt() method for bringing out “l” from the name variable.

const name = ‘jamil’;
const index = 4;
console.log(`The character at index ${index} is ${name.charAt(index)}`); //’The character at index 4 is l’

2. Some words about includes() method.

The includes() method is a case-sensitive search. In a text if you are searching for a word like “cat” it has to be “cat” it cannot be “Cat”. includes() method returns true of false so “cat” !== “Cat”.

const text = ‘Their is a cat here’;
const word = ‘cat’;
console.log(`”${word}’s” ${text.includes(word) ? ‘are’ : ‘are not’} loved by all`); //”cat’s” are loved by all

3. Difference between startsWith() and endsWith() method.

The main difference between the startsWith() and endsWith() method is that if you are searching for a word or a single string at the start you will have to use the startsWith() method if at the end you will have to use endsWith() method. both methods return a boolean value.

const word = ‘Saturday’;
console.log(word.startsWith(‘S’)); // true
console.log(word.endsWith(‘y’)); // true

4. Some factors of trim, trimStart & trimEnd methods in JavaScript.

trim() method removes whitespace from both ends.
trimStart() methon removes whitespace from start point.
trimEnd() methon removes whitespace from end point.

const txt = ‘ foo ‘;
console.log(txt.trim()); // ‘foo’
console.log(txt.trimStart()); // ‘foo ‘
console.log(txt.trimEnd()); // ‘ foo’

5. Use of Math.abs() function in JavaScript.

The Math.abs() function returns the absolute value of a number some examples are given.

Math.abs(‘-10’); // 1
Math.abs(-20); // 2
Math.abs(null); // 0
Math.abs(‘’); // 0
Math.abs([]); // 0
Math.abs([2]); // 2
Math.abs([1,2,3]); // NaN
Math.abs({}); // NaN
Math.abs(‘string’); // NaN
Math.abs(); // NaN

6. Difference between Math.ceil(), Math.floor() & Math.round() .

Math.ceil() function rounds a number up to the next largest integer number.
Math.floor() function rounds a largest integer less than or equal to a given number.
Math.round() function returns the value of a number rounded to the nearest integer.

console.log(Math.ceil(.85)); // expected output: 1
console.log(Math.floor(4.15));// expected output: 4
console.log(Math.round(4.49), Math.round(4.51)); // expected output: 4 5

7. Math.Max() & Math.Min() functions.

Math.Max() returns the highest number from given as input parameters. Math.Min() returns the lowest number from given as input parameters.

console.log(Math.max(1, 3, 2));
// expected output: 3

console.log(Math.min(1, 3, 2));
// expected output: 3

8. What is Math.random() function in JavaScript.

Math.random() returns a floating-point number from 0 to <1. It can be scale to desired range. it cannot be chosen or reset by the user.

console.log(Math.random()); // floating-point number is going shown randomly

function randomNumber(max) {
return Math.floor(Math.random() * max);
}

console.log(randomNumber(3)); // expected output: 0, 1 or 2

9. Use of JavaScript Reduce method in Array.

Reduce is a method that can be difficult to understand There are a lot of benefits to understanding reduce as it is often used in summing a number array. The reduce ethod executes a provided function for each value of the array (from left-to-right).The reducer function takes four arguments:

Accumulator
Current Value
Current Index
Source Array

const arrayNumbers = [1, 2, 3, 4];
// 1 + 2 + 3 + 4
console.log(arrayNumbers.reduce((accumulator, currentValue) => accumulator + currentValue));
// expected output: 10

10. forEach() methods use in JavaScript.

The forEach() method is an array method. when it is called it goes throw all the elements in an array.There is no way to stop or break a forEach() loop other than by throwing an error.

const array= [‘jamil’, ‘noor’, ‘nobin’];
array.forEach(el => console.log(el));

// expected output: “jamil”
// expected output: “noor”
// expected output: “nobin”

--

--