Skip to content

JavaScript space character

  • by

Use   to show space in an HTML document. You can create a string with multiple spaces in JavaScript.

let a = `something       something`;
var a = 'something' + '&nbsp &nbsp &nbsp &nbsp &nbsp' + 'something'

JavaScript space character

Simple example code.

<!DOCTYPE html>
<html>
<body>
  <script>
    var str ='something' + '&nbsp &nbsp &nbsp &nbsp &nbsp' + 'something'
    document.write(str)

  </script>

</body>
</html>

Output:

JavaScript space character

Or

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 &nbsp; character entity.

var a = 'something' + '&nbsp &nbsp &nbsp &nbsp &nbsp' + 'something';

document.body.innerHTML = a;

Space in JavaScript console

<script>
    var a = 'something' + '\xa0\xa0\xa0\xa0\xa0\xa0\xa0' + 'something';
    console.log(a)
</script>

You could compare with “==” instead of “===” and use ‘\xa0’ for comparing &nbsp; which is the actual character for non-breaking space.

Do comment if you have any doubts or suggestions on this Js topic.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading