Your Type Or Not
var im_a_number = 42; var im_another_number = 42.357;
What is it Really?
Big and Small
var im_a_big_number = 42e17; var im_a_small_number = 42e-6; document.write("Big number: " + im_a_big_number + "<br>"); document.write("Small number: " + im_a_small_number + "<br>");
The output will look like this:
Big number: 4200000000000000000 Small number: 0.000042
Getting Precise, and More
First, let’s declare some constants, then display them as-is:
var im_a_big_number = 42024363454736353; var im_a_small_number = .00000321; var im_a_too_precise_number = 1.023616785; var fix_me_at_two = 25.1; document.write("Big number exp: " + im_a_big_number + "<br>"); document.write("Small number exp: " + im_a_small_number + "<br>"); document.write("Four-digit precision: " + im_a_too_precise_number + "<br>"); document.write("Two-decimal fixed: " + fix_me_at_two + "<br>"); With this unformatted, unconverted output:
Big number exp: 42024363454736350 Small number exp: 0.00000321 Four-digit precision: 1.023616785 Two-decimal fixed: 25.1
Now let’s set the exponents and the precision:
var im_a_big_number = 42024363454736353; var im_a_small_number = .00000321; var im_a_too_precise_number = 1.023616785; var fix_me_at_two = 25.1; document.write("Big number exp: " + im_a_big_number.toExponential(1) + "<br>"); document.write("Small number exp: " + im_a_small_number.toExponential(2) + "<br>"); document.write("Four-digit precision: " + im_a_too_precise_number.toPrecision(4) + "<br>"); document.write("Two-decimal fixed: " + fix_me_at_two.toFixed(2) + "<br>");
And now the output looks like this:
Big number exp: 4.2e+16 Small number exp: 3.21e-6 Four-digit precision: 1.024 Two-decimal fixed: 25.10
Entering Other Formats
Once again, you just do it in-line. To turn a constant into an octal number, give it a leading zero, and to create a hexadecimal constant, use the standard 0x (zero + x):
var doc_octal = 0405; var what_the_hex = 0x3e71; document.write("Number of octal octopi: " + doc_octal + "<br>"); document.write("A hex of a lot: " + what_the_hex + "<br>");
Here’s the output:
Number of octal octopi: 261 A hex of a lot: 15985
Making It Look Right
You probably noticed something else as well: Even though the constants are octal and hexadecimal, the output is still decimal. What if you want to display the output in octal or hex?
There’s a method for that — specifically, the toString() method. Here’s how it’s used:
var doc_octal = 0405; var what_the_hex = 0x3e71; document.write("Number of octal octopi: " + doc_octal.toString(8) + "<br>"); document.write("A hex of a lot: " + what_the_hex.toString(16) + "<br>"); document.write("Plenty of ones and zeroes: " + what_the_hex.toString(2) + "<br>");
Number of octal octopi: 405 A hex of a lot: 3e71 Plenty of ones and zeroes: 11111001110001
As shown in the example above, the toString() method takes 2, 8, and 16 as arguments, converting the output to binary, octal, and hexadecimal respectively.
Not a Number?!?
var apples_and_oranges = "three apples" / "five oranges"; document.write("Three apples divided by five oranges is : " + apples_and_oranges + "<br>");
Three apples divided by five oranges is : NaN
You can also use the isNaN() function to see whether or not something really is a number:
var three_apples = "three apples"; var five_oranges = 5; document.write("Three apples is Not a Number : " + isNaN(three_apples) + "<br>"); document.write("Five oranges is Not a Number : " + isNaN(five_oranges) + "<br>");
Note that the isNaN() function is checking to see if something is not a number, so “true” means “not a number,” and “false” means “is a number.”
Three apples is Not a Number : true Five oranges is Not a Number : false
Divide by Zero Non-Error
Here’s a fun fact: You can do something like this:
var OMG_Disaster = 247/0; document.write("OMG! Disaster! It's " + OMG_Disaster + "!!! <br>");
and the universe will not get sucked into a black hole! What will happen?
OMG! Disaster! It's Infinity!!!
Is it genuine Infinity, or just an overflow error message in disguise?
var OMG_Disaster = 247/0; var Infinity_minus_five = OMG_Disaster - 5; document.write("Infinity minus five is " + Infinity_minus_five + ".<br>");
Look at the output:
Infinity minus five is Infinity.
That looks like genuine Infinity. And in fact, the isNaN() function will tell you that it is a number:
var OMG_Disaster = 247/0; var Tell_me = isNaN(OMG_Disaster); document.write("I say that OMG_Disaster is NOT a number! Is that true? <br>" + Tell_me + ".<br>");
And here’s the output:
I say that OMG_Disaster is NOT a number! Is that true? false.
Empower your team. Lead the industry.
Get a subscription to a library of online courses and digital learning tools for your organization with Udemy Business.