I\'d like for something like 5 + 6
to return \"56\"
instead of 11
.
This is the easy way to do this
var value=5+""+6;
You can also use toString
function to convert it to string and concatenate.
var a = 5;
var b = 6;
var value = a.toString() + b.toString();
Another possibility could be this:
var concat = String(5) + String(6);
I know this is an old post and has been answered many times. I too was wondering if JavaScript had a function that would do this. I was doing some math programming and needed to concatenate two numbers.
So the what if I needed to combine two numbers 17 and 29. Sure I can turn them into strings and concatenate them then turn the new string back into a number. That seems to work pretty well and I can go on with my code, but lets take a look here and try to figure out what is really happening here.
What are we doing to these two numbers, how do we take 17 and 29 and turn it into one thousand seven hundred and twenty-nine? Well we can multiply 17 by 100 then add 29. And how about 172 and 293 to get one hundred seventy-two thousand two hundred and ninety-three? Multiply 172 by 1000 and add 293. But what about only 2 and 9? Multiply 2 by 10 then add 9 to get 29.
So hopefully by now a pattern should be apparent to you. We can devise a math formula to do this calculation for us rather than just using strings. To concatenate any two numbers, a and b, we need to take the product of a and 10 to the power of length b then add b.
So how do we get the length of number b? Well, we could turn b into a string and get the length property of it.
a * Math.pow(10, new String(b).length) + b;
But there has to be a better way to do this without strings, right? Yes there is.
For any two numbers, a and b, with any base B. We are going to multiply a by base B to the power of length b (using log base of b then flooring it to get the nearest whole number then adding 1 to it) then adding b.
So now our code looks like this:
a * Math.pow(10, Math.floor(Math.log10(b)) + 1) + b;
But wait, what if I wanted to do this in base 2 or base 8? How can I do that? We can't use our formula that we just created with any other base but base 10. The JavaScript Math object already has built-in functions for base 10 and 2 (just Math.log), but how do we get log functions for any other base? We divide the log of b by the log of base. Math.log(b) / Math.log(base).
So now we have our fully functioning math based code for concatenating two numbers:
function concatenate(a, b, base) {
return a * Math.pow(base, Math.floor(Math.log(b) / Math.log(base)) + 1) + b;
}
var a = 17, var b = 29;
var concatenatedNumber = concatenate(a, b, 10);
// concatenatedNumber = 1729
If you knew you were only going to be doing base 10 math, you could add a check for base is undefined then set base = 10:
function concatenate(a, b, base) {
if(typeof base == 'undefined') {
base = 10;
}
return a * Math.pow(base, Math.floor(Math.log(b) / Math.log(base)) + 1) + b;
}
var a = 17, b = 29;
var newNumber = concatenate(a, b); // notice I did not use the base argument
// newNumber = 1729
To make it easier for me, I used the prototype to add the function to the Number object:
Number.prototype.concatenate = function(b, base) {
if(typeof base == 'undefined') {
base = 10;
}
return this * Math.pow(base, Math.floor(Math.log(b) / Math.log(base)) + 1) + b;
};
var a = 17;
var newNumber = a.concatenate(29);
// newNumber = 1729
Use "" + 5 + 6
to force it to strings. This works with numerical variables too:
var a = 5;
var b = 6;
console.log("" + a + b);
You can now make use of ES6 template literals.
const numbersAsString = `${5}${6}`;
console.log(numbersAsString); // Outputs 56
Or, if you have variables:
const someNumber = 5;
const someOtherNumber = 6;
const numbersAsString = `${someNumber}${someOtherNumber}`;
console.log(numbersAsString); // Outputs 56
Personally I find the new syntax much clearer, albeit slightly more verbose.