Shuai Li
Shuai Li

Reputation: 2766

Why does isNAN("12") evaluate to false?

Why does JavaScript interpret 12 and "12" as equal?

function sanitise(x) {
  if (isNaN(x)) {
    return NaN;
  }
  return x;
}

var a = "12"
var b = 12
console.log(typeof(a))
console.log(sanitise(a));
console.log(sanitise(b));

Output:

> "string"
> "12"
> 12

And then, what is the difference between "12" and "string"?

Upvotes: 6

Views: 1234

Answers (6)

gurvinder372
gurvinder372

Reputation: 68393

why “12” is a not NaN in JavaSciprt?

As per spec, there is an implicit conversion toNumber before checking if the given input is a number or not (or Not a Number - NaN )

  1. Let num be ToNumber(number).

So isNaN("12") => isNaN(12) => false

Upvotes: 2

Rohit Agrawal
Rohit Agrawal

Reputation: 1521

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

As per documentation, NaN values are generated when arithmetic operations result in undefined or unrepresentable values. Such values do not necessarily represent overflow conditions. A NaN also results from attempted coercion to numeric values of non-numeric values for which no primitive numeric value is available.

For example, dividing zero by zero results in a NaN — but dividing other numbers by zero does not.

Here "12" is not a number but it is not NaN either. Therefore isNaN() returns false.

Also, 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.

Therefore isNaN('s') returns true as 's' is converted to a number. Parsing this as a number fails and returns NaN.

Upvotes: 7

axiac
axiac

Reputation: 72226

"12" and 12 are not the same thing but in some contexts JS silently converts one to another.

For example, "12" + 12 is "1212" (converts the number to string) but "12" * 12 is 144 (converts the string to number).

The first conversion happens because + is used for both numbers addition and strings concatenation. If one of its operands is a string then it makes sure the other is also a string (converts it to string if it is not already a string). In order to work as the addition operator, both its operands must be numbers.

On the other hand, the * is used only as the multiplication operator and that's why it converts its operands to numbers in order to be able to work with them.

The isNaN() function checks if its argument has the special floating-point value NaN that cannot be compared against other numbers using the usual comparison operators (== and ===). It doesn't check the type of its argument; its purpose is not to tell apart numbers from other types. It is used to detect division by zero and values that cannot be represented by the floating-point number format (overflowing results on addition of large numbers, underflowing results on division of small values by large values etc.)

Upvotes: 0

Quentin
Quentin

Reputation: 943645

Was JavaScript takes "12 and 12 as same things?

It doesn't.

Under some conditions, it will implicitly convert 12 to "12" or vice versa (other examples include if you are doing a comparison with == or concatenating a string with 12 ("" + 12).

And then, what is the difference between "12" and "string"?

One is a string representing the number twelve as digits, the other is a string representing the word string.

The typeof operator tells you (in the form of a string) what type of data a value is.

Upvotes: 1

Vitaliy Terziev
Vitaliy Terziev

Reputation: 6691

Check the info below.

This function is different from the Number specific Number.isNaN() method.

The global isNaN() function, converts the tested value to a Number, then tests it.

Link

Upvotes: 1

Faly
Faly

Reputation: 13356

It's because behind the scene, isNaN converts the argument passed to it to number before checking if it's a number

Upvotes: 1

Related Questions