How do I combine 2 javascript variables into a string

后端 未结 5 882
有刺的猬
有刺的猬 2020-12-03 03:40

I would like to join a js variable together with another to create another variable name... so it would be look like;

for (i=1;i<=2;i++){
    var marker =         


        
5条回答
  •  执笔经年
    2020-12-03 04:04

    ES6 introduce template strings for concatenation. Template Strings use back-ticks (``) rather than the single or double quotes we're used to with regular strings. A template string could thus be written as follows:

    // Simple string substitution
    let name = "Brendan";
    console.log(`Yo, ${name}!`);
    
    // => "Yo, Brendan!"
    
    var a = 10;
    var b = 10;
    console.log(`JavaScript first appeared ${a+b} years ago. Crazy!`);
    
    //=> JavaScript first appeared 20 years ago. Crazy!
    

提交回复
热议问题