0

Why JS returns NaN in: + 'any_string'?

Please check the screenshot:

image

shrys
  • 5,381
  • 2
  • 15
  • 29
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,227
  • 10
  • 15
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,116
  • 2
  • 24
  • 36
Maxime Girou
  • 1,415
  • 2
  • 11
  • 26