EN
JavaScript - no-break / non-breaking space in string
8 points
In this very short article, we are going to look at how to insert a no-break (non-breaking) space character to string in JavaScript.
Quick solution:
Use the following
\xA0
character code.
xxxxxxxxxx
1
var text = 'a \xA0\xA0\xA0 b'; // U+00A0
2
3
console.log(text); // a b
Non-breaking space in unicode:
Unicode code |
JavaScript |
HTML |
HTML |
U+00A0 |
|   ,   or   | |
There are a few ways how to do it:
- by pasting code directly into JavaScript string - some codes could be invisible without hex editor,
- with
innerText
ortextContent
property and unicode character code, - with
innerHTML
property and html unicode code (e.g.
, 
or 
).
Check the below example:
xxxxxxxxxx
1
2
<html>
3
<body>
4
<div id="element"></div>
5
<script>
6
7
var element = document.querySelector('#element');
8
9
element.innerText = 'a \xA0\xA0\xA0 b'; // or \u00A0
10
11
// element.textContent = 'a \xA0\xA0\xA0 b'; // or \u00A0
12
// element.innerHTML = 'a b'; // or   or  
13
14
</script>
15
</body>
16
</html>