In the vast landscape of JavaScript, strings are a fundamental building block, and mastering JavaScript string methods is pivotal in crafting dynamic and engaging web experiences. The W3Schools platform provides an excellent playground to learn and practice these methods, offering interactive examples and clear documentation. Let's delve into the key JavaScript string methods, exploring them in detail and providing practical examples.

JavaScript strings are immutable, meaning they cannot be changed after they're created. Various methods enable us to manipulate, marching, slicing, and searching within these strings. With over 50 methods at our disposal, we'll focus on the most common and versatile ones.

Basic String Operations
We'll kickstart our journey by exploring some fundamental JavaScript string methods that allow us to extract and manipulate substrings, and retrieve information about the string itself.

length Property
The `length` property doesn't require any explanation. It returns the number of characters in a string. For instance, `text.length` will give us the length of the string `text`.

```javascript let text = "Hello, World!"; console.log(text.length); // Outputs: 13 ```
split() Method
The `split()` method divides a String into an ordered list of substrings, puts these substrings into an array, and returns the array. The separator is the character or set of characters that defines where one substring ends and the next one begins. If no separator is provided, it uses the default separator (comma).

```javascript let str = "Web Development Tutorial"; let words = str.split(" "); console.log(words); // Outputs: [ 'Web', 'Development', 'Tutorial' ]
Manipulating Strings
Once we've extracted substrings or found specific positions, we may want to manipulate the string further. Here are a couple of methods that help in doing so.

substring(), substr(), and slice() Methods
These methods extract a section of a string and return it as a new string, without modifying the original. The main difference between these methods lies in their syntax and the way they handle the start and end indices: - `substring()` considers the two parameters as indices and ignores negative values. - `substr()` treats the second parameter as the length of the extracted substring rather than its end index. - `slice()` is the most versatile and powerful, accepting up to three parameters: the start index, the end index (optional, defaults to the string's end), and a step value (for extracting a series of substrings).










![[Solved] How to count Vowels and Consonants in Java String Word? Example](https://i.pinimg.com/originals/9e/34/94/9e3494d47f9aeda7562f34f97430ad70.png)








```javascript let str = "Apple, Banana, Cantaloupe"; console.log(str.substring(7, 13)); // Outputs: Banana
```javascript console.log(str.substr(7, 8)); // Outputs: Banana
```javascript console.log(str.slice(7)); // Outputs: Banana, Cantaloupe console.log(str.slice(-3, -1)); // Outputs: op ```
replace() Method
The `replace()` method lets us swap one substring for another within a string. It takes two arguments: the substring to search for and the string to replace it with. If no global flag (`g`) is provided, it only replaces the first found occurrence. If `g` is included, it replaces all matched occurrences.
```javascript let sentence = "Please, revisit this sentence"; sentence = sentence.replace(/visit/g, 'review'); console.log(sentence); // Outputs: 'Please, review this sentence' ```
Searching Strings
Often, we need to find specific substrings within a string or determine if a string matches a specific pattern. JavaScript provides a handful of methods for these tasks.
indexOf() and lastIndexOf() Methods
`indexOf()` and `lastIndexOf()` allow us to find the first or last occurrence of a substring within another string, respectively. Each method accepts two arguments: the substring to search for and (optionally) the starting index for the search. If no substring is found, these methods return -1.
```javascript let str = "Apples are round, apples are sweet."; console.log(str.indexOf("apples")); // Outputs: 0 console.log(str.lastIndexOf("apples")); // Outputs: 28
```javascript console.log(str.indexOf("apples", 5)); // Outputs: 28
match() and search() Methods
бирая вuenta regex, both `match()` and `search()` can find substrings that match a regular expression pattern. `match()` returns an array of found matches, while `search()` returns the index of the first match or -1 if no match is found. If no regular expression is provided, these methods use the default regular expression / / .
```javascript let text = "Visit Microsoft!"; let match = text.match(/microsoft/i); console.log(match.index); // Outputs: 7 ```