To use this website fully, you first need to accept the use of cookies. By agreeing to the use of cookies you consent to the use of functional cookies. For more information read this page.

Strings in JavaScript

Strings are sets of ordered characters that make up text. Strings in JavaScript can be in either a single quotation mark or ' (or apostrophe) or double quotation marks or " (speech marks).

The following example stores a string in a variable:

JavaScript
var x = "Hello";
var y = 'World';

This part of the tutorial will focus on manipulation of strings.

String tools

The following lists just some of the methods that can be applied to strings in JavaScript.

Replace a string inside a string

A string containing a substring can have that substring inside it replaced with the replace method:

JavaScript
var s = "Hello world";
s = s.replace("world", "everyone");
alert(s);

Get the length of a string

In JavaScript, the length of a string is determined by the length property of the string. Other languages such as PHP use a function to obtain the length of string. The following is an example of the length property:

JavaScript
var s = "Hello world";
var length = s.length;
alert(length);

Getting the index of the first occurance of a string

To get the index or position of a substring within another string can be found using the indexOf method:

JavaScript
var s = "Hello world";
var position = s.indexOf("world");
alert(position);

Getting a substring from a string

It is possible to cut a section of a string or substring from a string using the substr method:

JavaScript
var s = "Hello world";
var sub_string = s.substr(6, 5);
alert(sub_string);

The substr method takes two arguments, the first is the start index and the second is the length of the substring:

JavaScript
var s = "Hello world";
var sub_string = s.substr(0, 5);
alert(sub_string);

Concatenating two strings

Concatenation of strings means the joining of strings. In JavaScript it is achieved with the standard + operator:

JavaScript
var str1 = "Hello";
var str2 = "world";
var concatenated = str1 + str2;
alert(concatenated);

Since the plus operator also works with mathematics, the following will parse the mathematics and will not concatenate:

JavaScript
var str1 = 5;
var str2 = 10;
var concatenated = str1 + str2;
alert(concatenated);

It can be concatenated by converting it to a string first. This is achieved by concatenating an empty string ("") at the beginning of the concatenation:

JavaScript
var str1 = 5;
var str2 = 10;
var concatenated = "" + str1 + str2;
alert(concatenated);

Getting a character from a string position

Strings are, as mentioned before, an ordered set of characters. As such, strings are arrays. This means that the standard method of obtaining an element from within the string can be used. This will return the appropriate character at that position:

JavaScript
var s = "Hello world";
var character = s[6];
alert(character);
Provide feedback on this page
Comments are sent via email to me.