How to create string with multiple spaces in JavaScript

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


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?





use xa0 code for each ` ` space
– Andrew Evt
Nov 5 '15 at 8:33


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' + '&nbsp &nbsp &nbsp &nbsp &nbsp' + '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' + '&nbsp &nbsp &nbsp &nbsp &nbsp' + '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?

Popular posts from this blog

Keycloak server returning user_not_found error when user is already imported with LDAP

PHP parse/syntax errors; and how to solve them?

How to scale/resize CVPixelBufferRef in objective C, iOS