In client-side development, numbers are used to define different kinds of shape and dimension of page interactivity. And obviously, in the back end numbers perform different kinds of calculative operation.
Variable declaration: Numbers
var myNum1 = 10, myNum2 = 7; document.write( "Sum is: ", myNum1 + myNum2 ); /* * var is the keyword for declaring variables * myNum is the variable name that you will define your own * '=' is the assignment operator and assigns a number value to it * 10, 7 are values (Number values) */
Sum is: 10
Note: Don’t put number values inside the single or double quotes. Otherwise, it will act as simple strings
Decimal Number Types
- Integer: Integer numbers are most common decimal number without any fraction. ( e:g:
var myInt = 5) Here 5 is an integer number.
- Floating Point: Floating point numbers are a basically pure fractional number. ( e:g:
var myFloat = 5.67)
Depending on base
- Decimal Number: by default any defining any number in a variable as decimal values.
0bas the initial of the binary number.
- Octal Number: In the same way, use
0oas the initial of octal number.
- Hexadecimal Number: You can store hexadecimal number by using
0xas the initial of hexadecimal numbers.
var a = 34; //decimal intege number var e = 43.45; // floating point decimal number var b = 0b110111010; // binary number var c = 0o5715251; // octal number var d = 0x162FA; // hexadecimal number
Don’t mess up
Note: Calculating any string with integer always executes as a string.
var x = 10, y = "30"; var z = x + y; documnet.write( z );
When the execution reaches to the third string then it will be converted to a string. Let’s see an example:
var x = 5, y = 6; z = "hello'; var total = x + y + z; console.log( total ); // expected output 11hello
console.log(Infinity + 1); console.log(Math.pow(10, 1000)); console.log(Math.log(0));