
JavaScript String Methods and Manipulation
Powerful Built-in Methods for Text Operations
Catalog
Click on this table of contents to jump to the corresponding section
String length
Result:
String charAt()
Result:
String at()
Result:
String charcodeAt()
Result:
JavaScript codePointAt()
Get code point value at the first position in a string:
JavaScript String concat()
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.
Extracting String Parts
There are 3 methods for extracting a part of a string:
- slice(start, end)
- substring(start, end)
- substr(start, length)
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.
JavaScript counts positions from zero. First position is 0, Second position is 1
Example: 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().
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.
The substr() method is removed (deprecated) in the latest JavaScript standard. Use substring() or slice() instead.
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.
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()
JavaScript String toLowerCase()
JavaScript String isWellFormed()
The isWellFormed() method returns true if a string is well formed.
Otherwise it returns false. A string is not well formed if it contains lone surrogates.
JavaScript String toWellFormed()
The String method toWellFormed() returns a new string where all "lone surrogates" are replaced with the Unicode replacement character (U+FFFD).
JavaScript String trim()
The trim() method removes whitespace from both sides of a string:
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.
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.
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.
Pad a string with "0" until it reaches the length 4:
Pad a string with "x" until it reaches the length 4:
The padStart() method is a string method. To pad a number, convert the number to a string first. See the example below.
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.
The padEnd() method is a string method. To pad a number, convert the number to a string first. See the example below.
JavaScript String repeat()
The repeat() method returns a string with a number of copies of a string. The repeat() method returns a new string. The repeat() method does not change the original string.
Lily and 4 people like this
Prev Post
Space The Final Frontier
Next Post
Telescopes 101
0 Comments
Leave a Reply
Recent Post

SMOTE for Imbalanced Classification with Python
12:28:00 16/05/2026

CLAHE Histogram Equalization with OpenCV: A Python Guide
12:06:00 16/05/2026

Histogram Equalization in Digital Image Processing
10:56:00 16/05/2026

Gaussian Noise Explained: What It Is and How It Works
10:36:00 16/05/2026

Python Image Blurring with OpenCV: Gaussian, Median & Bilateral Filter Guide
09:59:00 16/05/2026
Related Topics
Feeds
Don't miss what's next 👋
Enjoyed this content? Leave your email to get notified when we publish new insights, tutorials, and updates — no spam, ever.

