Numbers

All numbers in JavaScript are 64bit floating point numbers.

 

Addition Vs Concatenation:

 

Addition
var a = 7 ;
var b = 8 ;
document.write(a + b) ; //treats as numbers, therefore = 15

 

Concatenation
var c = "6";
var d = "9";
document.write(c + d) ; //treats as strings, therefore = 69

 

If any part of statement contains a string, the whole statement will be treated like a string.

var e = 4;
var f = "13";
document.write(e + f) ; //treats as strings, therefore = 413

 

NaN = Not a Number
If non computable values are used, JavaScript returns NaN.

var g = "Proteins";
var h = 3;
document.write(g * h) ; //returns Nan

 

To overcome this the builtin Number(); //converts to a number if possible

var myNum = Number(g) ;

if (isNaN(myNum)){

document.write("var i is not a number.") ;

}

 

Math object

var  x = 200.6

var y = Math.round(x); //201

 

Math.max

Math.min

Math.PI

Math.rando()m

.sqrt()

.log()

 

Leave a Reply