wsc
wsc

Reputation: 916

I want to concatenate a couple of variables with unknown types in a string

concatenation with pluses might lead to unexpected results, In fact I want to get the result 123

var one = 1;
var two = 2;
var three = '3';
var result = one + two + three;
alert(result);//I want-> 123

Upvotes: 1

Views: 156

Answers (2)

Rayon
Rayon

Reputation: 36609

+'' will cast it to string

Try this:

var one = 1;
var two = 2;
var three = '3';
var result = one + '' + two + '' + three;
alert(result);

Upvotes: 1

user5563486
user5563486

Reputation:

var one = 1;
var two = 2;
var three = '3';

var result = ''.concat(one, two, three); //"123"
alert(result)

Upvotes: 4

Related Questions