- JavaScript String Methods
- JavaScript String Length
- Example
- Extracting String Parts
- JavaScript String slice()
- Example
- Note
- Examples
- JavaScript String substring()
- Example
- JavaScript String substr()
- Example
- Example
- Example
- Replacing String Content
- Example
- Note
- Example
- Example
- Example
- Note
- Example
- Note
- JavaScript String ReplaceAll()
- Example
- Example
- Note
- Converting to Upper and Lower Case
- JavaScript String toUpperCase()
- Example
- JavaScript String toLowerCase()
- Example
- JavaScript String concat()
- Example
- Example
- Note
- JavaScript String trim()
- Example
- JavaScript String trimStart()
- Example
- JavaScript String trimEnd()
- Example
- JavaScript String Padding
- JavaScript String padStart()
- Examples
- Note
- Example
- Browser Support
- JavaScript String padEnd()
- Examples
- Note
- Example
- Browser Support
- Extracting String Characters
- JavaScript String charAt()
- Example
- JavaScript String charCodeAt()
- Example
- Property Access
- Example
- Note
- Example
- Converting a String to an Array
- JavaScript String split()
- Example
- Example
- Complete String Reference
- JavaScript String length
- COLOR PICKER
- Report Error
- Thank You For Helping Us!
JavaScript String Methods
String search methods are covered in the next chapter.
JavaScript String Length
The length property returns the length of a string:
Example
Extracting String Parts
There are 3 methods for extracting a part of a string:
JavaScript String slice()
slice() extracts a part of a string and returns the extracted part in a new string.
The method takes 2 parameters: start position, and end position (end not included).
Example
Slice out a portion of a string from position 7 to position 13:
Note
JavaScript counts positions from zero.
Examples
If you omit the second parameter, the method will slice out the rest of the string:
If a parameter is negative, the position is counted from the end of the string:
This example slices out a portion of a string from position -12 to position -6:
JavaScript String substring()
substring() is similar to slice() .
The difference is that start and end values less than 0 are treated as 0 in substring() .
Example
If you omit the second parameter, substring() will slice out the rest of the string.
JavaScript String substr()
substr() is similar to slice() .
The difference is that the second parameter specifies the length of the extracted part.
Example
If you omit the second parameter, substr() will slice out the rest of the string.
Example
If the first parameter is negative, the position counts from the end of the string.
Example
Replacing String Content
The replace() method replaces a specified value with another value in a string:
Example
Note
The replace() method does not change the string it is called on.
The replace() method returns a new string.
The replace() method replaces only the first match
If you want to replace all matches, use a regular expression with the /g flag set. See examples below.
By default, the replace() method replaces only the first match:
Example
let text = «Please visit Microsoft and Microsoft!»;
let newText = text.replace(«Microsoft», «W3Schools»);
By default, the replace() method is case sensitive. Writing MICROSOFT (with upper-case) will not work:
Example
To replace case insensitive, use a regular expression with an /i flag (insensitive):
Example
Note
Regular expressions are written without quotes.
To replace all matches, use a regular expression with a /g flag (global match):
Example
let text = «Please visit Microsoft and Microsoft!»;
let newText = text.replace(/Microsoft/g, «W3Schools»);
Note
You will learn a lot more about regular expressions in the chapter JavaScript Regular Expressions.
JavaScript String ReplaceAll()
In 2021, JavaScript introduced the string method replaceAll() :
Example
The replaceAll() method allows you to specify a regular expression instead of a string to be replaced.
If the parameter is a regular expression, the global flag (g) must be set, otherwise a TypeError is thrown.
Example
Note
replaceAll() is an ES2021 feature.
replaceAll() does not work in Internet Explorer.
Converting to Upper and Lower Case
A string is converted to upper case with toUpperCase() :
A string is converted to lower case with toLowerCase() :
JavaScript String toUpperCase()
Example
JavaScript String toLowerCase()
Example
let text1 = «Hello World!»; // String
let text2 = text1.toLowerCase(); // text2 is text1 converted to lower
JavaScript String concat()
concat() joins two or more strings:
Example
The concat() method can be used instead of the plus operator. These two lines do the same:
Example
Note
All string methods return a new string. They don’t modify the original string.
Strings are immutable: Strings cannot be changed, only replaced.
JavaScript String trim()
The trim() method removes whitespace from both sides of a string:
Example
JavaScript String trimStart()
ECMAScript 2019 added the String method trimStart() to JavaScript.
The trimStart() method works like trim() , but removes whitespace only from the start of a string.
Example
JavaScript String trimStart() is supported in all modern browsers since January 2020:
JavaScript String trimEnd()
ECMAScript 2019 added the string method trimEnd() to JavaScript.
The trimEnd() method works like trim() , but removes whitespace only from the end of a string.
Example
JavaScript String trimEnd() is supported in all modern browsers since January 2020:
JavaScript String Padding
ECMAScript 2017 added two new string methods to JavaScript: padStart() and padEnd() to support padding at the beginning and at the end of a string.
JavaScript String padStart()
The padStart() method pads a string from the start.
It pads a string with another string (multiple times) until it reaches a given length.
Examples
Pad a string with «0» until it reaches the length 4:
Pad a string with «x» until it reaches the length 4:
Note
The padStart() method is a string method.
To pad a number, convert the number to a string first.
Example
Browser Support
It is supported in all modern browsers:
padStart() is not supported in Internet Explorer.
JavaScript String padEnd()
The padEnd() method pads a string from the end.
It pads a string with another string (multiple times) until it reaches a given length.
Examples
Note
The padEnd() method is a string method.
To pad a number, convert the number to a string first.
Example
Browser Support
It is supported in all modern browsers:
padEnd() is not supported in Internet Explorer.
Extracting String Characters
There are 3 methods for extracting string characters:
JavaScript String charAt()
The charAt() method returns the character at a specified index (position) in a string:
Example
JavaScript String charCodeAt()
The charCodeAt() method returns the unicode of the character at a specified index in a string:
The method returns a UTF-16 code (an integer between 0 and 65535).
Example
Property Access
ECMAScript 5 (2009) allows property access [ ] on strings:
Example
Note
Property access might be a little unpredictable:
- It makes strings look like arrays (but they are not)
- If no character is found, [ ] returns undefined, while charAt() returns an empty string.
- It is read only. str[0] = «A» gives no error (but does not work!)
Example
Converting a String to an Array
If you want to work with a string as an array, you can convert it to an array.
JavaScript String split()
A string can be converted to an array with the split() method:
Example
text.split(«,») // Split on commas
text.split(» «) // Split on spaces
text.split(«|») // Split on pipe
If the separator is omitted, the returned array will contain the whole string in index [0].
If the separator is «», the returned array will be an array of single characters:
Example
Complete String Reference
For a complete String reference, go to our:
The reference contains descriptions and examples of all string properties and methods.
JavaScript String length
ES1 (JavaScript 1997) is fully supported in all browsers:
Chrome | Edge | Firefox | Safari | Opera | IE |
Yes | Yes | Yes | Yes | Yes | Yes |
COLOR PICKER
Report Error
If you want to report an error, or if you want to make a suggestion, do not hesitate to send us an e-mail:
Thank You For Helping Us!
Your message has been sent to W3Schools.
Top Tutorials
Top References
Top Examples
Get Certified
W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.