In this post, I’ll be discussing the converting and checking numbers method in JavaScript programming, using the Feynman Method and the 80’s movie classic Gremlins.
This is part of the Feynman Technique/20 hr Method challenge. For more information, click here.
Numeric vs Strings Review
One of the things Java and JavaScript have in common is how they see numeric values versus string data types. A quick reminder is that 3, “3”, and “three” are not all equal. Strings are letters/numbers that live within quotation marks that are seen as objects. Numbers without the quotation marks are just numbers, so 3 is a number, “3” is a string and “three” is most definitely a string. For the full explanation, you can take a look at my post on Java data types here.
Javascript base numbered on one of two groups:
- Base 10 system, which just means it uses individual numbers from 0 to 9 to make our complete number system. This is our regular decimal math system.
- Binary base 2 system only consists of 0 and 1. For example 0001=1, 0010=2, 0011=3, 0100=4, 0101=5, etc.
There is a Blip in the System
One of the things about Javascript equations is it’s not ideal to run scientific code on a JS machine. For example, if you run the following:
console.lconsole.log(0.1 + 0.2);
…this would be your output.
0.30000000000000004
Although it’s kind of arbitrary, fact of the matter is, we know it’s wrong, but that’s what the system comes back with.
In other words, do not use javascript to compute the code to land a space shuttle, even if it’s from SpaceX.
What we CAN do, on the other hand is ask the machine to convert numeric strings into numbers, if those strings follow certain rules, like when it comes to taking care of gremlins.
Converting Numbers
For converting string numbers into actual numbers, we have the following options:
- The Number Conversion method ver 1
console.log(Number("101"));
- The Number Conversion method ver 2
console.log(+"101");
- ParseInt (base 10, which is the #10 you see below, can be added to define the number, but it won’t work with binary 2). If the number to be converted from a string starts out with a letter like “G14”, the return on this would be NaN (Not a Number). It also only returns whole number, since the Int in ParseInt = integer.
console.log(Number.parseInt("60px", 10));
- ParseFloat (v1) – these can return decimals, and can be used with base 10 or binary 2. Because of it’s flexibility, it’s a preferred method to convert numbers from strings.
console.log(Number.parseFloat("5.5rem"));
- ParseFloat (v2) – this is the old school method, and not preferred, since adding Number to the log clarifies the usage.
console.log(parseFloat(`6.5px`));
It’s a lot less freaky than watching a bunch of furry gremlins turning into those creepy monsters…
Checking Numbers
If an input is a number, we have three methods and all three will return boolean (true/false) statements:
- NaN (Not a Number) – is the following entry Not a Number?
console.log(Number.isNaN(+`15y`));
console.log(Number.isNaN(`15`));
console.log(Number.isNaN(15));
console.log(Number.isNaN(15 / 0));
The return for these above are as follows:
true - it's seen as a string (+ does not convert into #)
true - also seen as a string
false - this is a number
false - NaN sees infinity as a number.
- IsFinite – the returns for these are almost the same as NaN, however the question that is asked is, if the following entry a finite number?
console.log(Number.isFinite(15));
console.log(Number.isFinite(`15`));
console.log(Number.isFinite(+`15X`));
console.log(Number.isFinite(15 / 0));
…the responses are as follows:
true - it's actually a number with an end.
false - this is a string.
false - this is also a string.
false - infinity has no end.
- isInteger – Since integers are whole numbers, the question this is asking is if the entry is a whole number.
console.log(Number.isInteger(15));
console.log(Number.isInteger(15.5));
console.log(Number.isInteger(15 / 0));
console.log(Number.isInteger(`15`));
console.log(Number.isInteger(+`15X`));
The outputs are pretty straight forward…
true - it's a whole number
false - not a whole number, even if it is a number.
false - infinity is not seen as a whole number
false - string is not a number
false - string is not a number
Ideally, isFinite is the preferred method to use when figuring out if the information you have is a number or a string, this is unless you are only looking for whole numbers, but whatever you’re looking for, make sure you choose the right filter option, otherwise, you may end up with a situation like this.
…and don’t trust js math when dealing with launch and landing.
For more information on Converting and Checking Numbers in Javascript, click on the links below:
- W3Schools – JavaScript Type Conversions
- MDN – Number JavaScript
- Go Make Things – Converting Strings to Number With Vanilla JavaScript
Happy coding!