JavaScript Strings
JavaScript strings are used to store and manipulate text.
JavaScript Strings
Strings can store a series of characters, like "John Doe".
Strings can be any character inserted into quotes. You can use single or double quotes:
Example
var carname = "Volvo XC60";
var carname = 'Volvo XC60';
You can access each character in a string using its index position:
Example
var character = carname[7];
The index of a string starts from 0, which means the first character has an index value of [0], the second is [1], and so on.
You can use quotes within a string, as long as they do not match the quotes surrounding the string:
Example
var answer = "It's alright";
var answer = "He is called 'Johnny'";
var answer = 'He is called "Johnny"';
You can also use escape characters to include quotes in a string:
Example
var x = 'It\'s alright';
var y = "He is called \"Johnny\"";
String Length
You can calculate the length of a string using the built-in length property:
Example
var txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; var sln = txt.length;
Special Characters
In JavaScript, strings are written within single or double quotes.
Because of this, the following example cannot be parsed by JavaScript:
The string "We are the so-called " is truncated.
To solve this issue, you can use a backslash () to escape the double quotes within the "Vikings" string, like this:
The backslash is an escape character. It converts special characters into string characters:
The escape character () can be used to escape apostrophes, newlines, quotes, and other special characters.
The following table lists special characters that can be escaped within a string:
Code | Output |
---|---|
\' | Single quote |
\" | Double quote |
\ | Backslash |
\n | Newline |
\r | Carriage return |
\t | Tab (horizontal) |
\b | Backspace |
\f | Form feed |
Strings Can Be Objects
Typically, JavaScript strings are primitive values, created with characters: var firstName = "John"
However, you can also define a string as an object using the new keyword: var firstName = new String("John")
Example
var x = "John";
var y = new String("John");
typeof x; // Returns String
typeof y; // Returns Object
| | Do not create String objects. They slow down execution speed and may produce other side effects: | | --- | --- |
Example
var x = "John";
var y = new String("John");
(x === y); // Returns false because x is a string and y is an object
=== denotes absolute equality, meaning both the data type and value must be the same.
String Properties and Methods
Primitive values, such as "John", do not have properties or methods (because they are not objects).
However, primitive values can use JavaScript properties and methods, as JavaScript can treat primitive values as objects when executing methods and properties.
String methods will be introduced in the next chapter.
String Properties
Property | Description |
---|---|
constructor | Returns the function that created the string's prototype |
length | Returns the length of the string |
prototype | Allows you to add properties and methods to an object |
String Methods
For more method examples, see: JavaScript 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 the combined string |
fromCharCode() | Converts Unicode values to characters |
indexOf() | Returns the position of the first occurrence of a specified value in a string |
lastIndexOf() | Returns the position of the last occurrence of a specified value in a string |
localeCompare() | Compares two strings in the current locale |
match() | Searches a string for a match against a regular expression, and returns the matches |
replace() | Searches a string for a specified value, or a regular expression, and returns a new string where the specified values are replaced |
search() | Searches a string for a specified value, or regular expression, and returns the position of the match |
slice() | Extracts a part of a string and returns a new string |
split() | Splits a string into an array of substrings |
substr() | Extracts the characters from a string, beginning at a specified start position, and through the specified number of character |
substring() | Extracts the characters from a string between two specified indices |
toLocaleLowerCase() | Converts a string to lowercase letters, according to the host's locale |
toLocaleUpperCase() | Converts a string to uppercase letters, according to the host's 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 both ends of a string |
valueOf() | Returns the primitive value of a String object |