Top JavaScript String Functions with example

Top JavaScript String Functions with example

list of 45 JavaScript string functions with examples :

### 1. `charAt()`
Returns the character at a specified index.
```javascript
let str = "Hello";
console.log(str.charAt(0)); // "H"
```

### 2. `charCodeAt()`
Returns the Unicode of the character at a specified index.
```javascript
let str = "Hello";
console.log(str.charCodeAt(0)); // 72
```

### 3. `concat()`
Joins two or more strings.
```javascript
let str1 = "Hello";
let str2 = " World";
console.log(str1.concat(str2)); // "Hello World"
```

### 4. `includes()`
Checks if a string contains a specified substring.
```javascript
let str = "Hello World";
console.log(str.includes("World")); // true
```

### 5. `endsWith()`
Checks if a string ends with a specified substring.
```javascript
let str = "Hello World";
console.log(str.endsWith("World")); // true
```

### 6. `indexOf()`
Returns the index of the first occurrence of a specified value.
```javascript
let str = "Hello World";
console.log(str.indexOf("o")); // 4
```

### 7. `lastIndexOf()`
Returns the index of the last occurrence of a specified value.
```javascript
let str = "Hello World";
console.log(str.lastIndexOf("o")); // 7
```

### 8. `match()`
Searches a string for a match against a regular expression.
```javascript
let str = "Hello World";
console.log(str.match(/o/g)); // ["o", "o"]
```

### 9. `replace()`
Replaces a specified value with another value in a string.
```javascript
let str = "Hello World";
console.log(str.replace("World", "JavaScript")); // "Hello JavaScript"
```

### 10. `search()`
Searches a string for a match against a regular expression and returns the index.
```javascript
let str = "Hello World";
console.log(str.search("World")); // 6
```

### 11. `slice()`
Extracts a part of a string and returns it as a new string.
```javascript
let str = "Hello World";
console.log(str.slice(0, 5)); // "Hello"
```

### 12. `split()`
Splits a string into an array of substrings.
```javascript
let str = "Hello World";
console.log(str.split(" ")); // ["Hello", "World"]
```

### 13. `startsWith()`
Checks if a string starts with a specified substring.
```javascript
let str = "Hello World";
console.log(str.startsWith("Hello")); // true
```

### 14. `toLowerCase()`
Converts a string to lowercase letters.
```javascript
let str = "Hello World";
console.log(str.toLowerCase()); // "hello world"
```

### 15. `toUpperCase()`
Converts a string to uppercase letters.
```javascript
let str = "Hello World";
console.log(str.toUpperCase()); // "HELLO WORLD"
```

### 16. `trim()`
Removes whitespace from both ends of a string.
```javascript
let str = "   Hello World   ";
console.log(str.trim()); // "Hello World"
```

### 17. `substring()`
Returns a part of the string between two specified indices.
```javascript
let str = "Hello World";
console.log(str.substring(0, 5)); // "Hello"
```

### 18. `toString()`
Returns the string value of an object.
```javascript
let num = 123;
console.log(num.toString()); // "123"
```

### 19. `valueOf()`
Returns the primitive value of a string object.
```javascript
let str = new String("Hello");
console.log(str.valueOf()); // "Hello"
```

### 20. `localeCompare()`
Compares two strings in the current locale.
```javascript
let str1 = "apple";
let str2 = "banana";
console.log(str1.localeCompare(str2)); // -1
```

### 21. `repeat()`
Returns a new string with a specified number of copies of the original string.
```javascript
let str = "Hello";
console.log(str.repeat(3)); // "HelloHelloHello"
```

### 22. `fromCharCode()`
Returns a string created from the specified sequence of UTF-16 code units.
```javascript
console.log(String.fromCharCode(72, 101, 108, 108, 111)); // "Hello"
```

### 23. `padStart()`
Pads the current string with another string (repeated, if needed) until the resulting string reaches the given length.
```javascript
let str = "5";
console.log(str.padStart(2, "0")); // "05"
```

### 24. `padEnd()`
Pads the current string with another string until the resulting string reaches the given length.
```javascript
let str = "5";
console.log(str.padEnd(2, "0")); // "50"
```

### 25. `trimStart()`
Removes whitespace from the beginning of a string.
```javascript
let str = "   Hello";
console.log(str.trimStart()); // "Hello"
```

### 26. `trimEnd()`
Removes whitespace from the end of a string.
```javascript
let str = "Hello   ";
console.log(str.trimEnd()); // "Hello"
```

### 27. `toLocaleLowerCase()`
Converts a string to lowercase letters, according to any locale.
```javascript
let str = "HELLO";
console.log(str.toLocaleLowerCase('tr')); // "hëllo" (Turkish locale)
```

### 28. `toLocaleUpperCase()`
Converts a string to uppercase letters, according to any locale.
```javascript
let str = "hello";
console.log(str.toLocaleUpperCase('tr')); // "HËLLO" (Turkish locale)
```

### 29. `codePointAt()`
Returns a non-negative integer that is the Unicode code point value of the character at the given position.
```javascript
let str = "𠜎";
console.log(str.codePointAt(0)); // 134071
```

### 30. `at()`
Returns the character at a specified index, allowing for negative indexing.
```javascript
let str = "Hello";
console.log(str.at(-1)); // "o"
```

### 31. `includes()`
Checks if a string contains a specified substring.
```javascript
let str = "Hello, world!";
console.log(str.includes("world")); // true
```

### 32. `replaceAll()`
Returns a new string with all matches of a pattern replaced by a replacement.
```javascript
let str = "Hello World";
console.log(str.replaceAll("o", "0")); // "Hell0 W0rld"
```

### 33. `normalize()`
Returns the Unicode Normalization Form of the string.
```javascript
let str = "Café";
console.log(str.normalize("NFC")); // "Café"
```

### 34. `split()`
Divides a string into an array of substrings.
```javascript
let str = "a,b,c";
console.log(str.split(",")); // ["a", "b", "c"]
```

### 35. `String.raw()`
A tag function for template literals.
```javascript
console.log(String.raw`Hello\nWorld`); // "Hello\nWorld"
```

### 36. `toString()`
Returns the string value of an object.
```javascript
let str = new String("Hello");
console.log(str.toString()); // "Hello"
```

### 37. `search()`
Searches a string for a match against a regular expression and returns the index.
```javascript
let str = "Hello World";
console.log(str.search(/o/)); // 4
```

### 38. `lastIndexOf()`
Returns the index of the last occurrence of a specified value.
```javascript
let str = "Hello World";
console.log(str.lastIndexOf("o")); // 7
```

### 39. `replace()`
Replaces the first occurrence of a substring.
```javascript
let str = "Hello World";
console.log(str.replace("World", "there")); // "Hello there"
```

### 40. `startsWith()`
Checks if a string starts with a specified substring.
```javascript
let str = "Hello";
console.log(str.startsWith("He")); // true
```

### 41. `endsWith()`
Checks if a string ends with a specified substring.
```javascript
let str = "Hello";
console.log(str.endsWith("lo")); // true
```

### 42. `split()`
Splits a string into an array of substrings.
```javascript
let str = "Hello World";
console.log(str.split(" ")); // ["Hello", "World"]
```

### 43. `concat()`
Combines two or more strings.
```javascript
let str1 = "Hello";
let str2 = " World";
console.log(str1.concat(str2)); // "Hello World"
```

### 44. `indexOf()`
Returns the index of the first occurrence of a specified value.
```javascript
let str = "Hello World";
console.log(str.indexOf("o")); // 4
```

### 45. `length`
Returns the length of a string.

Related Posts

What is Redux ? How it Work ?

What is Redux ? How it Work ?

Understanding Redux: A Comprehensive Overview Redux is a predictable state management library often used in JavaScript applications, particularly with React. It was created to manage the state…

JavaScript Class Object Questions and Answers

JavaScript Class Object Questions and Answers

JavaScript Interview Questions with Answers for Fresher

JavaScript Interview Questions with Answers for Fresher

Here’s a list of 50 common JavaScript questions along with their answers to help you get started or refresh your knowledge: 1. What is JavaScript? JavaScript is…

Leave a Reply

Your email address will not be published. Required fields are marked *