GP_JS_Number_and_Boolean
GP_JS_Number_and_Boolean
Number
● JavaScript Numbers are Always 64-bit Floating Point.
Unlike many other programming languages, JavaScript does not define different
types of numbers, like integers, short, long, floating-point etc.
● Precision
Example :
Case 1 : let x = 999999999999999; // x will be 999999999999999
when <=15 digits
Case 2 : let y = 9999999999999999; // y will be 10000000000000000
when >=15 digits
x = “5” , y = “2” ;
z = x*y ; // z = 10
x = “5” , y = “2” ;
z = x - y ; // z = 3
1
But “+” concatenates the Strings.
Methods
1. tostring( ) : Converts from Number to String
2. toFixed( ) : returns a string, with the number written with a specified number of
decimals
Variables to Numbers
● Number( ) : Converts from a variable to number
● parseInt( ) : Parses its argument and returns an integer
● parseFloat( ) : Parses its argument and returns a floating point number
2
Number( )
parseInt()
➔ parses a string and returns a whole number, does not contains decimals.
➔ Spaces are allowed.
➔ Only the first number is returned.
parseFloat()
➔ parses a string and returns a number, decimals are also included
➔ Spaces are allowed.
➔ Only the first number is returned.
3
Properties Of Number
Property Description
4
Boolean
Boolean holds two types of values: true or false
Boolean( ) Function :
The boolean( ) function is used to find out if an expression (or a variable) is true or not.
Boolean(0); // FALSE
Boolean(1); // TRUE
Boolean(-2); // TRUE
Boolean(“”); // FALSE
Boolean(“Coding Ninjas”); // TRUE
Boolean(“ ”); // TRUE because space(_)
let temp ;
Boolean(temp); // FALSE
5
NaN: For NaN, it is false by default