JavaScript Strings
Strings are a primitive data type. They are any grouping of characters (letters, spaces, numbers, or symbols) surrounded by single quotes ', double quotes " or backticks `.
Concatenation
In JavaScript, multiple strings can be concatenated or joined together using the + operator.
In the example below, multiple strings and variables containing string values have been concatenated. After the code block is run, the displayText variable will contain the concatenated string:
let service = 'credit card';let month = 'May 30th';let displayText = 'Your ' + service + ' bill is due on ' + month + '.';console.log(displayText);// Output: Your credit card bill is due on May 30th.
Interpolation
String interpolation is the process of evaluating string literals containing one or more placeholders (expressions, variables, etc). It can be performed using template literals (text ${expression} text), as in the following example:
let age = 7;// String concatenationconsole.log('Tommy is ' + age + ' years old.');// Output: Tommy is 7 years old.// String interpolationconsole.log(`Tommy is ${age} years old.`);// Output: Tommy is 7 years old.
Escaping Characters
On occasion, it may be necessary to include characters in a string that may have reserved meanings or to apply additional formatting. Characters, such as quotes, can be included by prepending a \. There are a number of predefined escape sequences such as \n or \t to add formatting such as a line break or tab respectively:
// Line breakconsole.log('Hello\nWorld');// Output:// Hello// World// Including quotesconsole.log('"Wayne\'s World"');// Output: "Wayne's World"
Codebyte Example
The following codebyte example demonstrates how the above concepts work:
Strings
- .at()
- Returns the character at a particular index in a string.
- .charAt()
- Returns a single character at the specified index of a string.
- .charCodeAt()
- Returns an integer representing the UTF-16 character code at the specified index in a string.
- .codePointAt()
- Returns the Unicode code point value at a specified index position in a string, including complex characters like emojis and symbols.
- .concat()
- Concatenates or combines strings together.
- .endsWith()
- Checks if a string ends with a specified substring, returning true if it does and false otherwise.
- .fromCodePoint()
- Returns a string created by using the specified sequence of Unicode code points.
- .includes()
- Returns true if a given value is included in a string.
- .indexOf()
- Searches a string for a given value and returns the first index if found.
- .length
- Calculates the total character count within a given string, including spaces and punctuation.
- .localeCompare()
- Compares two strings based on the current locale, returning a number that indicates their sort order.
- .match()
- Returns an array of matches by matching the string against a regular expression.
- .normalize()
- Returns the Unicode Normalization Form of a string.
- .padEnd()
- Pads a string with a given string until the resulting string reaches the specified length.
- .padStart()
- Generates a new string of a specified length by adding a given padding string to the beginning of the original string.
- .repeat()
- Repeats a string a specified number of times. String will be concatenated.
- .replace()
- Searches a string for a string value, or a regular expression, and returns a new string where some or all matches are replaced.
- .replaceAll()
- Returns a new string by replacing all the matches of a given search value with a given replacement value.
- .search()
- Takes a regular expression argument and returns either the character position of the start of the first matching substring or -1 if there is no match.
- .slice()
- Returns a selected portion of a string.
- .split()
- Returns a new array of substrings based on a given string.
- .startsWith()
- Checks whether a string begins with the specified characters. It returns true if it does, otherwise false.
- .substr()
- Extracts a portion of a string beginning at a specified position and continues for a specified number of characters.
- .substring()
- Returns a part of a string from a given starting index or between the start and end index. The index starts at zero.
- .toLowerCase()
- Convert a string to lowercase letters.
- .toString()
- Returns a string representing the object.
- .toUpperCase()
- Converts the lowercase letters of a string to uppercase.
- .trim()
- Removes existing whitespace from both ends of a string.
- .trimEnd()
- Removes whitespace characters from the end of a string.
- .trimStart()
- Removes whitespace characters from the start (left side) of a string.
- .valueOf()
- Returns the value of a String object as a string.
- fromCharCode()
- Returns a string created from the specified sequence of UTF-16 code units.
- matchAll()
- Returns an iterator of all results matching a regular expression in a string, including capturing groups.
- raw()
- Returns the raw string form of template literals without processing escape sequences.
All contributors
BrandonDusch
EugeneGoh_
christian.dinh- Anonymous contributor
- Anonymous contributor
CaupolicanDiaz
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn JavaScript on Codecademy
- Learn how to build back-end web APIs using Express.js, Node.js, SQL, and a Node.js-SQLite database library.
- Includes 8 Courses
- With Certificate
- Beginner Friendly.30 hours
- Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.
- Beginner Friendly.15 hours