0% found this document useful (0 votes)
11 views

Strings Js Notes

notes for strings in js

Uploaded by

Anish S Taklikar
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Strings Js Notes

notes for strings in js

Uploaded by

Anish S Taklikar
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

Certainly!

Here's a comprehensive overview of strings in arrays in JavaScript:

### **Strings in Arrays**

**1. **Definition and Basics**


- **Array**: A collection of elements that can be of any type, including strings.
Arrays are indexed collections where each element can be accessed using an index.
- **String**: A sequence of characters used to represent text.

**2. **Creating Arrays of Strings**


You can create an array where each element is a string:

```javascript
const fruits = ["Apple", "Banana", "Cherry"];
```

**3. **Accessing String Elements in an Array**


You can access individual strings in an array using their index:

```javascript
console.log(fruits[0]); // "Apple"
console.log(fruits[1]); // "Banana"
```

**4. **Modifying Strings in an Array**


You can update elements in an array:

```javascript
fruits[1] = "Blueberry"; // Changes "Banana" to "Blueberry"
```

**5. **Array Methods with Strings**


Several methods are useful for manipulating arrays of strings:

- **`.push()`**: Adds a new string to the end of the array.

```javascript
fruits.push("Orange");
```

- **`.pop()`**: Removes the last string from the array.

```javascript
fruits.pop(); // Removes "Orange"
```

- **`.shift()`**: Removes the first string from the array.

```javascript
fruits.shift(); // Removes "Apple"
```

- **`.unshift()`**: Adds a new string to the beginning of the array.

```javascript
fruits.unshift("Grapes");
```

- **`.splice()`**: Adds or removes strings from a specific index.


```javascript
fruits.splice(1, 1, "Mango"); // Removes "Blueberry" and adds "Mango" at index 1
```

- **`.join()`**: Joins all strings in the array into a single string.

```javascript
const joinedString = fruits.join(", "); // "Grapes, Mango, Cherry"
```

- **`.sort()`**: Sorts the strings in the array alphabetically.

```javascript
fruits.sort(); // ["Cherry", "Grapes", "Mango"]
```

- **`.reverse()`**: Reverses the order of strings in the array.

```javascript
fruits.reverse(); // ["Mango", "Grapes", "Cherry"]
```

**6. **Searching and Filtering**


You can search and filter arrays of strings:

- **`.indexOf()`**: Finds the index of a specific string.

```javascript
const index = fruits.indexOf("Cherry"); // 2
```

- **`.includes()`**: Checks if a specific string exists in the array.

```javascript
const hasMango = fruits.includes("Mango"); // true
```

- **`.filter()`**: Creates a new array with strings that pass a test.

```javascript
const longFruits = fruits.filter(fruit => fruit.length > 5); // ["Mango"]
```

- **`.map()`**: Creates a new array with the results of applying a function to each
string.

```javascript
const uppercasedFruits = fruits.map(fruit => fruit.toUpperCase()); // ["MANGO",
"GRAPES", "CHERRY"]
```

**7. **Iterating Over Arrays of Strings**


You can loop through arrays using various methods:

- **`for` loop**:

```javascript
for (let i = 0; i < fruits.length; i++) {
console.log(fruits[i]);
}
```

- **`forEach()`**:

```javascript
fruits.forEach(fruit => console.log(fruit));
```

- **`for...of`**:

```javascript
for (const fruit of fruits) {
console.log(fruit);
}
```

**8. **String Methods on Array Elements**


Since array elements are strings, you can use string methods on them:

```javascript
const fruitLengths = fruits.map(fruit => fruit.length); // [5, 6, 6]
```

**9. **Multi-dimensional Arrays**


Arrays of strings can also be multi-dimensional:

```javascript
const matrix = [
["Apple", "Banana"],
["Cherry", "Date"]
];
console.log(matrix[1][0]); // "Cherry"
```

### **Summary**

- Arrays can hold multiple strings.


- You can access, modify, and manipulate strings in arrays using various methods.
- Array methods like `.push()`, `.pop()`, `.splice()`, `.join()`, and others can be
used to manage and manipulate arrays of strings.
- Iteration and searching methods allow you to efficiently work with arrays of
strings.

This overview should help you understand how to work with strings in arrays
effectively! If you have more specific questions or need examples, feel free to
ask.

You might also like