How to repeat spaces

I am trying to get a line similar to the following:

[==== ]

For this, I use this code:

 '[' + '='.repeat (4) + ' '.repeat (4) + ']' 

... but I just get [==== ] ; those. all spaces are compressed into one. I checked this code on the Google Chrome console and really ' '.repeat(25) only creates one space. How to make repeat really produce more than one space?

I did not find anything related in the manual .

-1
source share
2 answers

Browsers collapse multiple spaces to prevent it from using non-breaking spaces .

Therefore, in your case, you can use

 ' '.repeat(4) 

(His argument is that the debug console should not strip spaces, but in your case it seems)

+1
source

This is a Chrome Developer Tools issue. Empty spaces can be repeated, but they are displayed as a single character, which is incorrect:

You can check the result as follows:

 (' ' + ' ').length // => 2 

Here is an example from the Node.js REPL:

In conclusion, your code does work, but the Google Chrome console does not display the result correctly.

If you're talking about displaying in HTML elements that don't have monospace fonts, use the @Jamiec solution using non-breaking spaces :

 '[' + '='.repeat (4) + ' '.repeat(4) + ']' 

As @James Thorpe, this was a bug in Chrome that was fixed. We will probably fix the fix when updating the browser version.

+6
source

Source: https://habr.com/ru/post/1235428/


All Articles