Necessary javascript math, number,string methods

The string is a very powerful fundamental functional datatype in programming. The string is a sequence of characters.it is used to represent the text form rather than numbers.in javascript, various string methods are used which make the developer’s life easier. some of the powerful javascript methods are given below:

1.charAt(): The charAt() method return individual characters of a string.it returns a new string.it takes an index as a parameter. if there is no index is provided as a parameter then the default value is 0.charAt() returns a string representing the character at the specific index. if the supplied index is out of range then javascript returns an empty value.

Syntax: charAT(index)

Example:

const name = “sama”;

const index = 3;

console.log(`${name.charAt(3)}`);

//expected value ‘a’

2.concat():The concat() method concatenates two different string into a new one.It took one or more strings as argument and returns a new string.it does not change or effect the original string.it just concates the string.if the passed arguments are not the string type concat() method convert them into string type and concates them.

Syntax:concat(str) or concat(str1,str2)

Example:

const firstName = “khadiza”;

const lastName = “sama”

console.log(firstName.concat(“ “,lastName);

//expected output khadiza sama

3.slice():slice() method took a portion of string and return it into new string.it does not change or affect the original string.it take two parameter as argument.one is the begin index and other is end index.the end index is optional.if the parameter is negative the position is counted from the end of the string.

Syntax:slice(begin index,end index)

Example:

const name = “khadiza morioum sama”

const newName = name.slice(8,-4)

console.log(newName);

//expected output morioum

NUMBER: the number is a primitive data type of javascript programming. Basically, it defines the number type value such as decimal, integer, float, etc. there are some methods of number. some of them are given below:

1.isNAN():isNAN() method is used to determine whether a value is not a number or not. it returns true or false as output. if the given value is NAN this function returns true otherwise false. the isNAN() function converts the tested value to a number, then tests it.

Syntax:isNAN(value)

2.parseFloat():parseFloat() is a method that parses string from an argument and returns it in a floating number. it took string as a parameter.if the number cannot be converted it returns a NAN.

Syntax:number.parseFloat(string)

example:

parseFloat(“45”) ;

//expected value 45

3.parseInt():parseInt() is a method that parses a string from an argument and returns it in a whole number. spaces are allowed but only the first number is returned.it took string as a parameter. if the argument is not a string then it can be converted into a string using tostring() method. if the number cannot be converted it returns a NAN.

Syntax: number.parseInt(string)

Example:

parseInt(“50.46”)

//expected value 50

MATH: It allows users to perform mathematical operations on numbers. it's not a functional object. it's not also a constructor type.it works with numbers. the are some methods of math. some of them are given below:

1.Math.abs():the abs() method returns a absolute value of a number.it does not matter if the passed value inside the abs() method is negative or positive.it always returns the absolute value. if the passed value is null then it returns NAN.

Syntax: math.abs(x)

Example:

Math.abs(-9);

//expected value 9

Math.abs(null)

//expected value NAN

2.Math.ceil():this function always returns a value that is rounded up to its nearest largest integer.if the passed value is null then I return 0.it does not give NAN error.

Syntax: Math.ceil(x)

Example:

Math.ceil(5.9)

//expected value 6

3.Math.round():this function take a floating point number as parameter and return it as nearest integer.round() is a static method of math.

Syntax: Math.round(x);

Example:

Math.round(5.7)

//expected value 6

4.Math.min(): this method returns the number with the lowest value. this is a static method. this takes numbers as parameters. if the parameter is not a number it converted it into a number but if it cant be converted then this method returns NAN.

Syntax:Math.min(value0,value1,….,valueN);

Example:

const a=4 ;

const b= 7;

const c = Math.min(a,b);

//expected value of c is 4;

this list covered some of javascript string,number and math methods.however many of other methods are not covered in this list such as indexOf,lastIndexOf,substr,math.floor,etc.

Hope you have enjoyed reading this article and found this article helpful.if you have enjoyed this then follow me on LinkedIn.