Communiquez avec les autres et partagez vos connaissances professionnelles

Inscrivez-vous ou connectez-vous pour rejoindre votre communauté professionnelle.

Suivre

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

user-image
Question ajoutée par Muhammad Hammad Javaid , IT Project Manager , Dubai Islamic Bank
Date de publication: 2013/04/06
Hazem Salama
par 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
par 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".

 

Avez-vous besoin d'aide pour créer un CV ayant les mots-clés recherchés par les employeurs?