0

When writing JavaScript I always took for granted that adding two integers together resulted in another integer. Or that adding two strings together would result in the concatenation. But I got to thinking, how does the language determine the types of instances behind the scenes prior to performing operations using those instances?

var one = 1;
var two = 2;
var fourStr = 'four';
var floaty = 1.5;

//this results in an integer
var three = one + two;           //3

//but this results in a string
var result = fourStr + one;      //'four1'

//and this results in a float
var floatenized = one + floaty;  //2.5

Does the runtime just determine the instance types and then reference some sort of internal type precedence or something? Can anyone explain exactly how these operations are performed by the runtime when instances of differing types are combined?

1
  • As ManseUK has pointed out when combining a string with a number the string takes precendence therefore the number is converted to a string first then the two are concatenated. Commented Apr 13, 2012 at 13:10

2 Answers 2

3

The ecmascript specification describes exactly how operators like + (section 11.6.1) act with different types.

Relevant for your example:

  • (number) + (number): result will be the sum of the floats
  • (string) + (number): result will be the concatenation of the string and the string represantation of the number
  • (number) + (string): result will be the concatenation of the string represantation of the number and the string

But the + can also act as a unary operator to transform a string into a number, as like as the binary - operator does (e.g.: +"4"+5==9, 5-"4"==1).

The type conversion algorithms are described in section 9 of the spec. How an environment should store the types of primitives and objects is not specified.

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

Comments

2

When you assign a number to a variable

var one = 1;
//or
var floaty = 1.5;

the value type is a number .... When you assign a string to a variable

var fourStr = 'four';

the value type is string. When you add a string and a number it converts the number to a string then concatenates the 2 strings

See the values section of this doc from Mozilla

6 Comments

Noting that variables don't have a type, their value does. ;-)
This isn't really telling me anything I don't already know. I know that the conversions are taking place, my question was more how and by what means is the conversion taking place in JavaScript.
@KodeKreachor the + symbol is shorthand for concatenation when used with a string - the toString() method is used to convert the number to a string ....
@ManseUK thanks for your answers, maybe I'm not stating myself clearly. How does the runtime know that a number needs to be converted into a string when it is concatenated w/ another string? Does it make a list of all the types in the expression and then, if any strings exist, converts them all into strings? How is this happening behind the scenes?
@KodeKreachor I assume it checks each of the types and converts all to strings if a string is found - you would have to look at the source (JavaScript) to verify
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.