2 ways to use javascript string with examples

The 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 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 string as javascript object example

Following example creates a string javascript object by using the new keyword. To see 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 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.

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

Following is a 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 Javascript string replace method.

Also see Javascript array

Was this article helpful?

Related Articles

Leave A Comment?