HTML CSS Bootstrap Javascript Icons Python
JavaScript Reference
Overview
JS Reference
JS Array Object JS Boolean Object JS Date Object JS Style Declaration Object JS Error Object JS Global Object JS Math Object JS Number Object JS String Object JS RegExp Object JS Statements
DOM Reference
DOM Attribute DOM Console DOM Document DOM Element DOM Events DOM Event Objects DOM History DOM HTMLCollection DOM Location DOM Navigator DOM Screen DOM Style DOM Window WEB Storage

JavaScript String Reference

JavaScript strings are used for storing and manipulating text.

The String Object

The JavaScript String object is a global object that is used to store strings.

A string can be any text inside double-quotes or single-quotes:

Unlike some other languages, JavaScript makes no distinction between single-quoted strings and double-quoted strings.

JavaScript string are zero-indexed: the first element of a string is at index 0, the second is 1, and so on.

You can learn more about Strings in our JavaScript String Tutorial.


String Object Properties

The following table lists the properties of the String object:

Property Description
constructor Returns the function that created the prototype of String object
length Returns the length of a string
prototype Allows you to add new properties and methods to an object

String Object Methods

The following table lists the methods of the String object:

Method Description
charAt() Returns the character at the specified index
charCodeAt() Returns the Unicode of the character at the specified index
concat() Joins two or more strings, and returns a new string
endsWith() Checks whether a string ends with a specified substring
fromCharCode() Converts Unicode values to characters
includes() Checks whether a string contains the specified substring
indexOf() Returns the index of the first occurrence of the specified value in a string
lastIndexOf() Returns the index of the last occurrence of the specified value in a string
localeCompare() Compares two strings in the current locale
match() Matches a string against a regular expression, and returns an array of all matches
repeat() Returns a new string which contains the specified number of copies of the original string
replace() Replaces the occurrences of a string or pattern inside a string with another string, and return a new string without modifying the original string
search() Searches a string against a regular expression, and returns the index of the first match
slice() Extracts a portion of a string and returns it as a new string
split() Splits a string into an array of substrings
startsWith() Checks whether a string begins with a specified substring
substr() Extracts the part of a string between the start index and a number of characters after it
substring() Extracts the part of a string between the start and end indexes
toLocaleLowerCase() Converts a string to lowercase letters, according to host machine's current locale
toLocaleUpperCase() Converts a string to uppercase letters, according to host machine's current locale
toLowerCase() Converts a string to lowercase letters
toString() Returns the value of a String object
toUpperCase() Converts a string to uppercase letters
trim() Removes whitespace from the beginning and end of a string
valueOf() Returns the primitive value of a String object

Note: All string methods return a new value. They do not change the original variable.