JavaScript Numbers
To handle numerical values, JavaScript has Number type. Number type can be used to represent decimal or non-decimal values. Unlike other programming languages, JavaScript does not use different number types like int, short, byte, float, double, etc.
<script>
num1=13;
num2=56.37;
num3=-13.6324;
document.write(num1+'<br>');
document.write(num2+'<br>');
document.write(num3+'<br>');
</script>
Output:13
56.37
-13.6324
We can also represent extra-large numbers,
<script>
var num1
= 67e9;
var num2
= 47e-5;
document.write(num1+'<br>');
document.write(num2+'<br>');
</script>
Output:67000000000
0.00047
Representation
All of the JavaScript number representations are 64-bit floating-point.JavaScript numbers are 64-bit double-precision floating-point numbers(IEEE 754 standard).In this format starting, 52-bits are used for value, the next 11-bits are used for exponents and the remaining one bit is used for the sign.
Accuracy
The integer numbers are accurate up to 15 digits. An integer is a number without an exponent or a period. We can put up to 17 decimals, but the floating-point calculation is not exact or extremely accurate.
<script>
var a =
0.20000000000000001; //17
decimal points
var b =
0.300000;
document.write((a+b)+'<br>');//not accurate
var a =
0.2000000000000001; //16
decimal points
var b =
0.300000;
document.write((a+b)+'<br>'); //accurate
</script>
Output:0.5
0.5000000000000001
Operations
We can perform all arithmetic, bitwise, assignment and logical operation on JavaScript numbers Using JavaScript operators.
<script>
var a =
56;
var b =
32;
document.write((a+b)+'<br>'); //ADD
document.write((a-b)+'<br>'); //SUB
document.write((a*b)+'<br>'); //MUL
document.write((a/b)+'<br>'); //DIV
document.write((a%b)+'<br>'); //MOD
document.write((a**2)+'<br>'); //EXPO
document.write((a<<2)+'<br>'); //ZERO FILL LEFT SHIFT
document.write((a>>2)+'<br>'); //SIGNED RIGHT SHIFT
document.write((a>>>2)+'<br>'); //ZERO
FILL RIGHT SHIFT
document.write((a&b)+'<br>'); //AND
document.write((a|b)+'<br>'); //OR
document.write((~b)+'<br>'); //NOT
document.write((a^b)+'<br>'); //XOR
</script>
88
24
1792
1.75
24
3136
224
14
14
32
56
-3324
The "+" operator
The "+" operator is used for both adding numbers and concatenating strings in JavaScript.If we "+" two numbers the output will be a number and if we "+" two strings the output will be String, but if we "+" a number and a string, the outcome will be a string.
<script>
var a =
56;
var b =
32;
var c='JavaScript';
var d='Programming';
document.write((a+b)+'<br>'); //number
document.write((c+d)+'<br>'); //string
document.write((a+c)+'<br>'); //string
</script>
88
JavaScriptProgramming
56JavaScript