Search This Blog

2024/03/23

Javascript:Multiline string



A string is an ordered sequence of character values.

Specifically, a string is a sequence of one or more
characters that can be either letters, numbers, or
symbols (such as punctuation marks).

There are three ways you can create a string in JavaScript:

By using single quotes.
By using double quotes.
By using backticks.

There are three ways to create strings that span multiple lines:

By using template literals.(backticks)
By using the + operator – the JavaScript concatenation operator.
By using the \ operator – the JavaScript backslash operator and
escape character.

If you choose to use single or double quotes instead of template
literals to create a string that spans multiple lines,
you would have to use either the + operator or the \ operator.


Code :
//putting string in single quote or double quote doesn't effect both
treated as same.

//using backslash
let multiLineStringOne="India is my country.\
\nAll indians are my brother & sister.\
\nI love my country and I am proud of its rich and varied heritage.\
\nI shall always strive to be worthy of it. \
\nI shall give my parents, teachers and all elders respect and treat everyone with courtesy.";

//quoting in backtick - the most simple way
let multiLineStringTwo=`India is my country.
All indians are my brother & sister.
I love my country and I am proud of its rich and varied heritage.
I shall always strive to be worthy of it.
I shall give my parents, teachers and all elders respect and treat everyone with courtesy.`;

/* using backslash at end of string
& hypen at beginning of string,hypen followed by space or followed nothing also work
even if we remove hypen it does not affect final output
*/
let multiLineStringThree='India is my country.\
- \nAll indians are my brother & sister. \
- \nI love my country and I am proud of its rich and varied heritage. \
- \nI shall always strive to be worthy of it. \
- \nI shall give my parents, teachers and all elders respect and treat everyone with courtesy.';

//using concatenation
let multiLineStringFour='India is my country.' +
'\nAll indians are my brother & sister.' +
'\nI love my country and I am proud of its rich and varied heritage.' +
'\nI shall always strive to be worthy of it.' +
'\nI shall give my parents, teachers and all elders respect and treat everyone with courtesy.';

let multiLineStringFive ='India is my country.\n\
All indians are my brother & sister. \n\
I love my country and I am proud of its rich and varied heritage. \n\
I shall always strive to be worthy of it. \n\
I shall give my parents, teachers and all elders respect and treat everyone with courtesy.';


console.log("-----------------------First----------------------");
console.log(multiLineStringOne)

console.log("-----------------------Second----------------------")
console.log(multiLineStringTwo)

console.log("----------------------Third-----------------------")
console.log(multiLineStringThree)

console.log("------------------------Forth-----------------------")
console.log(multiLineStringFour)

console.log("------------------------Five-----------------------")
console.log(multiLineStringFive)

Output:
-----------------------First----------------------
India is my country.
All indians are my brother & sister.
I love my country and I am proud of its rich and varied heritage.
I shall always strive to be worthy of it.
I shall give my parents, teachers and all elders respect and treat everyone with courtesy.
-----------------------Second----------------------
India is my country.
All indians are my brother & sister.
I love my country and I am proud of its rich and varied heritage.
I shall always strive to be worthy of it.
I shall give my parents, teachers and all elders respect and treat everyone with courtesy.
----------------------Third-----------------------
India is my country.-
All indians are my brother & sister. -
I love my country and I am proud of its rich and varied heritage. -
I shall always strive to be worthy of it. -
I shall give my parents, teachers and all elders respect and treat everyone with courtesy.
------------------------Forth-----------------------
India is my country.
All indians are my brother & sister.
I love my country and I am proud of its rich and varied heritage.
I shall always strive to be worthy of it.
I shall give my parents, teachers and all elders respect and treat everyone with courtesy.
------------------------Five-----------------------
India is my country.
All indians are my brother & sister.
I love my country and I am proud of its rich and varied heritage.
I shall always strive to be worthy of it.
I shall give my parents, teachers and all elders respect and treat everyone with courtesy.

Note:

convention is to use the \n newline character followed by the \ o
perator to make the string span multiple lines.

No comments:

Post a Comment