TexChen
TexChen

Reputation: 1

Why two or more spaces becomes one space in Javascript

I have created a simple HTML/JS/Angular script to demonstarte the problem I have faced. I have a work-around, but still wanted to post this question to the experts.

<body ng-app>
    <div ng-controller='abc'>
         <h1 ng-click='title= "A     B     C    D        EFGH";'>{{title}}   </h1>
    </div>
</body>

Angualr Code:

function abc($scope) {
   $scope.title = 'Hello World';
};

When Clciked on : continuous spaces in-side the string are replaced with single space. Output is "A B C D EFGH"

Why is this happening?

Upvotes: 0

Views: 117

Answers (1)

tomaoq
tomaoq

Reputation: 3056

Collapsing spaces is just another browser's default behavior that you can change using a CSS property, and in this case it is white-space.

white-space CSS property on MDN

As an example, this will preserve the amount of spaces you entered.

h1 {
    white-space: pre;
}

Upvotes: 2

Related Questions