Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and the Yahoo Answers website is now in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

Javascript concatination of strings?

I know that I can add 2 numbers

i.e.

var x = 1;

var y = 1;

var total = parseInt(x) + parseInt(y);

I know that I dont really need the parseInt(), but it tells javascript to treat the variable as an Integer...

Now if I have 2 numbers, but i DONT want Javascript to add them, i just want to concatenate them together....how do i do that?

i.e.

var x = 1;

var y = 2;

i want to be able to do x+y, and get "12"

Update:

SWEET - THANKS !

3 Answers

Relevance
  • 1 decade ago
    Favorite Answer

    You can force it to treat the operation as a string concatenation by starting the expression with a blank string, the "mode" of the operation will only change to string AFTER a string is encountered in the expression, but not before:

    var x = 1;

    var y = 2;

    var concatString = "" + x + y;

    // concatString == "12"

    var concatString2 = x + y + "..." + x + y;

    // concatString2 == "3...12"

    Similarly, if you were concatenating strings but wanted to do a math operation within the string, it would need to be offset in parentheses:

    var x = 1;

    var y = 2;

    var concatString = "The total weight is: " + x + y + "kg";

    // concatString == "The total weight is: 12kg"

    var sumString = "The total weight is: " + (x + y) + "kg";

    // sumString == "The total weight is: 3kg"

    A quick way to get something you've created as a string back to a number is to multiply it by 1 (a quick alternative to parseInt() if you know the string is only numbers).

    var x = 1;

    var y = 2;

    var concatString = "" + x + y;

    var badSum = concatString + 10;

    // badSum == "1210"

    var goodSum = concatString * 1 + 10;

    // goodSum == 22

    However, if your string could contain letters as well as numbers, this implicit "" + or the explicit String(x) cast will give you NaN (not a number). parseInt takes care of that:

    var x = "1 apple";

    var y = "2 apples";

    var concatSum1 = x + y + " apples";

    // concatSum1 == "1 apple2 apples apples"

    var concatSum2 = Number(x) + Number(y) + " apples";

    // concatSum2 == "NaN apples"

    var concatSum3 = 1*x + 1*y + " apples";

    // concatSum3 == "NaN apples"

    var concatSum4 = parseInt(x) + parseInt(y) + " apples";

    // concatSum4 == "3 apples"

  • 1 decade ago

    I usually do it by adding the empty string first because if there is one known string value the the entire expression will be treated as a string.

    var myString = "" + x + y;

    However I believe you can also do it by casting like this.

    var myString = String(x) + String(y)

    Likewise you could force it to be a number

    var myTotal = Number(x) + Number(y);

  • Anonymous
    5 years ago

    Are you expert in Javascript. i Need your help please contact with me at face book Dhusor megh is my id name.

Still have questions? Get your answers by asking now.