You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: notes/English/28-Array.md
+31-31Lines changed: 31 additions & 31 deletions
Original file line number
Diff line number
Diff line change
@@ -1,12 +1,12 @@
1
-
### What is an Array in JavaScript?
1
+
### What is an Array ?
2
2
An array in JavaScript is a data structure that stores a collection of elements, such as numbers or strings, in a linear fashion. Each element in an array is assigned a unique index starting from 0, which can be used to access and modify its value. Arrays in JavaScript can dynamically grow or shrink in size and support various methods for manipulating their contents.
3
3
4
4
5
-
### Need of Array in JavaScript?
5
+
### Need of Array ?
6
6
Arrays are an essential data structure in JavaScript as they allow you to store and manipulate collections of data, such as lists of numbers or strings. They provide a way to access elements using index positions, add or remove elements, sort the array, and perform various operations on the data efficiently. Arrays are also useful for organizing and structuring code, and many programming tasks involve working with arrays in some form.
7
7
8
8
9
-
### How to create an Array in JavaScript?
9
+
### How to create an Array ?
10
10
You can create an array in JavaScript by using square brackets []. Here's an example code snippet:
In the example above, `myArray` is an empty array and `anotherArray` is an array that contains five elements of different data types. Note that arrays in JavaScript are dynamic, which means you can add or remove elements from them at any time.
21
21
22
22
23
-
### Array Literal in JavaScript?
23
+
### Array Literal ?
24
24
An array literal in JavaScript is a way to create an array by enclosing a comma-separated list of values inside square brackets, like this: `[value1, value2, value3]`. For example, `let myArray = [1, 2, 3];` creates an array named `myArray` with three elements containing the values 1, 2, and 3. Array literals are a convenient way to initialize arrays with known values.
25
25
26
26
27
-
### Array Object in JavaScript?
27
+
### Array Object ?
28
28
In JavaScript, an array is an object that stores a collection of values in a list-like structure. It can hold any type of data, including numbers, strings, objects, and even other arrays. The elements in an array are identified by their index, which starts at 0 for the first element and increments by 1 for each subsequent element. You can create an array using the array literal notation, or by using the Array constructor function. Arrays have many built-in methods for manipulating and iterating over their elements, such as push(), pop(), slice(), forEach(), map(), and reduce().
29
29
30
30
31
-
### Index of Array in JavaScript?
31
+
### Index of Array ?
32
32
In JavaScript, the index of an array is a numerical value used to access and manipulate its elements. The first element in an array has an index of 0, the second element has an index of 1, and so on. You can access an element in an array using its index by placing the index within square brackets after the name of the array. For example:
In JavaScript, the length property of an array returns the number of elements in the array. For example, if an array has three elements, its length property will return 3. The length property is automatically updated when elements are added to or removed from the array.
44
44
45
45
46
-
### Array Declaration in JavaScript?
46
+
### Array Declaration ?
47
47
In JavaScript, an array can be declared using square brackets with comma-separated values inside the brackets or by calling the Array constructor.
Note that in both cases, the values inside the array can be of any data type (such as numbers, strings, objects, etc.). Additionally, arrays in JavaScript are dynamic and can grow or shrink as needed.
64
64
65
65
66
-
### Looping Array in JavaScript?
66
+
### Looping Array ?
67
67
To loop through an array in JavaScript, you can use a `for` loop or the `forEach()` method.
68
68
69
69
Using a `for` loop:
@@ -107,7 +107,7 @@ This uses a compare function that subtracts `b` from `a` instead of subtracting
107
107
Note that the `sort()` function modifies the original array and returns the sorted array.
108
108
109
109
110
-
### push function in JavaScript?
110
+
### push function ?
111
111
The "push()" function is a built-in method in JavaScript arrays that adds one or more elements to the end of an array and returns the new length of the array. The syntax for using the push() function is:
where "array" is the name of the array, and "element1", "element2", ..., "elementN" are the elements to be added to the end of the array.
118
118
119
119
120
-
### pop function in JavaScript?
120
+
### pop function ?
121
121
In JavaScript, the `pop()` function can be used on an array to remove and return the last element of the array. The array's length is reduced by 1 after calling this function. Here's an example:
The unshift() function in JavaScript is an array method that adds one or more elements to the beginning of an array and returns the new length of the array. The original array is modified by adding the elements at the beginning. The syntax is as follows:
where `element1`, `element2`, ..., `elementN` are the elements to be added to the beginning of the array.
139
139
140
140
141
-
### shift function in JavaScript?
141
+
### shift function ?
142
142
In JavaScript, the shift() function is used to remove the first element from an array and return it. This function also updates the length and indexes of the remaining elements in the array.
In JavaScript, the `toString` function is a method that can be called on an object to convert it into a string representation. By default, the `toString` method returns a string representing the object. However, some built-in objects like `Date`, `Array`, and `RegExp` override this method to provide a more meaningful string representation of the object. Additionally, developers can also override the `toString` method for their own custom objects to provide a customized string representation.
155
155
156
156
157
-
### join function in JavaScript?
157
+
### join function ?
158
158
The `join()` function is a built-in method in JavaScript that converts an array into a string by concatenating all elements of the array with a specified separator. The syntax for using `join()` is as follows:
In JavaScript, the `concat()` function is used to merge two or more arrays together and create a new array that contains all the elements from the original arrays. The syntax for `concat()` is as follows:
where `array1` is the initial array, and `array2`, `array3`, ..., `arrayN` are additional arrays that will be concatenated onto `array1`. The resulting `newArray` will contain all the elements from each of the input arrays in the order they were passed into the function.
193
193
194
194
195
-
### splice function in JavaScript?
195
+
### splice function ?
196
196
The `splice()` function in JavaScript is used to add or remove elements from an array at a specified index. It has the following syntax:
The `splice()` function returns an array containing the deleted elements, or an empty array if no elements were deleted. If elements were added to the array, the original array is modified and returned.
207
207
208
208
209
-
### slice function in JavaScript?
209
+
### slice function ?
210
210
The slice() function in JavaScript creates a new array by copying a portion of an existing array. It takes two arguments: the starting index (inclusive) and the ending index (exclusive) of the portion to be copied. If no ending index is provided, the slice includes all elements from the starting index to the end of the array. The original array is not modified by the slice function.
211
211
212
212
213
-
### sort function in JavaScript?
213
+
### sort function ?
214
214
The `sort()` function in JavaScript is a built-in method that allows you to sort the elements of an array in place, meaning it modifies the original array. You can call the `sort()` function on an array and pass in a compare function as its argument to specify how the elements should be sorted. If no compare function is passed in, the elements are sorted alphabetically by default.
The forEach function is a built-in method in JavaScript that is used to iterate over an array or array-like object and execute a callback function for each element. The callback function is passed three arguments: the current element, its index, and the entire array being iterated. The forEach function does not return anything, it simply executes the provided function on each element of the array.
251
251
252
252
253
-
### at function in JavaScript?
253
+
### at function ?
254
254
The `at` function in JavaScript is used to retrieve an element from an array at a specific index. It takes one parameter, which is the index of the element to be retrieved. The index can be a positive or negative integer, where negative indices are counted from the end of the array. The function returns the element if it exists at the specified index, and undefined otherwise.
255
255
256
256
Note that the `at` function is relatively new and may not be supported by all browsers. In older browsers or environments without support for `at`, you can use bracket notation (e.g., `myArray[index]`) instead to access array elements.
257
257
258
258
259
-
### map function in JavaScript?
259
+
### map function ?
260
260
The `map()` function in JavaScript is a built-in method available in Array objects. It creates a new array by calling a provided function on each element of the original array. The provided function takes three arguments: the current element being processed, its index, and the array being processed. The `map()` function does not modify the original array.
261
261
262
262
263
-
### filter function in JavaScript?
263
+
### filter function ?
264
264
The `filter()` function in JavaScript is an array method that creates a new array with all the elements that pass the test implemented by the provided function. The syntax for using `filter()` is:
The `reduce()` function in JavaScript is a higher-order function that allows you to iterate over an array and accumulate the values into a single value. It takes a callback function as its first parameter, which is applied to each element in the array, and then returns the final accumulated value. The callback function takes two parameters: an accumulator (which starts with an initial value or the first element of the array) and the current element being iterated over. Here's an example usage:
In this example, the `reduce()` function adds up all the numbers in the `numbers` array and returns the accumulated sum, which is 15.
296
296
297
297
298
-
### find function in JavaScript?
298
+
### find function ?
299
299
The `find()` function is a built-in method in JavaScript used to search for an element in an array that satisfies a provided condition. It takes in a callback function and returns the first element in the array that meets the condition specified by the callback, or undefined if no such element is found.
The `findIndex` function in JavaScript is an array method that returns the index of the first element in the array that satisfies a provided testing function. If no such element is found, it returns -1. The syntax for using `findIndex` is as follows:
In this example, `findIndex` searches through the `arr` array and finds the first element that is even, which is 2 at index 0. Therefore, `evenIndex` is set to 0.
333
333
334
334
335
-
### some function in JavaScript?
335
+
### some function ?
336
336
Sure, here are a few examples of functions in JavaScript:
337
337
338
338
1.`console.log()`: This function is used to print messages to the browser's console.
It is not possible to list every function in JavaScript as there are countless built-in functions and libraries, and new ones are constantly being developed by the community. However, there are some commonly used built-in functions that are essential for programming in JavaScript, such as `console.log()`, `setTimeout()`, `parseInt()`, `parseFloat()`, `Math.random()`, and many more. Additionally, developers can create their own custom functions to perform specific tasks or manipulate data in unique ways.
353
353
354
354
355
-
### flat function in JavaScript?
355
+
### flat function ?
356
356
In JavaScript, a flat function is used to flatten a multidimensional array into a one-dimensional array. The flat() function returns a new array with all elements flattened recursively up to the specified depth (by default it flattens all levels). Here's an example:
Note that the flat() function is only available in newer versions of JavaScript (ECMAScript 2019), so it may not be supported in older browsers. In that case, you can use a polyfill or a custom implementation to achieve the same result.
364
364
365
365
366
-
### flatMap function in JavaScript?
366
+
### flatMap function ?
367
367
The `flatMap()` function in JavaScript is an array method that combines mapping and flattening into a single step. It takes each element in the array, applies a mapping function to it, and then flattens the result into a new array. The resulting array has the same length as the original array, but the elements may be different due to the mapping and flattening process. The `flatMap()` function can also be used to remove empty or undefined values from an array.
0 commit comments