The most useful JavaScript string method and JavaScript math objects

Fahad Ezahar
3 min readNov 2, 2020

JavaScript String Method :

1. charAt() Method : A specific character can be taken from the string. The number you enter in the index parameter will return the index of the number of characters.

Example : Const greetings = ‘Good Morning’;

console.log(greetings.charAt(5));

// expected output: “M”

2. concat() Method : The concat method is used to join two or more strings.

Example :

const firstName = ‘fahad’;

const middleName = ‘mohammed’;

const lastName = ‘ezahar’;

console.log(firstName.concat(‘ ‘,middleName,’ ‘,lastName));

// expected output: “fahad mohammed ezahar”

3. includes() Method : Using the included method, checks whether one string contains another specified string. This method returns true if the string contains the characters, and false if not.

Example :

const greetings = ‘Good Morning’;

const word = ‘hello’;

console.log(greetings.includes(word));

// expected output:false

4. indexOf(string,start)Method : To find the string. If you enter the word or the index of the string in the string parameter, it will find out the position of that word in the whole string and the number in the index. And the start parameter is optional. If you give it (integer) then the number will start searching from where it will be indexed. For example, if you give 5, the search will start from the element with the number 5 index.

Example : const greetings = ‘how are you’;

console.log (greetings.indexOf(‘are’));

// expected output: 4

5. slice(start, end) Method : To extract the substring from the string. At the start you have to give the integer which will be exactly where to start and you have to give the end where another integer will take.

Example : const greetings = ‘how are you today ?’;

console.log(greetings.slice(4,7))

// expected output: “are”

6. split() Method : The split() method is used to split a string into an array of substrings, and returns the new array.

Example :

const greetings = “How are you ?”

const res = greetings.Split(“ “)

console.log(res);

// expected output: [“How”, “are”, “you”, “?”]

7.Replace () method : can be used to replace any string / word in a string. This method may take two parameters.

i. regEx: Give any regular expression in this parameter. Will replace the substring that matches.

ii. replaceText : The string / word that you want to replace. Must be given inside the quotation.

Example :

const greetings = ‘how are you fahad ?’

const regExp = /fahad/

const res = greetings.replace(regExp,’ezahar’);

console.log(res);

// expected output: “how are you ezahar ?”

Javascript Math objects :

Math objects can be used to work with mathematical terms.

Math objects include many mathematical methods.

1. Math.min () and Math.max () methods : The Math.min () and Math.max () methods are used to find the highest and lowest numbers:

Example :

Math.min(0, 15, 10, 5, -1); // returns -1

Math.max(0, 15, 10, 5, -1); // returns 15

2. Math.random method : returns a random number

3. Math.round () method : can be used to find the round off value of a number

Example:

Math.round(4.7); // returns 5

Math.round(4.4); // returns 4

4. Math.ceil () method : Using the Math.ceil () method, a number is rounded to the nearest maximum number.

Example:

Math.ceil(4.4); // returns 5

5. Math.floor () method : Using the Math.floor () method, a number is rounded to the nearest lowest number.

Example:

Math.floor(4.7); // returns 4

Math.floor () and Math.random () methods can be used together to create random numbers between 1 and 10

Example:

Math.floor(Math.random() * 11); // returns a random number between 0 and 10

--

--

Fahad Ezahar
0 Followers

I continue my learning on the challenges of becoming a professional web developer.