How to create string with multiple spaces in JavaScript

前端 未结 5 642
北海茫月
北海茫月 2020-12-08 06:37

By creating a variable

var a = \'something\' + \'        \' + \'something\'

I get this value: \'something something\'.

5条回答
  •  猫巷女王i
    2020-12-08 07:15

    With template literals, you can use multiple spaces or multi-line strings and string interpolation. Template Literals are a new ES2015 / ES6 feature that allows you to work with strings. The syntax is very simple, just use backticks instead of single or double quotes:

    let a = `something                 something`;
    

    and to make multiline strings just press enter to create a new line, with no special characters:

    let a = `something 
    
        
                             something`;
    

    The results are exactly the same as you write in the string.

提交回复
热议问题