2

Here's an easy one for you true believers: You can use + to convert a string to a number,

var thing = "12"
alert(thing);
alert(typeof thing); // string
thing = +thing;
alert(typeof thing); // number
if (thing == 112) alert("!"); // number

Can someone explain:

  1. What is the name of this process?
  2. How does + convert a string to a number?
1

1 Answer 1

3

Javascript uses a dynamic type system. For me it's a 'cast' operation.

The operator + could be a String operator ('a' + 'b') or an Number operator (1+2). It could be used also between Strings and numbers (remembering that 0 + '12' = 12 and '0'+'12' = '012')

By default, i think that the JS interpreter considered +thing as 0 + things so it casts this variable to a number

Sign up to request clarification or add additional context in comments.

1 Comment

According to developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… the target type of casting is due to the associativity of binary + being left to right ... it all starts with the type of value to the left of + requiring the other side to match type, thus requiring some type casting.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.