如何将字符串附加到Javascript变量

时间:2013-03-06 20:51:44

标签: javascript variables

我有一个Javascript变量var test="hello"我想添加前缀" world"到函数中的变量字符串,以便test =" worldhello"。如何在JavaScript代码中编写该命令?

4 个答案:

答案 0 :(得分:0)

字符串连接: test = "world"+test

答案 1 :(得分:0)

var test = "hello";
var newStr = "world" + test;

答案 2 :(得分:0)

var test = "hello";
test = test + " world";

var world = " world";
var test = "hello" + world;

答案 3 :(得分:0)

var var2 = "world";
test = "hello "+var2;

在函数中

function concatenate(str,con){ 
        return str.toString()+con.toString(); //toString() for prevent an int or float sum
 }
var x = concatenate('hello ','world'); //hello world
var y = concatenate(x,"!!!"); //hello world!!!