Why Does Isnan(" ") (String with Spaces) Equal False

Why does isNaN( ) (string with spaces) equal false?

JavaScript interprets an empty string as a 0, which then fails the isNAN test. You can use parseInt on the string first which won't convert the empty string to 0. The result should then fail isNAN.

Why is isNaN(1) false?

isNaN implicitly coerces the argument to Number, and then checks whether this coerced value is NaN.

See http://es5.github.io/#x15.1.2.4

That is, isNaN(foo) is equivalent to isNaN(Number(foo))

Code fix:

if (typeof arguments[i] !== 'number' || isNaN(arguments[i])) return false;

The second part of the condition is because typeof NaN === 'number'.


Your function might be a bit more readable in functional style, using ES5's Array#every method:

//returns whether all arguments are of type Number and not NaN
function numbers() {
return [].every.call(arguments, function(arg) {
return typeof arg === 'number' && !isNaN(arg);
});
}

Why does Number.isNaN() return false for Strings?

There is a distinction to be made between Number.isNaN and isNaN

https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/isNaN

The isNaN() function determines whether a value is NaN or not.

https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Number/isNaN

The Number.isNaN() method determines whether the passed value is NaN and its type is Number. It is a more robust version of the original, global isNaN().

The reason you are returning false is that "Stack Overflow" is not a number it is a string.

console.log('Number.isNaN("Stack Overflow"): '+Number.isNaN("Stack Overflow"));console.log('isNaN("Stack Overflow"): '+isNaN("Stack Overflow"));

isNaN('Hello World!') vs isNaN('')

According to the documentation:

Since the very earliest versions of the isNaN function specification, its behavior for non-numeric arguments has been confusing. When the argument to the isNaN function is not of type Number, the value is first coerced to a Number. The resulting value is then tested to determine whether it is NaN. Thus for non-numbers that when coerced to numeric type result in a valid non-NaN numeric value (notably the empty string and boolean primitives, which when coerced give numeric values zero or one), the "false" returned value may be unexpected; the empty string, for example, is surely "not a number." The confusion stems from the fact that the term, "not a number", has a specific meaning for numbers represented as IEEE-754 floating-point values. The function should be interpreted as answering the question, "is this value, when coerced to a numeric value, an IEEE-754 'Not A Number' value?"

You may want to use Number.isNaN instead of isNan, it behaves differently (this is also mentioned in the link I provided)

console.log string is empty if isNaN is not a number or a string

Add if (isNan.value.trim() == "") { to very first condition. Also use isNan.value.trim() == "" so it will also handle text with white space only.

Please refer Why does isNaN(" ") (string with spaces) equal false?

function isNotANumber() {
let isNan = document.querySelector("#nan");

// Use below condition as first condition.
// Also use .trim() so it will also handle text with white space only
if (isNan.value.trim() == "") {
console.log(isNan.value + " is not a number");
} else if (isNaN(isNan.value) == false) {
console.log(isNan.value + " is a number");
} else if (isNaN(isNan.value) == true) {
console.log("nothing to submit");
}
}
body {
padding: 0;
margin: 0;
background: #518688;
}

b {
color: darkturquoise;
}

input,
button {
padding: 5px;
boder: none;
outline: none;
}
<main id="NaN">
<input id="nan" type="text"><button onclick="isNotANumber()">submit</button>
</main>

Why is isNaN(null) == false in JS?

I believe the code is trying to ask, "is x numeric?" with the specific case here of x = null. The function isNaN() can be used to answer this question, but semantically it's referring specifically to the value NaN. From Wikipedia for NaN:

NaN (Not a Number) is a value of the numeric data type representing an undefined or unrepresentable value, especially in floating-point calculations.

In most cases we think the answer to "is null numeric?" should be no. However, isNaN(null) == false is semantically correct, because null is not NaN.

Here's the algorithmic explanation:

The function isNaN(x) attempts to convert the passed parameter to a number1 (equivalent to Number(x)) and then tests if the value is NaN. If the parameter can't be converted to a number, Number(x) will return NaN2. Therefore, if the conversion of parameter x to a number results in NaN, it returns true; otherwise, it returns false.

So in the specific case x = null, null is converted to the number 0, (try evaluating Number(null) and see that it returns 0,) and isNaN(0) returns false. A string that is only digits can be converted to a number and isNaN also returns false. A string (e.g. 'abcd') that cannot be converted to a number will cause isNaN('abcd') to return true, specifically because Number('abcd') returns NaN.

In addition to these apparent edge cases are the standard numerical reasons for returning NaN like 0/0.

As for the seemingly inconsistent tests for equality shown in the question, the behavior of NaN is specified such that any comparison x == NaN is false, regardless of the other operand, including NaN itself1.

Is Number.IsNaN() more broken than isNaN()

isNaN() and Number.isNaN() both test if a value is (or, in the case of isNaN(), can be converted to a number-type value that represents) the NaN value. In other words, "NaN" does not simply mean "this value is not a number", it specifically means "this value is a numeric Not-a-Number value according to IEEE-754".

The reason all your tests above return false is because all of the given values can be converted to a numeric value that is not NaN:

Number('')    // 0
Number(' ') // 0
Number(true) // 1
Number(false) // 0
Number([0]) // 0

The reason isNaN() is "broken" is because, ostensibly, type conversions aren't supposed to happen when testing values. That is the issue Number.isNaN() is designed to address. In particular, Number.isNaN() will only attempt to compare a value to NaN if the value is a number-type value. Any other type will return false, even if they are literally "not a number", because the type of the value NaN is number. See the respective MDN docs for isNaN() and Number.isNaN().

If you simply want to determine whether or not a value is of the number type, even if that value is NaN, use typeof instead:

typeof 'RAWRRR' === 'number' // false

Why is the result of Number.isNaN('abc') false?

NaN is a special number value. Number.isNaN verifies whether the passed in value is equal to NaN, because "normal" comparison doesn't work (NaN == NaN is false by definition).

Unlike some other methods, Number.isNaN does not perform type conversion. 'abc' is a string value therefore it cannot be NaN.

The specification defines Number.isNaN as:

  1. If Type(number) is not Number, return false.
  2. If number is NaN, return true.
  3. Otherwise, return false.

Note that there's also the global isNaN function, which behaves differently: If you give it a value that isn't of the number type, it converts it to the number type before doing its check. So isNaN('abc') is true because if you convert 'abc' to number using the default string-to-number conversion, you get NaN; but Number.isNaN('abc') is false because it doesn't do that conversion.

TypeScript isNaN only accepts a number

I advise you to implement your code differently.

The reasons:

  1. It might be short, but it's not easy to understand what's going on
  2. Using isNaN isn't the best option here: isNaN("") returns false as well

You better try to convert the value into a number and check if that's NaN or not (as @smnbbrv wrote):

if (typeof expectedValue === "string" && !Number.isNaN(Number(expectedValue))) {
expectedValue = Number(expectedValue);
}

Edit

You can pass your value as any:

isNaN(ctualValue as any)

To bypass the compiler check.



Related Topics



Leave a reply



Submit