29 String Methods in JavaScript
JavaScript string methods are built-in functions that can be called on a string to perform operations and manipulate the string. These methods allow developers to work with strings in a more efficient and intuitive way.
Further Js has 29 string methods. Let’s explore all of them.
charAt()
- Returns the character at the specified index in a string.
let str = "hello";
console.log(str.charAt(1)); // "e"
charCodeAt()
- Returns the Unicode value of the character at the specified index in a string.
let str = "hello";
console.log(str.charCodeAt(1)); // 101 (the Unicode value for "e")
concat()
- Joins two or more strings and returns a new string.
let str1 = "hello";
let str2 = "world";
console.log(str1.concat(" ", str2)); // "hello world"
endsWith()
- Returns a Boolean indicating whether the string ends with the specified character(s).
let str = "hello world";
console.log(str.endsWith("world")); // true
includes()
- Returns a Boolean indicating whether the string contains the specified character(s).
let str = "hello world";
console.log(str.includes("world")); // true
indexOf()
- Returns the index of the first occurrence of the specified character(s) in a string.
let str = "hello world";
console.log(str.indexOf("o")); // 4
lastIndexOf()
- Returns the index of the last occurrence of the specified character(s) in a string.
let str = "hello world";
console.log(str.lastIndexOf("o")); // 7
localeCompare()
- Compares two strings in the current locale and returns a number indicating their relative position.
let str1 = "apple";
let str2 = "banana";
console.log(str1.localeCompare(str2)); // -1 (because "apple" comes before "banana" in alphabetical order)
match()
- Searches a string for a match against a regular expression and returns an array of matches.
let str = "The quick brown fox jumps over the lazy dog.";
let regex = /[A-Z]/g;
console.log(str.match(regex)); // ["T", "B", "F"]
normalize()
- Returns a Unicode Normalization Form of a given string.
let str = "M\u0301xico";
console.log(str.normalize()); // "México"
padEnd()
- Pads the end of a string with a specified character until it reaches a specified length.
let str = "hello";
console.log(str.padEnd(10, ".")); // "hello....."
padStart()
- Pads the beginning of a string with a specified character until it reaches a specified length.
let str = "hello";
console.log(str.padStart(10, ".")); // ".....hello"
repeat()
- Returns a new string with a specified number of copies of an existing string.
let str = "hello";
console.log(str.repeat(3)); // "hellohellohello"
replace()
- Searches a string for a specified value and returns a new string with the value replaced.
let str = "hello world";
console.log(str.replace("world", "mars")); // "hello mars"
search()
- Searches a string for a specified value and returns the position of the match.
const str = 'The quick brown fox jumps over the lazy dog.';
console.log(str.search('brown')); // 10
console.log(str.search(/cat/g)); // -1
slice()
- Extracts a section of a string and returns a new string.
const str = 'The quick brown fox jumps over the lazy dog.';
console.log(str.slice(4, 9)); // "quick"
split()
- Splits a string into an array of substrings.
const str = 'The quick brown fox jumps over the lazy dog.';
console.log(str.split(' ')); // ["The", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog."]
console.log(str.split('o')); // ["The quick br", "wn f", "x jumps ", "ver the lazy d", "g."]
startsWith()
- Returns a Boolean indicating whether the string starts with the specified character(s).
const str = 'Hello World!';
const startsWithHello = str.startsWith('Hello');
const startsWithWorld = str.startsWith('World');
console.log(startsWithHello); // Output: true
console.log(startsWithWorld); // Output: false
substr()
- Extracts a specified number of characters from a string, starting at a specified index.
const str = 'The quick brown fox jumps over the lazy dog.';
console.log(str.substr(4, 5)); // "quick"
substring()
- Extracts the characters between two specified indices in a string.
const str = 'The quick brown fox jumps over the lazy dog.';
console.log(str.substring(4, 9)); // "quick"
toLocaleLowerCase()
- Converts a string to lowercase in the current locale.
const str = 'HELLO';
console.log(str.toLocaleLowerCase()); // "hello"
toLocaleUpperCase()
- Converts a string to uppercase in the current locale.
const str = 'hello';
console.log(str.toLocaleUpperCase()); // "HELLO"
toLowerCase()
- Converts a string to lowercase.
const str = 'HELLO';
console.log(str.toLowerCase()); // "hello"
toString()
- Returns a string representation of an object.
const num = 123;
console.log(num.toString()); // "123"
toUpperCase()
- Converts a string to uppercase.
const str = 'hello';
console.log(str.toUpperCase()); // "HELLO"
trim()
- Removes whitespace from both ends of a string.
const str = ' Hello World ';
console.log(str.trim()); // "Hello World"
trimEnd()
- Removes whitespace from the end of a string.
const str = ' Hello World! ';
const trimmedStr = str.trimEnd();
console.log(trimmedStr); // Output: ' Hello World!'
trimStart()
- Removes whitespace from the beginning of a string.
const str = ' Hello World! ';
const trimmedStr = str.trimStart();
console.log(trimmedStr); // Output: 'Hello World! '
valueOf()
- Returns the primitive value of an object.
const strObj = new String('Hello World!');
const primitiveValue = strObj.valueOf();
console.log(primitiveValue); // Output: 'Hello World!'
Thanks for reading ❤️