JavaScript String
Creating Strings
1. Using Double Quotes
const string1 = “Hello, World!”;
2. Using Single Quotes
const string2 = ‘Hello, World!’;
Common String Methods
1. length Property : Returns the length of the string.
const str = “Hello, World!”;
console.log(str.length);
// Output: 13
2. charAt(index) : Returns the character at the specified index.
const str = “Hello, World!”;
console.log(str.charAt(0));
// Output: H
3. indexOf(searchValue) : Returns the index of the first occurrence of the specified value, or -1 if not found.
const str = “Hello, World!”;
console.log(str.indexOf(“World”));
// Output: 7
console.log(str.indexOf(“world”));
// Output: -1
4. lastIndexOf(searchValue) : Returns the index of the last occurrence of the specified value, or -1 if not found.
const str = “Hello, World! Hello!”;
console.log(str.lastIndexOf(“Hello”));
// Output: 14
5. includes(searchValue) : Returns true if the string contains the specified value, otherwise false.
const str = “Hello, World!”;
console.log(str.includes(“World”));
// Output: true
console.log(str.includes(“world”));
// Output: false
6. startsWith(searchValue) : Returns true if the string starts with the specified value, otherwise false.
const str = “Hello, World!”;
console.log(str.startsWith(“Hello”));
// Output: true
console.log(str.startsWith(“World”));
// Output: false
7. endsWith(searchValue) : Returns true if the string ends with the specified value, otherwise false.
const str = “Hello, World!”;
console.log(str.endsWith(“World!”));
// Output: true
console.log(str.endsWith(“Hello”));
// Output: false
8. substring(start, end) : Returns the part of the string between the start and end indexes.
const str = “Hello, World!”;
console.log(str.substring(0, 5));
// Output: Hello
9. slice(start, end) : Similar to substring but also supports negative indices.
const str = “Hello, World!”;
console.log(str.slice(0, 5));
// Output: Hello
console.log(str.slice(-6, -1));
// Output: World
10. toLowerCase() : Returns the string converted to lowercase.
const str = “Hello, World!”;
console.log(str.toLowerCase());
// Output: hello, world!
11. toUpperCase() : Returns the string converted to uppercase.
const str = “Hello, World!”;
console.log(str.toUpperCase());
// Output: HELLO, WORLD!
12. trim() : Removes whitespace from both ends of the string.
const str = ” Hello, World! “;
console.log(str.trim());
// Output: Hello, World!
13. replace(searchValue, replaceValue) : Returns a new string with some or all matches of a pattern replaced by a replacement.
const str = “Hello, World!”;
const newStr = str.replace(“World”, “Universe”);
console.log(newStr);
// Output: Hello, Universe!
14. split(separator) : Splits a string into an array of substrings using the specified separator.
const str = “Hello, World!”;
const arr = str.split(“, “);
console.log(arr);
// Output: [“Hello”, “World!”]
15. concat() : Merges or combine multiple entities into a single one.
const string1 = “Hello”;
const string2 = “World”;
const combinedString = string1.concat(” “, string2, “!”);
// Output: combinedString will be “Hello World!”