JavaScript Basic Array and Math Function

Kazi Imam Hossain
2 min readNov 2, 2020

Array Functions

An array is a special variable, which can hold more than one value at a time. Here I will discuss some basic array functions.

let car = ['BMW', 'Audi', 'Ferrari'];
// ["BMW", "Audi", "Ferrari"]

1. Push

If we want to add an item at the end of an array then we will use the push function.

car.push('Lamborghini');
// ["BMW", "Audi", "Ferrari", "Lamborghini"]

2. Pop

If we want to remove an item from the end of an array then we will use the pop function.

car.pop();
// ["BMW", "Audi", "Ferrari"]

3. Shift

To remove an item in front of an array we have to use the shift function.

car.shift();
// ["Audi", "Ferrari"]

4. Unshift

To add an item in front of an array we have to use the unshift function.

car.unshift("Land Rover");
// ["Land Rover", "Audi", "Ferrari"]

5. IndexOf

The indexOf function is used to find the index number of values from an array.

console.log(car.indexOf("Ferrari"));
// 2

Math Functions

Math is a built-in object that has properties and methods for mathematical constants and functions. It’s not a function object.
Math works with the Number type. It doesn’t work with BigInt.

1. Abs

The Math.abs() function converts a number to an absolute value.

console.log(Math.abs(-25));
// 25
console.log(Math.abs("-5 * 2"));
// 10

2. Ceil

The Math.ceil() function round a decimal number upward to the nearest integer.

console.log(Math.ceil(7.2));
// 8
console.log(Math.ceil(-7.9));
// -7

3. Floor

The Math.floor() function round a decimal number downward to its nearest integer.

console.log(Math.floor(7.2));
// 7
console.log(Math.floor(-7.9));
// -8

4. Round

The Math.round() function returns a rounded number to its nearest integer number of a decimal number.

console.log(Math.round(7.2));
// 7
console.log(Math.round(7.9));
// 8
console.log(Math.round(8.5));
// 9
console.log(Math.round(-8.5));
// -8

5. Sqrt

The Math.sqrt() function returns a square root of a number. Though square root is not possible for the negative value its returns a NaN for negative values.

console.log(Math.sqrt(25));
// 5
console.log(Math.floor(-9));
// NaN

--

--

Kazi Imam Hossain
0 Followers

A curious JavaScript Lover as well as MERN stack developer. I know React, NodeJS, ExpressJS, MongoDB and continue deep learning on these.