4

I have a variable

var number = 1;

I'm then trying to add a 2 onto a number but not add them together like this.

1 + 2 = 2

but instead I want, to join 2 onto the number so i would get

12

1
  • Yeah i think i wa just over thinking this one Commented Jul 22, 2016 at 12:11

5 Answers 5

4

You can use += and add 2 as string then again convert it to number

var number = 1;
number += '2';
console.log(Number(number));

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you just what I was looking for
Clear answer. And straight to the point.
0

Use parseInt() to convert the value into integer.

Comments

0

Use this:

var number = 1;
var z = parseInt(number) + '2';

console.log(z);

Comments

0

One way is to add an empty string between the numbers like so:

var number = 1;
var result = number + "" + 2;
alert(result);

Another way it to cast the number to a string using toString()

var number = 1;
var result = number.toString() + 2;
alert(result);

Working example here

2 Comments

Thank you just what I was looking for
will do when it lets me
0

You can use the shorthand

var number = 1;
number = +(number + '' + 2);
console.log(number)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.