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/32-Destructuring-Array.md
+22-11Lines changed: 22 additions & 11 deletions
Original file line number
Diff line number
Diff line change
@@ -1,4 +1,5 @@
1
-
### What is destructuring and Destructuring Arrays in javascript. Explain with example
1
+
### What is destructuring and destructuring Arrays ?
2
+
2
3
Destructuring is a feature in JavaScript that allows you to extract values from arrays, objects, and other structures into distinct variables.
3
4
4
5
Destructuring Arrays is the process of breaking down an array into individual elements, and assigning those elements to separate variables. This can be done using array destructuring syntax, which uses square brackets on the left-hand side of the assignment operator to specify the variables to which the array elements should be assigned.
@@ -19,7 +20,8 @@ console.log(c); // Output: 3
19
20
In this example, we have an array `numbers` containing three values. We use array destructuring to assign each value to a separate variable `a`, `b`, and `c`. Now we can use these variables independently, as shown in the console.log statements.
20
21
21
22
22
-
### How to Reverse values using destructuring in javascript. Explain with example
23
+
### How to reverse values using destructuring ?
24
+
23
25
To reverse values using destructuring in JavaScript, you can use an array with the `reverse()` method and then destructure the reversed array into new variables. Here's an example:
24
26
25
27
```javascript
@@ -41,7 +43,8 @@ console.log(five); // Output: 1
41
43
In this example, we first create an array of numbers, then use the `reverse()` method to reverse the order of the elements in the array. We then destructure the reversed array into variables named `one`, `two`, `three`, `four`, and `five`. Finally, we log each variable to the console to confirm that the values have been reversed.
42
44
43
45
44
-
### How to Return two values from function in javascript. Explain with example
46
+
### How to return two values from function?
47
+
45
48
In JavaScript, it is not possible to directly return two values from a function. However, there are several ways to achieve this:
46
49
47
50
1. Using an Array: We can create an array and store the values that we want to return in it. Then, we can return the array from the function.
@@ -93,7 +96,8 @@ console.log(b); // Output: 20
93
96
```
94
97
95
98
96
-
### What is Destructuring of nested array in javascript. Explain with example
99
+
### How to Destructure a nested array ?
100
+
97
101
Destructuring of nested array in JavaScript is a way to extract values from an array that contains other arrays, objects or primitives.
98
102
99
103
For example, consider the following nested array:
@@ -113,7 +117,8 @@ Here, the variables `a`, `b`, `c`, `d`, `e`, `f` and `g` are assigned the values
113
117
So, after running the above code, `a` will contain 1, `b` will contain 2, `c` will contain 3, `d` will contain 4, `e` will contain 5, `f` will contain 6, and `g` will contain 7.
114
118
115
119
116
-
### how to set default values in destructuring in javascript. Explain with example
120
+
### How to set default values in destructuring ?
121
+
117
122
In JavaScript, default values can be set in destructuring by using the `=` operator. The default value will be used if the corresponding variable in the destructuring assignment is undefined.
118
123
119
124
Here's an example:
@@ -135,7 +140,8 @@ console.log(age); // 25
135
140
In the second example, we're setting a default value of 25 for the `age` variable. If `age` is undefined in the object being destructured (`personWithAge`), the default value of 25 will be used instead. We're also renaming the `name` variable to `newName` using the syntax `{ name: newName }`.
136
141
137
142
138
-
### What is Destructuring Objects in javascript. Explain with example
143
+
### What is destructuring Objects ?
144
+
139
145
Destructuring objects in JavaScript is a way to extract values from an object and assign them to variables in a concise and readable manner.
140
146
141
147
Here's an example:
@@ -172,7 +178,8 @@ console.log(yearsOld); // 30
172
178
This allows us to use a different variable name for clarity or to avoid naming conflicts.
173
179
174
180
175
-
### How to Extract any value from object in javascript using destructring. Explain with example
181
+
### How to extract any value from object using destructring ?
182
+
176
183
To extract any value from an object in JavaScript using destructuring, you can use the following syntax:
In the above example, we are renaming `firstName` to `first` and `lastName` to `last` while destructuring the `person` object. This means that we are creating two new variables `first` and `last` whose values are the respective values of the `firstName` and `lastName` properties of the `person` object.
221
229
222
230
223
-
### Default values in destructuring in javascript. Explain with example
231
+
### Setting Default values in destructuring ?
232
+
224
233
In JavaScript, it's possible to set default values for variables when using destructuring assignment. This means that if the value being destructured is undefined or null, a default value will be used instead.
In this code, we are using object destructuring to create two variables `name` and `age`, with default values of "Anonymous" and 18 respectively. Since we're destructuring an empty object `{}`, the values of `name` and `age` will be the defaults we specified.
235
244
236
245
237
-
### Destructuring of Nested Object in javascript. Explain with example
246
+
### Destructuring of Nested Object ?
247
+
238
248
Destructuring is a feature in JavaScript that allows you to extract values from objects and arrays and assign them to variables. When dealing with nested objects, destructuring can provide a concise way to access their properties.
239
249
240
250
Here's an example of destructuring a nested object:
In the example above, we have an object `user` with a nested object `address`. We use destructuring to extract the properties `name`, `age`, `street`, `city`, `state`, and `zip` from the `user` object by assigning them to variables with the same names. We also use the syntax `address: { ... }` to destructure the nested `address` object.
266
276
267
277
268
-
### How to use destrcutring in Function in javascript. Explain with example
278
+
### How to use destrcutring in Function ?
279
+
269
280
Destructuring is a way to extract values from arrays or objects into distinct variables. You can use destructuring in function arguments to unpack arguments passed to the function.
270
281
271
282
Here's an example of how to use destructuring in a function:
0 commit comments