Javascript: How much Math do you do?

Yes. Javascript provides the built-in object Math. It has several useful methods to work on numbers. Let’s go through, which we really use.

Round to integer

round() – ➡️ Rounds to its nearest integer.

ceil() – ⬆️ Rounds up to its neares…

Yes. Javascript provides the built-in object Math. It has several useful methods to work on numbers. Let’s go through, which we really use.

Round to integer

  1. round() – ➡️ Rounds to its nearest integer.
  2. ceil() – ⬆️ Rounds up to its nearest integer.
  3. floor() – ⬇️ Rounds down to its nearest integer.
const num = 10.5;

console.log(Math.round(num)); // 11
console.log(Math.ceil(num)); // 11
console.log(Math.floor(num)); // 10

const num1 = 10.4;

console.log(Math.round(num1)); // 10
console.log(Math.ceil(num1)); // 11
console.log(Math.floor(num1)); // 10

Remember like this:

round – 10.0 – 10.4 -> 10 and 10.5 -> 11.0 -> 11

ceil (up) – 10.1 – 10.9. -> 11

floor (down) – 10.1 – 10.9 -> 10

The same applies to negative numbers.

Return Integer part

trunc() – returns integer part as it is.

const num = 10.547;

console.log(Math.trunc(num));
> 10

Return absolute number

abs() – returns positive number.

const num = 10.547;
const num1 = -10.547;

console.log(Math.abs(num)); // 10.547
console.log(Math.abs(num1)); // 10.547

power & sqrt

Hope no explanation is needed.

const num = 9;

console.log(Math.pow(num, 2)); // 81
console.log(Math.sqrt(num)); // 3

max & min

Returns Max number and min number in the given list.

console.log(Math.max(3,4,5,1,2)); // 5

console.log(Math.min(3,4,5,1,2)); // 1

random number

random() – method used to return random number between 0 – 1.

console.log(Math.random()); // 0.34609498534013383

Few more methods of Math object that we rarely use them.

Those are:

Math.sign() – returns -1 (negative), 0 (null), 1 (positive) based on the number provided.

Math.sin() & Math.cos() – trignometric operations – sin 90 – 1.

Math.log(x) & Math.log2() & Math.log10() – returns logarthmic conversion.

Thanks 😊


Print Share Comment Cite Upload Translate Updates
APA

Kurapati Mahesh | Sciencx (2022-10-01T16:12:19+00:00) Javascript: How much Math do you do?. Retrieved from https://www.scien.cx/2022/10/01/javascript-how-much-math-do-you-do/

MLA
" » Javascript: How much Math do you do?." Kurapati Mahesh | Sciencx - Saturday October 1, 2022, https://www.scien.cx/2022/10/01/javascript-how-much-math-do-you-do/
HARVARD
Kurapati Mahesh | Sciencx Saturday October 1, 2022 » Javascript: How much Math do you do?., viewed ,<https://www.scien.cx/2022/10/01/javascript-how-much-math-do-you-do/>
VANCOUVER
Kurapati Mahesh | Sciencx - » Javascript: How much Math do you do?. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/10/01/javascript-how-much-math-do-you-do/
CHICAGO
" » Javascript: How much Math do you do?." Kurapati Mahesh | Sciencx - Accessed . https://www.scien.cx/2022/10/01/javascript-how-much-math-do-you-do/
IEEE
" » Javascript: How much Math do you do?." Kurapati Mahesh | Sciencx [Online]. Available: https://www.scien.cx/2022/10/01/javascript-how-much-math-do-you-do/. [Accessed: ]
rf:citation
» Javascript: How much Math do you do? | Kurapati Mahesh | Sciencx | https://www.scien.cx/2022/10/01/javascript-how-much-math-do-you-do/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.