A short review of some JavaScript Math and Array methods
In our daily life, we have to perform many operations in our calculations. Some of them are done by digital devices which are running with a programming language and the rest of them done by doing the calculation in hand. Today we are going to learn how the JavaScript programming language did these operations. let’s get started.
1. Abs method
Abs is an acronym for absolute. This method returns the positive value always. We may know that the mathematical term |x|=x and |-x|=x. The Abs method does the pretty same thing. Here the programming style example that would help you with the uses of that method.
Syntax: abs(value)
const x=5;const y=-6;console.log(abs(x)); //output should be 5console.log(abs(y)); // output should be 6
note: abs method does the same thing for the floating-point number such as 5.7 or -7.9
2. Ceil method
The ceil method makes the number its closest highest. Suppose for positive value, you pass 5.03,5.5 or 5.7 as an argument to this method then it returns 6. For a negative value, if you pass -5.03,-5.5, or -5.7 as an argument to this method then it returns -5.
Syntax : ceil(value)
const x = 5.03;const y = -5.5;console.log(ceil(x)); //output should be 6console.log(ceil(y)); //output should be -5
3. Floor method
The floor method makes the number its closest lowest. Suppose for positive value, you pass 5.03,5.5 or 5.7 as an argument to this method then it returns 5. For a negative value, if you pass -5.03,-5.5, or -5.7 as an argument to this method then it returns -6.
Syntax : floor(value)
const x = 5.03;const y = -5.5;console.log(floor(x)); //output should be 5console.log(floor(y)); //output should be -6
4. Min method
The method min is for finding the minimum one or the lowest number among numbers. It is one kind of searching for the lowest values from the numbers.
Syntax: min(num1, num2, num3,……, numN)
const lowest = min(2, -4, -5.6, 3.5, 6);console.log(lowest); //output should be -5.6// if you have an array of numbersconst a = [3, -4, -5, -3.5, 6];const lowest = min(…a);console.log(lowest); // output should be -3.5
5. Max method
The method max is for finding the maximum one or the highest number among numbers. It is one kind of searching for the highest values from the numbers.
Syntax: max(num1, num2, num3,……, numN)
const highest = max(2, -4, -5.6, 3.5, 6);console.log(highest); //output should be 6// if you have an array of numbersconst a = [3, -4, -5, -3.5, 9];const highest = max(…a);console.log(highest); // output should be 9
From here we will discuss array(collection of numbers or strings) methods
1.Concat method
The word concat comes from concatenation means adding two or more arrays and returns a new array. here is the example
Syntax: array1.concat(array2)
const array1 = [1, 2, 3, 4, 5];const array2 = [7, 8, 9];const newArray = array1.concat(array2);console.log(newArray) ; // output [1, 2, 3, 4, 5, 7, 8, 9]
2. Pop method
The pop method pops the very first element from the array and array length is decreased by one.
syntax: arrayName.pop();
const a = [3, 5, 2, -5, 0, 1];const b = a.pop();console.log(b); // output should be 3;console.log(a); // a array becomes [5, 2, -5, 0, 1]
3. Push method
The push method uses to push the element into the last of the array and array length is increased by one.
syntax: arrayName.push(element);
const a = [3, 5, 2, -5, 0, 1];a.push(17);console.log(a); // a array becomes [5, 2, -5, 0, 1,17]
4. Filter method
The filter method is used for filtering elements from the collection(array) of numbers or strings. Here the filter is done under conditions. Suppose an array is given [1,2,3,4,5] and you want numbers greater than 3 then the filter method returns [4,5]. The filter method returns the elements which satisfy the conditions as an array.
Syntax: array.filter(element => conditions);
const a = [‘rohim’, ‘korim’, ‘jalal’, ‘toyaha’, ‘kodu’]const b = a.filter(name => name.length == 5)console.log(b); // output looks like [‘rohim’, ‘korim’, ‘jalal’]
5. Slice method
If you want to slice the collection or string (array), the slice method would help you. You have to just tell the slice method at which to begin, sometimes you can slice the array into a specific portion by telling starting index and ending index. By default, the slice method returns the existing array.
Syntax: array.slice(), array.slice(startIndex), array.slice(startIndex, endIndex)
const a = [‘rohim’, ‘korim’, ‘jalal’, ‘toyaha’, ‘kodu’]const x = a.slice()console.log(x); // [‘rohim’, ‘korim’, ‘jalal’, ‘toyaha’, ‘kodu’]const x = a.slice(3, 4)console.log(x); // [‘toyaha’, ‘kodu’]const x = a.slice(2)console.log(x); // [ ‘jalal’, ‘toyaha’, ‘kodu’]