Start networking and exchanging professional insights

Register now or log in to join your professional community.

Follow

In Javascript If you add a number and a string, what will be datatype of result?

user-image
Question added by Muhammad Hammad Javaid , IT Project Manager , Dubai Islamic Bank
Date Posted: 2013/04/06
Hazem Salama
by Hazem Salama , Senior Member of Technical Staff , Verizon Communications

Javascript + operator will concatenate the two variables and will treat the number as a string to complete the operation. Hence, the result will be a string. Try this in your console var a = 1; var b = 'x'; var c = a+b; console.log(typeof(a)); // will return number as the type console.log(typeof(c)); // will return string as the type of the result

Jonathan de Flaugergues
by Jonathan de Flaugergues , software engineer , Abbeal

According to EcmaScript language specification, the addition operator either performs string concatenation or numeric addition.

The production result = leftPrimitive + rightPrimitive is evaluated as follows :

I type of leftPrimitive is string or type of right primitive is string, result is the result of concatening of leftPrimitive followed by rightPrimitive.

 

Therefore, the answer is : The datatype will be "string".

 

Do you need help in adding the right keywords to your CV? Let our CV writing experts help you.