JavaScript Strings

A string (or a text string) is a series of characters like "John Doe". Strings are written with quotes. You can use single or double quotes:

   var carName = "Volvo XC60";     (Using double quotes)
   var carName = 'Volvo XC60';      (Using single quotes)


You can use quotes inside a string, as long as they don't match the quotes surrounding the string:

   var answer = "It's alright";      (Single quote inside double quotes)
   var answer = "He is called 'Johnny'";      (Single quotes inside double quotes)
   var answer = 'He is called "Johnny"';      (Double quotes inside single quotes)

HTML file: Displayed by browser:
<body>
<p id="demo"></p>
<script>
   var carName1 = "Volvo XC60";
   var carName2 = 'Volvo XC60';
   var answer1 = "It's alright";
   var answer2 = "He is called 'Johnny'";
   var answer3 = 'He is called "Johnny"';
   document.getElementById("demo").innerHTML =
   carName1 + "<br />" +
   carName2 + "<br />" +
   answer1 + "<br />" +
   answer2 + "<br />" +
   answer3;
</script>
</body>

More about strings later in this tutorial.

BackTable of ContentsNext