JavaScript String Methods
JavaScript String Methods
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO [Link] C
[Link] 1/25
15/01/2024 09:48 JavaScript String Methods
Tutorials
Example
Exercises Services Sign Up Log in
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO [Link] C
let text = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
let length = [Link];
Try it Yourself »
Example
let text = "HELLO WORLD";
let char = [Link](0);
Try it Yourself »
The method returns a UTF-16 code (an integer between 0 and 65535).
[Link] 2/25
15/01/2024 09:48 JavaScript String Methods
Tutorials
Example
Exercises Services Sign Up Log in
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO [Link] C
let text = "HELLO WORLD";
let char = [Link](0);
Try it Yourself »
Examples
Get the third letter of name:
Try it Yourself »
Try it Yourself »
The at() method returns the character at a specified index (position) in a string.
The at() method is supported in all modern browsers since March 2022:
Note
[Link] 3/25
15/01/2024 09:48 JavaScript String Methods
Browser Support
at() is an ES2022 feature.
JavaScript 2022 (ES2022) is supported in all modern browsers since March 2023:
Sep 2021 Sep 2021 Oct 2021 Mar 2023 Oct 2021
Property Access [ ]
Example
let text = "HELLO WORLD";
let char = text[0];
Try it Yourself »
Note
Property access might be a little unpredictable:
[Link] 4/25
15/01/2024 09:48 JavaScript String Methods
Tutorials
Example
Exercises Services Sign Up Log in
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO [Link] C
let text = "HELLO WORLD";
text[0] = "A"; // Gives no error, but does not work
Try it Yourself »
slice(start, end)
substring(start, end)
substr(start, length)
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:
Try it Yourself »
Note
JavaScript counts positions from zero.
[Link] 5/25
15/01/2024 09:48 JavaScript String Methods
First position is 0.
Tutorials
Second position is 1.
Exercises Services Sign Up Log in
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO [Link] C
Examples
If you omit the second parameter, the method will slice out the rest of the string:
Try it Yourself »
If a parameter is negative, the position is counted from the end of the string:
Try it Yourself »
This example slices out a portion of a string from position -12 to position -6:
Try it Yourself »
ADVERTISEMENT
[Link] 6/25
15/01/2024 09:48 JavaScript String Methods
The difference is that start and end values less than 0 are treated as 0 in
Tutorials
substring() . Exercises Services Sign Up Log in
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO [Link] C
Example
Try it Yourself »
If you omit the second parameter, substring() will slice out the rest of the string.
The difference is that the second parameter specifies the length of the extracted
part.
Example
let str = "Apple, Banana, Kiwi";
let part = [Link](7, 6);
Try it Yourself »
If you omit the second parameter, substr() will slice out the rest of the string.
Example
let str = "Apple, Banana, Kiwi";
let part = [Link](7);
[Link] 7/25
15/01/2024 09:48 JavaScript String Methods
Try it Yourself
Tutorials
» Exercises Services Sign Up Log in
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO [Link] C
If the first parameter is negative, the position counts from the end of the string.
Example
let str = "Apple, Banana, Kiwi";
let part = [Link](-4);
Try it Yourself »
Example
let text1 = "Hello World!";
let text2 = [Link]();
Try it Yourself »
Example
[Link] 8/25
15/01/2024 09:48 JavaScript String Methods
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO [Link] C
Try it Yourself »
Example
Try it Yourself »
The concat() method can be used instead of the plus operator. These two lines do
the same:
Example
text = "Hello" + " " + "World!";
text = "Hello".concat(" ", "World!");
Note
All string methods return a new string. They don't modify the original string.
Formally said:
[Link] 9/25
15/01/2024 09:48 JavaScript String Methods
JavaScript
Tutorials String
Exercises trim()
Services Sign Up Log in
Example
let text1 = " Hello World! ";
let text2 = [Link]();
Try it Yourself »
The trimStart() method works like trim() , but removes whitespace only from the
start of a string.
Example
Try it Yourself »
Apr 2018 Jan 2020 Jun 2018 Sep 2018 May 2018
[Link] 10/25
15/01/2024 09:48 JavaScript String Methods
JavaScript
Tutorials String
Exercises trimEnd()
Services Sign Up Log in
The trimEnd() method works like trim() , but removes whitespace only from the
end of a string.
Example
Try it Yourself »
JavaScript String trimEnd() is supported in all modern browsers since January 2020:
Apr 2018 Jan 2020 Jun 2018 Sep 2018 May 2018
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:
[Link] 11/25
15/01/2024 09:48 JavaScript String Methods
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO [Link] C
Try it Yourself »
Try it Yourself »
Note
The padStart() method is a string method.
Example
let numb = 5;
let text = [Link]();
let padded = [Link](4,"0");
Try it Yourself »
Browser Support
padStart() is an ECMAScript 2017 feature.
[Link] 12/25
15/01/2024 09:48 JavaScript String Methods
Chrome
Tutorials
58 Exercises
Edge 15 Services 52
Firefox
Safari 11 SignOpera
Up Log in
45
HTML
Apr 2017 JAVASCRIPT
CSS Apr 2017SQL Mar 2017
PYTHON JAVA SepPHP
2017 HOW TO
May 2017
[Link] C
It pads a string with another string (multiple times) until it reaches a given length.
Examples
let text = "5";
let padded = [Link](4,"0");
Try it Yourself »
Try it Yourself »
Note
The padEnd() method is a string method.
Example
[Link] 13/25
15/01/2024 09:48 JavaScript String Methods
let numb = 5;
Tutorials Exercises
let text = [Link]();
Services Sign Up Log in
Try it Yourself »
Browser Support
padEnd() is an ECMAScript 2017 feature.
Apr 2017 Apr 2017 Mar 2017 Sep 2017 May 2017
Examples
Create copies of a text:
Try it Yourself »
[Link] 14/25
15/01/2024 09:48 JavaScript String Methods
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO [Link] C
Try it Yourself »
Syntax
[Link](count)
Parameters
Parameter Description
count Required.
The number of copies wanted.
Return Value
Type Description
Browser Support
repeat() is an ES6 feature (JavaScript 2015).
May 2016 Apr 2017 Jun 2017 Sep 2016 Jun 2016
[Link] 15/25
15/01/2024 09:48 JavaScript String Methods
Replacing
Tutorials String
Exercises Content
Services Sign Up Log in
HTML CSS
The replace()JAVASCRIPT SQLa specified
method replaces PYTHONvalueJAVA PHP value
with another HOWinTO [Link]
a string: C
Example
let text = "Please visit Microsoft!";
let newText = [Link]("Microsoft", "W3Schools");
Try it Yourself »
Note
The replace() method does not change the string it is called on.
If you want to replace all matches, use a regular expression with the /g flag set. See
examples below.
Example
Try it Yourself »
By default, the replace() method is case sensitive. Writing MICROSOFT (with upper-
case) will not work:
[Link] 16/25
15/01/2024 09:48 JavaScript String Methods
Tutorials
Example
Exercises Services Sign Up Log in
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO [Link] C
let text = "Please visit Microsoft!";
let newText = [Link]("MICROSOFT", "W3Schools");
Try it Yourself »
Example
let text = "Please visit Microsoft!";
let newText = [Link](/MICROSOFT/i, "W3Schools");
Try it Yourself »
Note
Regular expressions are written without quotes.
To replace all matches, use a regular expression with a /g flag (global match):
Example
Try it Yourself »
Note
[Link] 17/25
15/01/2024 09:48 JavaScript String Methods
You will learn a lot more about regular expressions in the chapter JavaScript Regular
Tutorials
Expressions. Exercises Services Sign Up Log in
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO [Link] C
Example
text = [Link]("Cats","Dogs");
text = [Link]("cats","dogs");
Try it Yourself »
If the parameter is a regular expression, the global flag (g) must be set, otherwise a
TypeError is thrown.
Example
text = [Link](/Cats/g,"Dogs");
text = [Link](/cats/g,"dogs");
Try it Yourself »
Note
replaceAll() is an ES2021 feature.
[Link] 18/25
15/01/2024 09:48 JavaScript String Methods
Converting
Tutorials a String
Exercises to an
Services Array
Sign Up Log in
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO [Link] C
If you want to work with a string as an array, you can convert it to an array.
Example
Try it Yourself »
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
[Link]("")
Try it Yourself »
[Link] 19/25
15/01/2024 09:48 JavaScript String Methods
The reference contains descriptions and examples of all string properties and
[Link] Exercises Services Sign Up Log in
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO [Link] C
Exercise:
Convert the text into an UPPERCASE text:
Submit Answer »
ADVERTISEMENT
[Link] 20/25
15/01/2024 09:48 JavaScript String Methods
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO [Link] C
COLOR PICKER
[Link] 21/25
15/01/2024 09:48 JavaScript String Methods
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO [Link] C
ADVERTISEMENT
ADVERTISEMENT
[Link] 22/25
15/01/2024 09:48 JavaScript String Methods
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO [Link] C
ADVERTISEMENT
REPORT ERROR
Top Tutorials
HTML Tutorial
CSS Tutorial
JavaScript Tutorial
How To Tutorial
SQL Tutorial
[Link] 23/25
15/01/2024 09:48 JavaScript String Methods
Python Tutorial
[Link] Tutorial
Tutorials Exercises
Bootstrap Tutorial
PHP Tutorial
Services Sign Up Log in
HTML
CSS JavaJAVASCRIPT
Tutorial SQL PYTHON JAVA PHP HOW TO [Link] C
C++ Tutorial
jQuery Tutorial
Top References
HTML Reference
CSS Reference
JavaScript Reference
SQL Reference
Python Reference
[Link] Reference
Bootstrap Reference
PHP Reference
HTML Colors
Java Reference
Angular Reference
jQuery Reference
FORUM ABOUT
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.
[Link] 24/25
15/01/2024 09:48 JavaScript String Methods
[Link].
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO [Link] C
[Link] 25/25