Create a string with multiple spaces in JavaScript
We have a string with extra spaces and if we want to display it in the browser then extra spaces will not be displayed. Adding the number of spaces to the string can be done in the following ways. In this article, we are going to learn how to Create a string with multiple spaces in JavaScript.
Below are the methods to create a string with multiple spaces in JavaScript:
Table of Content
Method 1:Using the JavaScript substr() Method
This method gets a part of a string, starts at the character at the defined position, and returns the specified number of characters.
Syntax:
string.substr(start, length);
Example 1: This example adds spaces to the string by  .
// Input String
let string = "A Computer Science Portal";
// Display input string
console.log(string);
// Funcion to add space
function gfg_Run() {
console.log(
string.substr(0, 2) + " " + string.substr(2)
);
}
// Function call
gfg_Run();
Output
A Computer Science Portal A Computer Science Portal
Example 2: This example adds spaces to the string by \xa0(it's a NO-BREAK SPACE char).
// Input String
let string = "A Computer Science Portal";
// Display input string
console.log(string);
// Funcion to add space
function gfg_Run() {
console.log(
string.substr(0, 18) +
"\xa0\xa0\xa0\xa0\xa0\xa0\xa0 " +
string.substr(18)
);
}
// Function call
gfg_Run();
Output
A Computer Science Portal A Computer Science Portal
Method 2: Using padStart() and padEnd() method
These methods are used to add padding or spaces in the start and end respectively.
Example: In this example, we will use the above methods with the substr() method to add spaces in the string.
// Input String
let string = "A Computer Science Portal";
// Display input string
console.log(string);
// Funcion to add space
function gfg_Run() {
console.log(
string.substr(0, 2).padEnd(3, " ") +
string.substr(2, 17) +
string.substr(18).padStart(3, " ")
);
}
// Function call
gfg_Run();
Output
A Computer Science Portal A Computer Science Portal
Method 3: Using String Concatenation
In this method, we concatenate " " in between the strings to add multiple spaces.
Example: In this example, we are Using String Concatenation.
const stringWithSpaces = "This" + " " + "is" + " " + "a" + " " + "string" + " " + "with" + " " + "multiple" + " " + "spaces.";
console.log(stringWithSpaces);
Output
This is a string with multiple spaces.
Method 5: Using String Repeat Method
The String.prototype.repeat() method constructs and returns a new string which contains the specified number of copies of the string on which it was called.
Example:
// Define the string
let originalString = "This is a computer science portal";
// Define the number of spaces needed
let numOfSpaces = 5;
// Create a string with multiple spaces using the repeat() method
let stringWithSpaces = ' '.repeat(numOfSpaces);
// Display the original string with the string containing multiple spaces inserted
console.log(originalString.replace(" ", stringWithSpaces));
Output
This is a computer science portal
Method 6: Using Template Literals with Backticks
Template literals in JavaScript allow for more readable and flexible string formatting. Using backticks, you can directly include spaces in the string without needing special characters or methods. This approach is straightforward and enhances code readability.
Example:
const name = "John";
const greeting = `Hello, ${name}!`;
console.log(greeting);
// Example with multiple lines and spaces
const multiLineString = `This is a string
with multiple spaces and
lines.`;
console.log(multiLineString);
Output
Hello, John! This is a string with multiple spaces and lines.