tutorialstonight logo
A Platform To Learn To Code

JAVASCRIPT STRING



What is javascript string?

Javascript String is a primitive data type that is used to store textual values. Example "Hello World", "learning javascript", etc.

The string value must be enclosed in single quotes ( '' ), double quotes ( "" ) or in backticks ( `` ).

'Hello World'
"Hello World"
`Hello World`

Example: Single quote and Double quote

Run the code

All the above examples are considered to be a string.


Javascript backticks vs quotes

Backticks are different from quotes. Backticks allow us to embed any javascript expression into the string, by wrapping it in ${...} but you can't write any javascript expression inside single or double-quotes.

Expression inside ${...} will be evaluated and output will be returned. Example : `1 + 1 = ${1+1}` will output '1 + 1 = 2'.

Example

Run the code

Backticks also allow the span of string to multiple lines. See the example below:

Example

Run the code

Defining String in Javascript

There are 2 ways to create a string in javascript

  1. String literal
  2. Using String constructor

  1. Defining String using string literal is easy and preferable. To define using string literal just wrap text in single quotes ( '' ), double quotes ( "" ) or in backticks ( `` ).

    Example: String Using string literal

    Run the code
  2. To create a string using String constructor use the new keyword and pass text in the String function either single quotes or double-quotes.

    Example: String Using String constructor

    Run the code

Length Of String In Javascript

In javascript, String has many properties and predefined methods. We will learn about string methods in the coming chapter.

One of such properties is length property. It is used to find the length of a string.

Example: String Length

Run the code

String Concatenation In Javascript

Concatenation is adding 2 or more than 2 strings into a single string. In javascript, String can be concatenated using + operator between two different strings.

Example: String concatenation

Run the code

Escape Character in Javascript

Sometimes you need to show quotes in texts but when you write extra quotes in the string then Javascript gets confused for where the string ends. See the example below, we want the "ocean" to be in double-quotes.

"Color of "ocean" is Blue."

The above string will be chopped at "Color of ". And will also cause an error Uncaught SyntaxError: unexpected token: identifier, because javascript identifies string end at 2nd occurrence of the double quote and the next character becomes meaningless for it.

To solve the above problem we use the backslash escape character ( \ ) in the string.

Backslash character ( \ ) is an escape character which is used to escape the character which has a special meaning, like a single quote, double quote, backtick, etc.

To Display Write Output
double quote ( " ) \" "
single quote ( ' ) \' '
backslash ( \ ) \\ \

Here is how we can write the above sentence in javascript.

Example: String escape character

Run the code

The code \' escape single quote.

Example: Single quote escape

Run the code

To escape backslash itself, write 2 backslashes together (\\).

Example: Escaping backslash

Run the code

Here are more commonly used escape characters in javascript:

Character Description
\n New Line (use in string to create new line)
\t Horizontal Tab (use in string to create a tab)
\b backspace
\f form feed
\v Verticle tab
\f form feed
\xXX Unicode character. Example: \x23
\uXXXX A Unicode symbol. Example: \u00A5

Example

Run the code

points to remember Points to remember:
  1. Javascript string is used to store textual values.
  2. You can use a single quote, double quote and backticks to create a string.
  3. There are 2 ways to create a string: string literal and String Constructor.
  4. Backslash \ is used as an escape character.
  5. Use length property to find the length of the string.