JavaScript > Strings

Strings in JavaScript

How to store and manipulate the text in JavaScript?

By using strings in JavaScript we can store a series of characters (names, senetences, letters, words) and also we can manipulate the text. A string is nothing but the text inside the quotes, either it may be double or single quotes.

       <div id="myId"></div>

        var x = "Sheo Narayan";
        var y = 'SN ITFunda';
        document.getElementById("myId").innerHTML = x + "<br/>" + y;

In the above code snippet we have two strings in script block in that one is "Sheo Narayan" which is in double quotes and another one is 'SN ITFunda' which is in single quotes, below that there is an document.getElementById("myId")which returns the element that has Id "id=myID" in the HTML page.

The variable 'x' with string "Sheo Narayan" and varible 'y' with string 'SN ITFunda' appeared in seperate lines in the output, because we have given the breakline <br/> in between them. 


In the same way we can quote inside a string also, in case if we want to insert the quotes directly without inserting any special characters in the string, there must be difference in inside and outside used quotes, if we use single quotes outside the string, we should only use the double quotes inside the string and in the same way if we use double quotes outside the string, we should only use single quotes inside the string.

       <div id="getId"></div>

        var a = "This is 'DotNetFunda'";
        var b = 'This is "TechFunda"';
        var c = "This is 'KidsFunda' & 'FarmingFunda'";
        document.getElementById("getId").innerHTML = a + "<br/>" + b + "<br/>" + c

Inserting quotes inside string

In the above code snippet we have three strings along with the variables a, b, c. We used the different quotes inside and outside the strings. Three strings are appeared one after the other in the new line because we use the breakline <br/> in between them.


 Views: 5445 | Post Order: 62

Write for us

Hosting Recommendations