Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is "string" - "string" NaN? [duplicate]

This is something like an infinite loop for me.

var sM = "Hello" - "World";
console.log(sM) && console.log(typeof sM);

I understand why string - string outputs NaN, but then "Hello" - "World" typeof is a number.

It means subtracting string with another string gives you a type of a number.

Where is the logic in that?

like image 666
super11 Avatar asked Dec 18 '22 16:12

super11


1 Answers

why string - string outputs NaN

Because subtraction only deals in numbers, so it converts both sides to numbers and gets Not A Number.

but then "Hello" - "World" typeof is a number.

NaN has the type Number. This is standard in computing.

like image 149
Quentin Avatar answered Dec 28 '22 07:12

Quentin