String is a collection of text that we can store as a variable. Strings have to surround in quotation marks(“ ”).
eg: var name = "Sanduni";
If we want to use quotation marks as a part of string, how to do it?. For this, We have to use escape character(\) before the quotation marks of string to tell JavaScript to ignore these quotation marks because they are a part of string. Also they are wanted to printed out on the screen.
eg : var messege = “Teacher said, \“Do your home work\” ”;
String Concatenation
String concatenation is used to join 2 strings or 2 sentences. The + operator does string concatenation as soon as one of its operands is a string. Then the other operand is converted to string.
eg : var sentence1 = “I am Sanduni”;
var sentence2 =”and I live in Sri Lanka”
var combined_sentence = sentence1 + sentence2;
Use HTML elements inside the string
Think that if we want to write a string as header. To do that we can use <h1> tag inside the string such as,
Document.write(“<h1>Let’s learn JavaScript</h1>”);
JavaScript treat HTML tags inside the string as actual HTML tags.
Let's look at the example.
The result we got is
Hope you understand Strings. If you have any questions please comment below. Thank you.

No comments:
Post a Comment