How to create string with multiple spaces in JavaScript

Multi tool use


How to create string with multiple spaces in JavaScript
By creating a variable
var a = 'something' + ' ' + 'something'
I get this value: 'something something'
.
'something something'
How can I create a string with multiple spaces on it in JavaScript?
xa0
3 Answers
3
Use xa0
- it is a NO-BREAK SPACE char.
xa0
Reference from UTF-8 encoding table and Unicode characters, you can write as below:
var a = 'something' + 'xa0xa0xa0xa0xa0xa0xa0' + 'something';
Nice! When you add it to an html element jQuery converts xa0 to
– Buffalo
Aug 10 '16 at 14:45
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.
var a = 'something' + '         ' + 'something'
A common character entity used in HTML is the non-breaking space ( ).
Remember that browsers will always truncate spaces in HTML pages. If you write 10 spaces in
your text, the browser will remove 9 of them. To add real spaces to your text,
you can use the
character entity.
http://www.w3schools.com/html/html_entities.asp
Demo
var a = 'something' + '         ' + 'something';
document.body.innerHTML = a;
You can use the <pre>
tag with innerHTML. The HTML <pre>
element represents preformatted text which is to be presented exactly as written in the HTML file. The text is typically rendered using a non-proportional ("monospace") font. Whitespace inside this element is displayed as written. If you don't want a different font, simply add pre
as a selector in your CSS file and style it as desired.
<pre>
<pre>
pre
Ex:
var a = '<pre>something something</pre>';
document.body.innerHTML = a;
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?
use
xa0
code for each ` ` space– Andrew Evt
Nov 5 '15 at 8:33