Javascript Working With Numbers

JS Code Snippets Working With Numbers
Share:

About

In this code snippet, we’ll check out some of the Math class functions in Javascript.

More information about all the functions and constants here.

Let’s see the code example below.

Code:

//You can get constants from the Math class. 
let pi = Math.PI;

//Display a certain amount of decimal places.
console.log(pi);
console.log(pi.toPrecision(3)); 

//Round number.
console.log(Math.round(pi));
//Round up.
console.log(Math.ceil(pi));
//Round down.
console.log(Math.floor(pi));
//Get the absolute value of a number.
console.log(Math.abs(-10));

//Do exponentiation. 2^8
console.log(Math.pow(2,8));
//Get squre root of a number.
console.log(Math.sqrt(9));
//Gets base 2 log of 256.
console.log( (Math.log(256)/Math.log(2)) );

//Trigonometric functions.
console.log(Math.sin(90));
console.log(Math.cos(90))

Resulting output:

Share:

Leave a Reply

Your email address will not be published. Required fields are marked *

The following GDPR rules must be read and accepted:
This form collects your name, email and content so that we can keep track of the comments placed on the website. For more info check our privacy policy where you will get more info on where, how and why we store your data.

Advertisment ad adsense adlogger