Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to concatenate two numbers in javascript?

Tags:

javascript

People also ask

How do you concatenate numbers?

This means that you can no longer perform any math operations on them. To combine numbers, use the CONCATENATE or CONCAT, TEXT or TEXTJOIN functions, and the ampersand (&) operator. Notes: In Excel 2016, Excel Mobile, and Excel for the web, CONCATENATE has been replaced with the CONCAT function.

How do you concatenate variables in JavaScript?

In JavaScript, we can assign strings to a variable and use concatenation to combine the variable to another string. To concatenate a string, you add a plus sign+ between the strings or string variables you want to connect. let myPet = 'seahorse'; console.

Can you concat strings in JavaScript?

The concat() method joins two or more strings. The concat() method does not change the existing strings. The concat() method returns a new string.

What is a concatenation operator in JavaScript?

Joining multiple strings together is known as concatenation. The concatenation operator. The concatenation operator (+) concatenates two or more string values together and return another string which is the union of the two operand strings. The shorthand assignment operator += can also be used to concatenate strings.


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.


var value = "" + 5 + 6;
alert(value);

just use:

5 + "" + 6