Strings — primitive type

A string type refers to one or more characters of text. In JavaScript, a string is enclosed inside single or double quotes. "Scripting master", 'HTML', 'JavaScript', and "1999", are all examples of strings. Why is "1999" a string? As we stated above, a variable of string type is surrounded by double or single quotes. Note, 1999, is an example of numerical type because of the absence of the quotes.

When working with strings, it is important to understand that strings must be either surrounded with single or double quotes. The following, for example, represents an invalid string:

"This is not a valid string.'

In the above example, we are not being consistent when creating the string: we start with double quotation marks but end with only single quote. If you need to use an apostrophe (or single quote) in the string, then, you may want to surround it with double quotation marks to make it a valid string. For example, this is not a valid

'they're here!'

string because we have unbalanced quotes. Our string contains a single quote while we start and end our string with a single quote. We can make that a valid string by using double quotes around the string:

"they're here!"

On the other hand, if you have to use double quotes in a string, then, you may want to avoid using double quotes around the string. As another example, the following is an invalid string:

""JavaScript" is easy."

because again here we have unbalanced quotes. Instead, you may create that string as:

'"JavaScript" is easy.'