JavaScript String Methods

String methods help you to work with strings.
Primitive values, like "John Doe", cannot have properties 
or methods (because they are not objects). 
But with JavaScript, methods and properties are also available to 
primitive values, because JavaScript treats primitive values 
as objects when executing methods and properties.
The length property returns the length of a string:
There are 3 methods for extracting a part of a string:
slice(start, end)
substring(start, end)
substr(start, length)
slice() extracts a part of a string and returns the 
extracted part in a new string.
The method takes 2 parameters: the start position, and the end position (end 
not included).
This example slices out a portion of a string from position 7 to position 12 (13-1):
JavaScript counts positions from zero.
First position is 0.
Second position is 1.
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: 
If you omit the second parameter, the method will slice out the rest of the string:
or, counting from the end:
substring() is similar to slice().
The difference is that substring() cannot accept negative indexes.
If you omit the second parameter, substring() will slice out the rest of the 
string.
substr() is similar to slice().
The difference is 
that the second parameter specifies the length 
of the extracted part.
If you omit the second parameter, substr() will slice out the rest of the 
string.
If the first parameter is negative, the position counts from the end of the 
string.
The replace() method replaces a specified value with another 
value in a string:
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:

Try it Yourself »

By default, the replace() method is case sensitive. Writing MICROSOFT (with 
upper-case) will not work:

Try it Yourself »

To replace case insensitive, use a regular expression with an /i flag (insensitive):

Try it Yourself »

Regular expressions are written without quotes.
To replace all matches, use a regular expression with a /g flag (global match):

Try it Yourself »

You will learn a lot more about regular expressions in the chapter JavaScript Regular 
Expressions.
A string is converted to upper case with toUpperCase():
A string is converted to lower case with toLowerCase():
concat() joins two or more strings:
The concat() method can be used instead of the plus operator. 
These two lines do the same:
All string methods return a new string. They don't modify the original string.
Formally said:
Strings are immutable: Strings cannot be changed, only replaced.
The trim() method removes whitespace from both sides of a string:
ECMAScript 2017 added two String methods: padStart()
and padEnd() to support padding at the beginning and at the end of a string.
The padStart() method pads a string with another string:
The padStart() method is a string method.
To pad a number, convert the number to a string first.
See the example below.
padStart() is an ECMAScript 2017 feature.
It is supported in all modern browsers:
padStart() is not supported in Internet Explorer.
The padEnd() method pads a string with another string:
The padEnd() method is a string method.
To pad a number, convert the number to a string first.
See the example below.
padEnd() is an ECMAScript 2017 feature.
It is supported in all modern browsers:
padEnd() is not supported in Internet Explorer.
There are 3 methods for extracting string characters:
charAt(position)
charCodeAt(position)
Property access [ ]
The charAt() method returns the character at a specified 
index (position) in a string:
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).
ECMAScript 5 (2009) allows property access [ ] on strings:
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!)
If you want to work with a string as an array, you can convert it to an array.
A string can be converted to an array with the split() method:
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:
For a complete String reference, go to our:
Complete JavaScript String Reference.
The reference contains descriptions and examples of all string properties and methods.
Convert the text into an UPPERCASE text:
let txt = "Hello World!";
txt = txt.;

Start the Exercise
We just launchedW3Schools videos
Get certifiedby completinga course today!
If you want to report an error, or if you want to make a suggestion, do not hesitate to send us an e-mail:
help@w3schools.com
Your message has been sent to W3Schools.