Strings in JavaScript

The string is the way to store a word or combination of words in string variables. The string variables are basically objects. The string object has properties and built-in functions that you can use to manipulate strings in JavaScript.

This tutorial explains how to use strings with examples and at the bottom part, you can see useful JavaScript string functions’ links as well.

Declaring string variables

This is how you can create JS string as a primitive type:

var str_name = “Your name”;

You can also use the new keyword with String to create a string:

var str_name = new String(“Your name”);

Normally, you should use primitive type to create strings as these are faster in execution.

* A string in JavaScript is enclosed in single or double quotes.

A string example by primitive type

Following is an example of creating a string variable. We will create a string variable with primitive values.

Experience this example online

Creating a string as JavaScript object example

Following example creates a string JvaScript object by using the new keyword. To see the demo online, click the link below:

Experience this example online

Dealing with special characters in strings

As mentioned earlier you can use single or double quotes to enclose strings. What if your string contains quoted text? For example,

var str_quoted = (“This is “quoted” string”);

This is a wrong way to create a string in JavaScript.

This is how a string with double quotes can be created by using a backslash:

var str_quoted = (“This is \”quoted\” string”);

See following working example to learn more about this.

JavaScript escape quotes example

Following is an example of using JavaScript escape quotes with the backslash:

Experience this example online

As you can see, javascript escapes the quotes by using a backslash. The quotes can be seen in the alert.

JavaScript escape characters

Similarly, you can use other JavaScript escape characters as follows:

  • \” – To add double quotes in the string
  • \’ – To add single quote in the string
  • \n – adds a line in the string

Useful JavaScript string functions and properties

Following are a few useful string functions and properties that you can use to manipulate strings. Click on any to go to its detailed chapter with online examples.

  • String length (property)
  • JavaScript substring
  • JavaScript replace
  • Javascript split string
  • Slice method of string
  • Trim

Let us look at a few string method examples.

A string length property example

Following is a JavaScript string length property example. The length property returns the total number of characters in a given string. As you click on the button, the number of characters in the string will be shown in an alert.

Experience this example online

JavaScript replace method example

The replace method searches the string and returns a new string after replacing the given strings.

Experience this example online

The demo shows two alerts before and after the string replace method.