Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

JavaScript string


May 06, 2021 JavaScript


Table of contents


JavaScript string


JavaScript strings are used to store and process text.


JavaScript string

Strings can store a series of characters, such as "John Doe."

A string can be any character inserted into quotation marks. You can use single or double quotes:

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

You can use the index location to access each character in the string:

var character = carname[7];

The index of the string starts at 0, which means that the first character index value is .0, the second is .1, and so on.

You can use quotation marks in strings, and the quotation marks in strings should not be the same as quotation marks in strings:

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

You can also add escape characters to strings to use quotation marks:

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

Try it out . . .


The length of the string

You can use the built-in property length to calculate the length of the string:

var txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var sln = txt.length;

Try it out . . .

Tip: You can learn more about JavaScript's ability to get string lengths in the JavaScript programming battle section of this site.


Special characters

In JavaScript, strings are written in single or double quotes.

Because of this, the following instance JavaScript cannot be resolved: x

"We are the so-called "Vikings" from the north."

The string "We are the so-called" is truncated.

How to solve the above problems? You can use the backslash to escape the double quotes in the "Vikings" string, as follows:

"We are the so-called \"Vikings\" from the north."

The backslash is an escape character. Escape characters convert special characters to string characters:

Escape characters () can be used to escape apostrophes, line changes, quotation marks, and other special characters.

The following table lists special characters that can be escaped using escape characters in strings:

Code Output
\' Single quotes
\" Double quotes
\\ Backslash
\n Line change
\r Enter
\t tab (tab)
\b The back-checker
\f Page break


The string can be an object

Typically, the JavaScript string is the original value and can be created using characters: var firstName s "John"

But we can also use the new keyword to define a string as an object: var firstName s new String ("John").

var x = "John";
var y = new String("John");
typeof x // returns String
typeof y // returns Object

Try it out . . .
JavaScript string Do not create String objects. It slows down execution and may have other side effects:

var x = "John";
var y = new String("John");
(x === y) // is false because x is a string and y is an object.

Try it out . . .


String properties and methods

The original value string, such as "John," has no properties and methods (because they are not objects).

The original value can use JavaScript's properties and methods because JavaScript can execute methods and properties as objects.

The string method we'll cover in the next chapter.


String property

Property Describe
constructor Returns the function that created the string property
length Returns the length of the string
prototype Allows you to add properties and methods to an object


String method

Method Describe
charAt() Returns the character that specifies the location of the index
charCodeAt() Returns the Unicode value of the character that specifies the index position
concat() Connect two or more strings, returning the connected string
fromCharCode() Convert the specified Unicode value to a string
indexOf() Returns the location in the string where the specified character appears for the first time
lastIndexOf() Returns the location in the string where the specified character last appeared
localeCompare() Compare the two strings in a local-specific order
match() A match was found for one or more regular expressions
replace() Replaces a substruce that matches a regular expression
search() Retrieves the value that matches the regular expression
slice() Extracts pieces of the string and returns the extracted part in the new string
split() Split the string into substring arrays
substr() Extracts the specified number of characters from the string from the starting index number
substring() Extracts the character between two specified index numbers in the string
toLocaleLowerCase() Depending on the host's locale, strings are converted to lesscase, and only a few languages, such as Turkish, have local-specific case maps
toLocaleUpperCase() Depending on the host's locale, the string is converted to capital, and only a few languages, such as Turkish, have a local-specific case map
toLowerCase() Convert the string to small case
toString() Returns the value of the string object
toUpperCase() Convert the string to capital
trim() Removes the blanks from the beginning and end of the string
valueOf() Returns the original value of a string object

Related articles

JavaScript Standard Reference Manual: JavaScript String Object