Algorithm


  1. Declare a Variable:

    • Declare a variable to store the multiline string.
  2. Use Template Literals:

    • Utilize template literals (enclosed by backticks ``) for multiline strings.
    • Break lines within the template literal as needed.

 

Code Examples

#1 Code Example- Create Multiline Strings Using +

Code - Javascript Programming

// program to create a multiline strings

// using the + operator
const message = 'This is a long message\n' + 
    'that spans across multiple lines\n' + 
    'in the code.'

console.log(message);
Copy The Code & Try With Live Editor

Output

x
+
cmd
This is a long message
that spans across multiple lines
in the code.

#2 Code Example- Create Multiline Strings Using \

Code - Javascript Programming

// program to create a multiline strings

// using the \ operator
const message = 'This is a long message\n \
that spans across multiple lines\n \
in the code.'

console.log(message);
Copy The Code & Try With Live Editor

Output

x
+
cmd
This is a long message
that spans across multiple lines
in the code.

#3 Code Example- Create Multiline Strings Using Template Literal

Code - Javascript Programming

// program to create a multiline strings

// using the template literal

const message = `This is a long message
that spans across multiple lines
in the code.`

console.log(message);
Copy The Code & Try With Live Editor

Output

x
+
cmd
This is a long message
that spans across multiple lines
in the code.
Advertisements

Demonstration


JavaScript Programing Example to Create Multiline Strings-DevsEnv

Previous
JavaScript Practice Example #3 - Assign 3 Variables and Print Good Way