How to concatenate variables in javascript?

后端 未结 3 667
梦谈多话
梦谈多话 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条回答
  • 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 = "<input type='checkbox'> ";
        output += "<input type='text'> ";
        return output;
    } //end of NewMenuItem(){
    
    0 讨论(0)
  • 2021-01-29 16:26

    "+=" is the standard way to concatenate in javascript;

    var a = "yourname";
    var b = "yourlastname";
    var name = a + b;
    var complete_name = "my name is: ";
    complete_name += name;
    

    result : my name is: yourname yourlastname

    0 讨论(0)
  • 2021-01-29 16:31

    With Concat function or with plus operator (+).

    Check this link jsfiddle to see a working example.

    0 讨论(0)
提交回复
热议问题