Loading...

JavaScript String Methods

String Methods in JavaScript

JavaScript provides many methods for working with strings. Since strings are immutable (they cannot be changed once created), these methods return new strings without altering the original one.

Basic String Methods

1. String Length

The .length property returns the number of characters in a string. This is useful for checking the length of user input.

javascript
2 lines
|
17/ 500 tokens
1
2
let text = "Hello, World!";
let length = text.length; // Outputs: 13
Code Tools

2. charAt()

The charAt() method returns the character at a specified position in a string.

javascript
2 lines
|
18/ 500 tokens
1
2
let text = "JavaScript";
let character = text.charAt(2); // Outputs: "v"
Code Tools

3. charCodeAt()

The charCodeAt() method returns the Unicode of the character at a specified index in the string.

javascript
2 lines
|
23/ 500 tokens
1
2
let text = "JavaScript";
let unicode = text.charCodeAt(2); // Outputs: 118 (Unicode for "v")
Code Tools

4. at()

The at() method returns the character at a specified index. It works similarly to charAt().

javascript
2 lines
|
17/ 500 tokens
1
2
let text = "JavaScript";
let character = text.at(3); // Outputs: "a"
Code Tools

5. Accessing Characters Using []

You can access characters in a string like an array by using square brackets.

javascript
2 lines
|
17/ 500 tokens
1
2
let text = "JavaScript";
let character = text[4]; // Outputs: "S"
Code Tools

6. slice()

The slice() method extracts a section of a string and returns it as a new string. You can specify the start and end positions.

javascript
2 lines
|
22/ 500 tokens
1
2
let text = "JavaScript is fun";
let part = text.slice(0, 10); // Outputs: "JavaScript"
Code Tools

7. substring()

The substring() method works like slice() but does not accept negative indexes.

javascript
2 lines
|
23/ 500 tokens
1
2
let text = "JavaScript is fun";
let part = text.substring(0, 10); // Outputs: "JavaScript"
Code Tools

8. substr()

The substr() method extracts a specified number of characters from a starting position.

javascript
2 lines
|
22/ 500 tokens
1
2
let text = "JavaScript is fun";
let part = text.substr(0, 10); // Outputs: "JavaScript"
Code Tools

9. toUpperCase()

The toUpperCase() method converts all characters in a string to uppercase.

javascript
2 lines
|
22/ 500 tokens
1
2
let text = "hello world";
let upperText = text.toUpperCase(); // Outputs: "HELLO WORLD"
Code Tools

10. toLowerCase()

The toLowerCase() method converts all characters in a string to lowercase.

javascript
2 lines
|
22/ 500 tokens
1
2
let text = "HELLO WORLD";
let lowerText = text.toLowerCase(); // Outputs: "hello world"
Code Tools

11. concat()

The concat() method joins two or more strings and returns a new string.

javascript
3 lines
|
27/ 500 tokens
1
2
3
let text1 = "Hello";
let text2 = "World";
let result = text1.concat(" ", text2); // Outputs: "Hello World"
Code Tools

12. trim()

The trim() method removes whitespace from both sides of a string.

javascript
2 lines
|
22/ 500 tokens
1
2
let text = "   Hello World   ";
let trimmedText = text.trim(); // Outputs: "Hello World"
Code Tools

13. trimStart()

The trimStart() method removes whitespace only from the beginning of a string.

javascript
2 lines
|
26/ 500 tokens
1
2
let text = "   Hello World   ";
let trimmedStartText = text.trimStart(); // Outputs: "Hello World   "
Code Tools

14. trimEnd()

The trimEnd() method removes whitespace only from the end of a string.

javascript
2 lines
|
25/ 500 tokens
1
2
let text = "   Hello World   ";
let trimmedEndText = text.trimEnd(); // Outputs: "   Hello World"
Code Tools

15. padStart()

The padStart() method pads the beginning of a string with a specified character to reach a desired length.

javascript
2 lines
|
19/ 500 tokens
1
2
let text = "5";
let paddedText = text.padStart(3, "0"); // Outputs: "005"
Code Tools

16. padEnd()

The padEnd() method pads the end of a string with a specified character to reach a desired length.

javascript
2 lines
|
18/ 500 tokens
1
2
let text = "5";
let paddedText = text.padEnd(3, "0"); // Outputs: "500"
Code Tools

17. repeat()

The repeat() method repeats a string a specified number of times.

javascript
2 lines
|
20/ 500 tokens
1
2
let text = "Hi! ";
let repeatedText = text.repeat(3); // Outputs: "Hi! Hi! Hi! "
Code Tools

18. replace()

The replace() method replaces a specified value in a string with a new value. It only replaces the first occurrence.

javascript
2 lines
|
28/ 500 tokens
1
2
let text = "Hello, World!";
let newText = text.replace("World", "JavaScript"); // Outputs: "Hello, JavaScript!"
Code Tools

19. replaceAll()

The replaceAll() method replaces all occurrences of a specified value in a string with a new value.

javascript
2 lines
|
31/ 500 tokens
1
2
let text = "Hello, World! Hello again!";
let newText = text.replaceAll("Hello", "Hi"); // Outputs: "Hi, World! Hi again!"
Code Tools

20. split()

The split() method splits a string into an array based on a specified separator.

javascript
2 lines
|
27/ 500 tokens
1
2
let text = "JavaScript,HTML,CSS";
let languages = text.split(","); // Outputs: ["JavaScript", "HTML", "CSS"]
Code Tools

Key Takeaways on JavaScript String Methods

  • Immutability: JavaScript strings are immutable, meaning methods create new strings rather than changing the original.
  • Length Property: .length provides the total number of characters in a string, useful for validating input length.
  • Character Access: Use charAt() or square brackets ([]) to access individual characters in a string.
  • String Extraction: Use slice(), substring(), or substr() to get parts of a string. slice() allows negative indexes, while substring() does not.
  • Case Conversion: toUpperCase() and toLowerCase() are useful for converting text to all uppercase or lowercase.
  • Whitespace Removal: trim(), trimStart(), and trimEnd() remove spaces from around or within strings, helpful for cleaning user input.
  • Concatenation: Use concat() or simply + to combine strings into one.
  • Padding: padStart() and padEnd() add padding to strings, useful for formatting text in fixed-width fields.
  • Replacing Text: replace() replaces the first match, while replaceAll() replaces all occurrences in the string.
  • Repeating Text: repeat() duplicates a string a set number of times.
  • Splitting Text: split() divides a string into an array based on a specified separator, ideal for breaking up text data.

Frequently Asked Questions

String methods in JavaScript are built-in functions that allow manipulation of string values. Common methods include length, toUpperCase(), toLowerCase(), slice(), replace(), and split(), helping modify and retrieve string data efficiently.

The String() method in JavaScript converts a given value into a string. It can transform numbers, booleans, objects, and other data types into a string format, making it useful for type conversion and text processing.

To create a custom string method in JavaScript, extend String.prototype with a function. For example: String.prototype.reverse = function() { return this.split('').reverse().join(''); } adds a reverse() method to all strings.

JavaScript methods are functions associated with objects that perform specific actions. They operate on the object’s data, such as push() for arrays or toUpperCase() for strings, enabling efficient data manipulation and processing.

Still have questions?Contact our support team