Tutorials Tonight's Logo


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`
var str1 = 'Hello World!'; // Single Quotes
var str2 = "Hello World!"; // Double Quotes
var str3 = `Hello World!`; // backticks
▶ 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'.

var valueOfPI = `Value of PI = ${Math.PI}.`;
var sum = `10 + 15 = ${10 + 15}`;
var multiplication = `20 * 12 = ${20 * 12}`;
▶ Run the code

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

var webStack = `Languages:
  * HTML
  * CSS
  * Javascript`;
▶ 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 ( `` ).

    var str1 = 'Hello World!'; // Single Quotes
    var str2 = "Hello World!"; // Double Quotes
    var str3 = `Hello World!`; // Backticks
    ▶ 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.

    var str1 = new String("Hello World!"); // 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.

var alphabets = "abcdefghijklmnopqrstuvwxyz";
var length = alphabets.length; // output: 26
▶ 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.

var str1 = "Hello ";
var str2 = "world";
var str3 = str1 + str2; // Hello world
▶ 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.

var sentence =  "Color of \"ocean\" is Blue.";
▶ Run the code

The code \' escape single quote.

var sentence =  'Escaping 'single quotes'.';
▶ Run the code

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

var sentence =  "This is how to display backslash \\ itself";
▶ 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
var a = "\x23";
var b = "\u00A5";
▶ Run the code

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.

Follow Us

Copyright © Tutorials Tonight. All Rights Are Reserved.