JavaScript String Methods
JavaScript String Methods
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C
let text = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
let length = text.length;
Try it Yourself »
Example
let text = "HELLO WORLD";
let char = text.charAt(0);
Try it Yourself »
The method returns a UTF-16 code (an integer between 0 and 65535).
Tutorials
Example Exercises Services Sign Up Log in
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C
let text = "HELLO WORLD";
let char = text.charCodeAt(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
The at() method is a new addition to JavaScript.
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C
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:
Example
let text = "HELLO WORLD";
Tutorials
text[0] = "A";
Exercises Services
// Gives no error, but does not work
Sign Up Log in
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C
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.
First position is 0.
Second position is 1.
Tutorials
Examples Exercises Services Sign Up Log in
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C
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 »
The difference is that start and end values less than 0 are treated as 0 in substring() .
Example
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C
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 = str.substr(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 = str.substr(7);
Try it Yourself »
If the first parameter is negative, the position counts from the end of the string.
Example
let str = "Apple, Banana, Kiwi";
let part = str.substr(-4);
Try it Yourself
Tutorials
» Exercises Services Sign Up Log in
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C
Example
let text1 = "Hello World!";
let text2 = text1.toUpperCase();
Try it Yourself »
Example
let text1 = "Hello World!"; // String
let text2 = text1.toLowerCase(); // text2 is text1 converted to lower
Try it Yourself »
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C
let text1 = "Hello";
let text2 = "World";
let text3 = text1.concat(" ", text2);
Try it Yourself »
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.
Formally said:
Example
let text1 = " Hello World! ";
let text2 = text1.trim();
Try it Yourself »
Tutorials Exercises Services Sign Up Log in
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C
The trimStart() method works like trim() , but removes whitespace only from the start of a string.
Example
Try it Yourself »
JavaScript String trimStart() is supported in all modern browsers since January 2020:
Apr 2018 Jan 2020 Jun 2018 Sep 2018 May 2018
The trimEnd() method works like trim() , but removes whitespace only from the end of a string.
Example
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C
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:
Try it Yourself »
Try it Yourself »
Tutorials Exercises Services Sign Up Log in
NoteCSS
HTML JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C
The padStart() method is a string method.
Example
let numb = 5;
let text = numb.toString();
let padded = text.padStart(4,"0");
Try it Yourself »
Browser Support
padStart() is an ECMAScript 2017 feature.
Apr 2017 Apr 2017 Mar 2017 Sep 2017 May 2017
It pads a string with another string (multiple times) until it reaches a given length.
Tutorials
Examples Exercises Services Sign Up Log in
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C
let text = "5";
let padded = text.padEnd(4,"0");
Try it Yourself »
Try it Yourself »
Note
The padEnd() method is a string method.
Example
let numb = 5;
let text = numb.toString();
let padded = text.padEnd(4,"0");
Try it Yourself »
Browser Support
padEnd() is an ECMAScript 2017 feature.
Examples
Create copies of a text:
Try it Yourself »
Try it Yourself »
Syntax
string.repeat(count)
Parameters
Parameter Description
count Tutorials Required.
Exercises Services
The number of copies wanted.
Sign Up Log in
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C
Return Value
Type Description
Browser Support
repeat() is an ES6 feature (JavaScript 2015).
May 2016 Apr 2017 Jun 2017 Sep 2016 Jun 2016
Example
let text = "Please visit Microsoft!";
let newText = text.replace("Microsoft", "W3Schools");
Try it Yourself »
NoteTutorials
Exercises Services Sign Up Log in
If you want to replace all matches, use a regular expression with the /g flag set. See examples below.
Example
let text = "Please visit Microsoft and Microsoft!";
let newText = text.replace("Microsoft", "W3Schools");
Try it Yourself »
By default, the replace() method is case sensitive. Writing MICROSOFT (with upper-case) will not work:
Example
let text = "Please visit Microsoft!";
let newText = text.replace("MICROSOFT", "W3Schools");
Try it Yourself »
Example
let text = "Please visit Microsoft!";
let newText = text.replace(/MICROSOFT/i, "W3Schools");
Try it Yourself »
Tutorials Exercises Services Sign Up Log in
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C
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");
Try it Yourself »
Note
You will learn a lot more about regular expressions in the chapter JavaScript Regular Expressions.
Example
text = text.replaceAll("Cats","Dogs");
text = text.replaceAll("cats","dogs");
Try it Yourself »
The replaceAll() method allows you to specify a regular expression instead of a string to be replaced.
Tutorials Exercises Services Sign Up Log in
If the parameter is a regular expression, the global flag (g) must be set, otherwise a TypeError is thrown.
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C
Example
text = text.replaceAll(/Cats/g,"Dogs");
text = text.replaceAll(/cats/g,"dogs");
Try it Yourself »
Note
replaceAll() is an ES2021 feature.
If you want to work with a string as an array, you can convert it to an array.
Example
text.split(",") // Split on commas
text.split(" ") // Split on spaces
text.split("|") // Split on pipe
Try it Yourself »
Tutorials Exercises Services Sign Up Log in
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C
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
text.split("")
Try it Yourself »
The reference contains descriptions and examples of all string properties and methods.
Exercise:
Convert the text into an UPPERCASE text:
Submit Answer »
Start the Exercise
Tutorials Exercises Services Sign Up Log in
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C
❮ Previous Next ❯
W3schools Pathfinder
Track your progress - it's free! Sign Up Log in
COLOR PICKER
How To Tutorial
Tutorials
SQL Tutorial
Exercises
Python Tutorial
Services SQL Reference
Python Reference
W3.CSS Reference
Sign Up Log in
HTML
CSS W3.CSS Tutorial
JAVASCRIPT SQL PYTHON Bootstrap
JAVA Reference
PHP HOW TO W3.CSS C C++ C
Bootstrap Tutorial PHP Reference
PHP Tutorial HTML Colors
Java Tutorial Java Reference
C++ Tutorial Angular Reference
jQuery Tutorial jQuery Reference
Copyright 1999-2024 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.