0

Why JS returns NaN in: + 'any_string'?

Please check the screenshot:

image

shrys
  • 5,635
  • 2
  • 20
  • 32
0seNse0
  • 43
  • 5

2 Answers2

3

Using the unary plus operator you can convert something into a number. By doing +'some_string' you convert the string to a number, but since some_string is not a valid number you get NaN Not a Number.

Rain336
  • 1,312
  • 11
  • 17
2

The "+" tries to convert the string to an integer. See docs here

As it has failed to convert it to a number, the output is NaN : Not A Number

console.log(+"3");
console.log(+"randomString");
George
  • 6,471
  • 2
  • 27
  • 36
Maxime Girou
  • 1,477
  • 2
  • 15
  • 29