Working with Numbers in JavaScript

You already have basic idea of how numbers are used in JavaScript. JavaScript also offers how to define a number in octal or hexadecimal notation. You can also define floating point numbers in scientific notation.

Floating Point Numbers in Scientific Notation

Scientific notation or standard form of a number is used to represent very big or very small numbers. You can easily define numbers in scientific notations in JavaScript.

Example


var n1 = 1234e-5; //0.01234

var n2 = 13e15; //13000000000000000

var n3 = 5E-10; //5e-10

var n4 = 5E+10; //50000000000

The letter e can be small or capital followed by negative or positive sign. If no sign is given, it will assume positive sign.

Octal Numbers in JavaScript

For defining octal numbers in JavaScript, precede the number with a '0' (zero).

Example


var h1 = 010; //8

var h2 = 011; //9

var h3 = 01234; //668

var h4 = -010; //-8

Positive octal numbers are preceded by '+' (plus) sign or nothing. Negative octal numbers are preceded by '-' (minus) sign.

Hexadecimal Numbers in JavaScript

For defining hexadecimal numbers in JavaScript, precede the number with a '0x' or '0X'.

Example


var o1 = 0x10; //16

var o2 = 0X11; //17

var o3 = 0xFF; //255

var o4 = -0xff; //-255

Positive hexadecimal numbers are preceded by '+' (plus) sign or nothing. Negative hexadecimal numbers are preceded by '-' (minus) sign.

Converting Octal/Hexadecimal String to Decimal Number

parseInt() function can be used to convert octal/hexadecimal string into its decimal format.

parseInt(string, base);

Base tells the function that in what format the string is in. For octal string, base value should be 8 and for hexadecimal string, base value should be 16.

Example

<script>

var x = parseInt('010', 8); //8
document.write(x);
document.write("<br />");

var y = parseInt('0xFF', 16); //255
document.write(y);
document.write("<br />");

var z = parseInt('0xccff', 16); //52479
document.write(z);

</script>

Mathematical Constants in JavaScript

Math object in JavaScript provides various mathematical constants and mathematical functions. Below is the list of various constants.

Constant Description Value
Math.PI Constant PI 3.141592653589793
Math.E Euler's Constant 2.718281828459045
Math.LN2 Natural log of 2 0.6931471805599453
Math.LN10 Natural log of 10 2.302585092994046
Math.LOG2E Base 2 log of E 1.4426950408889634
Math.LOG10E Base 10 log of E 0.4342944819032518
Math.SQRT1_2 Square root of 0.5 0.7071067811865476
Math.SQRT2 Squre root of 2 1.4142135623730951

Example

<script>

var x = Math.PI;
document.write(x); //outputs 3.141592653589793

</script>

Commonly used Mathematical Methods in JavaScript

Some commonly used methods that are used mostly in JavaScript are as follows:

Method Description
Math.abs(number) returns an absolute value
Math.sqrt(number) returns square root of a number
Math.pow(a, b) returns power value a raised to the power b
Math.log(number) returns natural log value
Math.exp(number) returns Euler's contsant raised to power of specified number
Math.max(a, b) returns larger of two numbers
Math.min(a, b) returns smaller of two numbers

Example

<script>

var a = Math.abs(-7); //7
document.write(a);
document.write("<br />");

a = Math.sqrt(64); //8
document.write(a);
document.write("<br />");

a = Math.pow(2, 3); //2x2x2 = 8
document.write(a);
document.write("<br />");

a = Math.log(20); //2.995732273553991
document.write(a);
document.write("<br />");

a = Math.exp(5); //148.41315910257657
document.write(a);
document.write("<br />");

a = Math.max(5,10); //10
document.write(a);
document.write("<br />");

a = Math.min(5,10); //5
document.write(a);
document.write("<br />");

a = Math.min(-50,-49); //-50
document.write(a);

</script>

Trigonometric Methods in JavaScript

Trignometric Methods are rarely used but still sometimes helpful. Some of the trigonometric methods used are given below:

Method Description
Math.sin(number) returns sine value
Math.cos(number) returns cosine value
Math.tan(number) returns tangent value
Math.asin(number) returns arc sine value
Math.acos(number) returns arc cosine value
Math.atan(number) returns arc tangent value

Example

<script>

var a = Math.sin(0); //sin 0 degree = 0
document.write(a);
document.write("<br />");

a = Math.cos(0); //cos 0 degree = 1
document.write(a);
document.write("<br />");

a = Math.tan(0); //tan 0 degree = 0
document.write(a);

</script>

Rounding Numbers in JavaScript

You can round off numbers using the functions given in the table.

Method Description
Math.ceil(number) returns rounded up integer value
Math.floor(number) returns rounded down integer value
Math.round(number) returns nearest integer value

Example

<script>

var a = Math.ceil(12.4); //13
document.write(a);
document.write("<br />");

a = Math.floor(12.4); //12
document.write(a);
document.write("<br />");

a = Math.round(12.4); //12
document.write(a);
document.write("<br />");

a = Math.round(12.5); //13
document.write(a);

</script>

Rounding Number up to specific level of precision in JavaScript

While rounding a number using round() method, you will always end up getting some integer. Not all the time you want this. Sometimes you need to get a value up to some specific decimal level or precision. For example, Math.PI returns 3.141592653589793. What if you want to get this value up to 2 decimal places (3.14)? For doing so, you can multiply the floating point number with 100. Then use the round() method and then divide the number again by 100. Below is a formula that you can use.

Syntax

(Math.round(number * precision factor)) / precision factor

Precision factor represents the decimal level and is equal to 10 raise to the power of the decimal places. For example if you want 2 decimal places, precision factor will be 100(10^2), for 3 decimal places it should be 1000(10^3).

Example

<script>

var p = Math.round(Math.PI); //3
document.write(p);
document.write("<br />");

//for 2 decimal places
var p = Math.PI * 100;
p = Math.round(p);
p /= 100;
document.write(p);
document.write("<br />");

//for 4 decimal places
var p = (Math.round(Math.PI * 10000))/10000;
document.write(p);

</script>

Random Number in JavaScript

Math.random() method is used to generate a random floating point number between 0.0 and 1.0. You can always increase its range by multiplying it to a number. For example, multiplying it with 10 will increase its range from 0.0 to 10.0. And to get an integer range, use Math.ceil() so that the range actually becomes 1 to 10. You can use Math.floor() to make the range between 0 and 9.

Syntax

Math.random();

Example

<script>

var r = Math.random();
r *= 10;
r = Math.ceil(r); //returns any number between 1-10
document.write(r);
document.write("<br />");

r = Math.floor(Math.random()*10); //returns any number between 0-9
document.write(r);

</script>

Infinity in JavaScript

Infinity is a global property (variable) that is used to represent infinite values in JavaScript.

Example

<script>

var i = 100 / 0;
document.write(i); //Infinity

</script>

There are also two other global properties in JavaScript, POSITIVE_INFINITY and NEGATIVE_INFINITY but they are rarely used.

NaN in JavaScript

NaN is a global property which means "Not a Number".

Example

<script>

var n = "string" * 10; //NaN
document.write(n);

</script>
<< Arrays Working with Strings >>