How to concatenate variables in javascript?

后端 未结 3 666
梦谈多话
梦谈多话 2021-01-29 15:41

I am trying to concatenate variables in inside a function and then return. In php we just put a period before the \"=\" but it is not working in javascript.

Can someone

3条回答
  •  闹比i
    闹比i (楼主)
    2021-01-29 16:20

    The + operator will concatenate two strings, ie. "Hello" + " World" //> "Hello World". Using += is a short cut for assigning and concatenating a variable with its self.

    ie. instead of:

    var myVar = "somestring";
    myVar = myVar + "another String";
    

    you can just do:

    var myVar = "somestring";
    myVar += "another String";
    

    For your problem:

    function NewMenuItem() {
        //This is just a small example. The end result is more broader then this
        var output = " ";
        output += " ";
        return output;
    } //end of NewMenuItem(){
    

提交回复
热议问题