JavaScript String Object

The String object is used to process existing blocks of characters.

JavaScript strings

A string is used to store a series of characters like "John Doe".

a string can use single or double quotes:


var carname="Volvo XC60";
var carname='Volvo XC60';

you use the position (index) to access any character in the string:


var character=carname[7];

the index of the string is zero-based, so the first character of the string is [0], the second character is [1], and so on.

you can use quotation marks in strings, as follows:


var answer="It's alright";
var answer="He is called 'Johnny'";
var answer='He is called "Johnny"';

or you can use the escape character (\) in a string using quotation marks:


var answer='It\'s alright';
var answer="He is called \"Johnny\"";

 Try It! 


String uses the length property length to calculate the length of the string:


var txt="Hello World!";


Try It! 

Find the string in strings

Strings use indexOf() to locate the first occurrence of a specified character in the string:


var str="Hello world, welcome to the universe.";
var n=str.indexOf("welcome");

Try It! 

if no corresponding character function is found, -1 is returned

The lastIndexOf() method starts at the end of the string to find where the string appears.

Content matching

The match() function is used to find a specific character in a string and, if found, return that character.


var str="Hello world!";
document.write(str.match("world") + "<br>");
document.write(str.match("World") + "<br>");

Try It! 

Replace the content

The replace() method replaces some characters with others in the string.


str="Please visit Microsoft!"
var n=str.replace("Microsoft","TutorialFish");

 Try It! 

String case conversion

String case conversion uses the functions toUpperCase() / toLowerCase():


var txt="Hello World!"; // String
var txt1=txt.toUpperCase(); // txt1 text will be converted to uppercase
var txt2=txt.toLowerCase(); // txt2 text will be converted to lower case

 Try It! 

The string is converted to an array

strings are converted to arrays using the split() function:


txt="a,b,c,d,e" // String
txt.split(","); // Use comma to separate
txt.split(" "); // Use spaces to separate
txt.split("|"); // Use vertical bars to separate

 Try It! 

Special characters

In Javascript, you can use backslash (\) to insert special symbols, such as apostrophes, quotation marks, and other special symbols.

Check out the JavaScript code below:

var txt="We are the so-called "Vikings" from the north.";

In JavaScript, strings start and stop using single or double quotes. This means that the above string will be cut into: We are the so-called

to solve the above problem, you can use backslash to escape the quotation marks:

var txt="We are the so-called \"Vikings\" from the north.";

JavaScript will output the correct text string: We are the so-called "Vikings" from the north.

the following table lists the other special characters that can be escaped using backslashes:

\"double quotation marks
\\diagonal bar
\nline breaks
\fpage feeds

String properties and methods


  • length

  • prototype

  • constructor


  • charAt()

  • charCodeAt()

  • concat()

  • fromCharCode()

  • indexOf()

  • lastIndexOf()

  • match()

  • replace()

  • search()

  • slice()

  • split()

  • substr()

  • substring()

  • toLowerCase()

  • toUpperCase()

  • valueOf()

For more methods and properties, see JavaScript String Objects