Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 23de13e

Browse files
Added arrays
1 parent 4991355 commit 23de13e

File tree

2 files changed

+147
-3
lines changed

2 files changed

+147
-3
lines changed

‎README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,9 @@ This repository contains Vanilla JavaScript topics and their notes to learn from
7979
- [Polymorphism](./docs/objects.md#-polymorphism)
8080
- [this keyword](./docs/objects.md#-this-keyword)
8181
- [Prototype inheritance](./docs/objects.md#-prototype-inheritance)
82-
2. Arrays:
83-
- Array methods
84-
- Array iteration
82+
2. [Arrays:](./docs/arrays.md)
83+
- [Array methods](./docs/arrays.md#-array-methods)
84+
- [Array iteration](./docs/arrays.md#-array-iteration)
8585
3. Asynchronous JavaScript:
8686
- Callbacks
8787
- Understanding asynchronous operations

‎docs/arrays.md

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
## ⚑ Arrays:
2+
3+
Arrays are generally [Non-Primitive Data Types](./data-types.md#-non-primitive-data-types).
4+
5+
Array in JavaScript are collections of value list, where the values are any data type. They represent list of values.
6+
7+
### ☴ Overview:
8+
1. [Array Methods](#-array-methods)
9+
2. [Array Iteration](#-array-iteration)
10+
11+
### ✦ Array Methods:
12+
JavaScript provide a variety of methods for manipulating and working with arrays.
13+
14+
**Common Array Methods:**
15+
- `push():` Adds elements to the end of an array.
16+
- `pop():` Removes the last element from an array and returns it.
17+
- `shift():` Removes the first element from an array and returns it.
18+
- `unshift():` Adds elements to the beginning of an array.
19+
- `slice():` Creates a new array containing a portion of the original array.
20+
- `splice():` Removes or replaces elements in an array.
21+
- `concat():` Combines multiple arrays into a new array.
22+
- `join():` Joins elements of an array into a string.
23+
- `reverse():` Reverses the order of elements in an array.
24+
- `sort():` Sorts elements in an array.
25+
- `forEach():` Executes a function for each element in an array.
26+
- `map():` Creates a new array by applying a function to each element of the original array.
27+
- `filter():` Creates a new array containing elements that meet a certain condition.
28+
- `reduce():` Applies a function to each element in an array to reduce it to a single value.
29+
- `indexOf():` Returns the index of the first occurrence of an element in an array.
30+
- `lastIndexOf():` Returns the index of the last occurrence of an element in an array.
31+
- `includes():` Checks if an array contains a specified element.
32+
- `find():` Returns the first element in an array that meets a certain condition.
33+
- `findIndex():` Returns the index of the first element in an array that meets a certain condition.
34+
35+
```javascript
36+
let fruits = ["apple", "banana", "orange"];
37+
38+
// Adding elements
39+
fruits.push("grape");
40+
fruits.unshift("mango");
41+
console.log(fruits); // Output: (5) ['mango', 'apple', 'banana', 'orange', 'grape']
42+
43+
// Removing elements
44+
let removedFruit = fruits.pop();
45+
console.log(removedFruit); // Output: grape
46+
console.log(fruits); // Output: (4) ['mango', 'apple', 'banana', 'orange']
47+
48+
// Creating a new array
49+
let slicedFruits = fruits.slice(1, 3);
50+
console.log(slicedFruits); // Output: (2) ['apple', 'banana']
51+
console.log(fruits); // Output: (4) ['mango', 'apple', 'banana', 'orange']
52+
53+
// Joining elements
54+
let fruitString = fruits.join(", ");
55+
console.log(fruitString); // Output: mango, apple, banana, orange
56+
console.log(fruits); // Output: (4) ['mango', 'apple', 'banana', 'orange']
57+
58+
// Sorting elements
59+
fruits.sort();
60+
console.log(fruits); // Output: (4) ['apple', 'banana', 'mango', 'orange']
61+
62+
// Iterating over elements
63+
fruits.forEach(function(fruit) {
64+
console.log(fruit);
65+
});
66+
/*
67+
apple
68+
banana
69+
mango
70+
orange
71+
*/
72+
73+
// Mapping elements
74+
let fruitLengths = fruits.map(function(fruit) {
75+
return fruit.length;
76+
});
77+
78+
console.log(fruitLengths); // Output: (4) [5, 6, 5, 6]
79+
```
80+
81+
### ✦ Array Iteration:
82+
Array iteration involves traversing each element of an array to perform operations on them. There are several ways to iterate over arrays in JavaScript.
83+
84+
**Iteration of Array:**
85+
- for loop:
86+
```javascript
87+
for (let i = 0; i < fruits.length; i++) {
88+
console.log(fruits[i]);
89+
}
90+
```
91+
92+
- for...in loop:
93+
```javascript
94+
for (let index in fruits) {
95+
console.log(fruits[index]);
96+
}
97+
```
98+
99+
- for...of loop:
100+
```javascript
101+
for (let fruit of fruits) {
102+
console.log(fruit);
103+
}
104+
```
105+
- Array methods as loop:
106+
- forEach()
107+
- map()
108+
- filter()
109+
- reduce()
110+
111+
*These methods provide different ways to iterate over arrays and perform operations on their elements, depending on the specific needs.*
112+
113+
**Note:** When comes to loop, iterations, setTimeout and setInterval. Use [`let`](./variables.md#-let). That treats as a new [`variable`](./variables.md) on each iteration. But [`var`](./variables.md#-var) overwrites that memory place.
114+
115+
```javascript
116+
/* let usage */
117+
let fruits = ["apple", "banana", "orange"];
118+
for(let fruit of fruits) {
119+
setTimeout(() => { console.log(fruit) }, 1000);
120+
}
121+
/*
122+
apple
123+
banana
124+
orange
125+
*/
126+
127+
/* var usage */
128+
let fruits = ["apple", "banana", "orange"];
129+
for(var fruit of fruits) {
130+
setTimeout(() => { console.log(fruit) }, 1000);
131+
}
132+
/*
133+
orange
134+
orange
135+
orange
136+
*/
137+
```
138+
139+
---
140+
[&#8682; To Top](#-arrays)
141+
142+
[&#10094; Previous Topic](./objects.md) &emsp; [Next Topic &#10095;](./asynchronous-javascript.md)
143+
144+
[&#8962; Goto Home Page](../README.md)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /