Number methods
Let's move on to some built-in methods on the Number
object. We have seen a few already, these are so popular that some of them have been made into global methods.
Checking if something is (not) a number
This can be done with isNaN()
. We have seen this already when we talked about global methods, we can use this method without Number
in front of it. Often you will want to do the opposite, you can negate the function with an !
in front of it:
let x = 34;
console.log(isNaN(x));
console.log(!isNaN(x));
let str = "hi";
console.log(isNaN(str));
This will log to the console:
false
true
true
Since x
is a number, isNaN
will be false
. But this result negated becomes true
, since x
is a number. The string hi
is not a number, so it will become false
. And this one?
let str1 = "5";
console.log(isNaN(str1));
Some funky stuff is going on here, even though 5
is between quotation marks, JavaScript still sees that it's a...