Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I add multiple spaces with ES6 string templates?

I have a string template that currently looks like this:

var option = "\u00A0" + "\u00A0" + "\u00A0" + "\u00A0" + option.name;

that I am trying to change to the new ES6 syntax

var option = `    ${option.name}`

But when it shows up in on screen none of the spaces are in the ES6 version or there is no 4 space indention on the strings in which I am specifying it. The problem might have something to do with me using these strings in as options in a select. Any ideas?

like image 824
jhamm Avatar asked Oct 27 '15 20:10

jhamm


People also ask

How do you add multiple spaces in JavaScript?

Use   It is the entity used to represent a non-breaking space. It is essentially a standard space, the primary difference being that a browser should not break (or wrap) a line of text at the point that this occupies.

How do you write multi-line strings in template literals?

HTML. Example 2: If you use double/single quote to write multiline string then we use the newline character (\n). Use an extra backslash ( \ ) after every newline character (\n), this backslash tells the JavaScript engine that the string is not over yet.

How do you add a line break in string template?

To escape a backtick in a template literal, put a backslash ( \ ) before the backtick.

How do you add a space in a string literal?

Hey, you can simply do the following: string string1 = "Rose" + new string(' ', 10);


1 Answers

In the first example you are using a non-breaking space (\u00A0) and in the second example a normal space (\u0020). So in addition to changing the syntax, you also changed the characters.

This doesn't really have anything to do with ES6 in particular. If you use the same character it will work as expected:

var option = `\u00A0\u00A0\u00A0\u00A0${option.name}`;
like image 136
Felix Kling Avatar answered Oct 19 '22 04:10

Felix Kling