Numbers can contain various values. To create a number, a variable with var keyword has to be created and value has to be stored in it.
- Decimal value: A number with a normal value can be as follows.
Var b=25;
- Octal number: JavaScript can convert the octal value to decimal value. A number with octal value(a number with 8 digits from 0-7) can be as follows.
var b=012; //contains the decimal value 10
- Hexadecimal number: Hexadecimal value can be created using 0x and can be as follows.
var myHex1=0xF; //contains decimal value 15
- Floating point number: These are defined by placing the numbers after the decimal point.
var d=2.6;
var e=.6; // valid but not recommended
var f=3.; // considered as 3 only
var g=5.0; //considered as 5 only
var f=6e5; // the output will be 60000. Large/small numbers can use e to represent.
var h=2e-4; // the output will be 0.0002
- IsFinite(): IsFinite() function gives the result in the form of boolean. If the number is finite, then the result will be true, if not, the result will be false.
var num1=2*Number.MAX_VALUE; //infinite number
var test=isFinite(num1); //false
- NaN (Not a Number): In this case, the result will not be a number.
var num2="Orange"/5;
Below example shows numbers and various types in it.
[c]
<!DOCTYPE html>
<html>
<head>
<title> variables test</title>;
<h2> Welcome to Splessons</h2>;
</head>
<body>
<script type="text/javascript">
//decimal value
var a=5;
document.write("Your number is ", a);
//Octal value
var b=012;
document.write("Octal number is", b);
//Hexadecimal value
var c=0xF;
document.write("Hexadecimal number is", c);
//Floating point values
var d=2.6;
document.write("Float point is",d);
var e=.6;
document.write("Float point is",e);
var f=3.;
document.write("Float point is",f);
var g=5.0;
document.write("Float point is",g);
var h=6e5;
document.write("Float point is",h);
var i=2e-4;
document.write("Float point is",i);
//calculating sum of two floating points
var sum=d+g;
document.write("The sum is ",sum);
//isInfinite() function and the range
var num1=2*Number.MAX_VALUE;
var res=isFinite(num1);
document.write("The number is infinite and hence result is ",res);
//NaN
var num2="Orange"/5;
document.write("", num2);
</script>
</body>
</html>[/c]
Output: