Skip to content

Commit 6f45f1b

Browse files
authored
Update 32-Destructuring-Array.md
1 parent 4301966 commit 6f45f1b

File tree

1 file changed

+22
-11
lines changed

1 file changed

+22
-11
lines changed

notes/English/32-Destructuring-Array.md

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff 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+
23
Destructuring is a feature in JavaScript that allows you to extract values from arrays, objects, and other structures into distinct variables.
34

45
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
1920
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.
2021

2122

22-
### How to Reverse values using destructuring in javascript. Explain with example
23+
### How to reverse values using destructuring ?
24+
2325
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:
2426

2527
```javascript
@@ -41,7 +43,8 @@ console.log(five); // Output: 1
4143
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.
4244

4345

44-
### How to Return two values from function in javascript. Explain with example
46+
### How to return two values from function?
47+
4548
In JavaScript, it is not possible to directly return two values from a function. However, there are several ways to achieve this:
4649

4750
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
9396
```
9497

9598

96-
### What is Destructuring of nested array in javascript. Explain with example
99+
### How to Destructure a nested array ?
100+
97101
Destructuring of nested array in JavaScript is a way to extract values from an array that contains other arrays, objects or primitives.
98102

99103
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
113117
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.
114118

115119

116-
### how to set default values in destructuring in javascript. Explain with example
120+
### How to set default values in destructuring ?
121+
117122
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.
118123

119124
Here's an example:
@@ -135,7 +140,8 @@ console.log(age); // 25
135140
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 }`.
136141

137142

138-
### What is Destructuring Objects in javascript. Explain with example
143+
### What is destructuring Objects ?
144+
139145
Destructuring objects in JavaScript is a way to extract values from an object and assign them to variables in a concise and readable manner.
140146

141147
Here's an example:
@@ -172,7 +178,8 @@ console.log(yearsOld); // 30
172178
This allows us to use a different variable name for clarity or to avoid naming conflicts.
173179

174180

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+
176183
To extract any value from an object in JavaScript using destructuring, you can use the following syntax:
177184

178185
```javascript
@@ -206,7 +213,8 @@ console.log(name, age); // Output: "John" 30
206213
```
207214

208215

209-
### How to rename Object property name in destrcutring in javascript. Explain with example
216+
### How to rename Object property name in destrcutring ?
217+
210218
In JavaScript destructuring, you can rename an object property by using the colon (:) syntax. Here's an example:
211219

212220
```javascript
@@ -220,7 +228,8 @@ console.log(last); // Output: "Doe"
220228
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.
221229

222230

223-
### Default values in destructuring in javascript. Explain with example
231+
### Setting Default values in destructuring ?
232+
224233
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.
225234

226235
Here's an example:
@@ -234,7 +243,8 @@ console.log(age); // Output: 18
234243
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.
235244

236245

237-
### Destructuring of Nested Object in javascript. Explain with example
246+
### Destructuring of Nested Object ?
247+
238248
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.
239249

240250
Here's an example of destructuring a nested object:
@@ -265,7 +275,8 @@ console.log(zip); // Output: 12345
265275
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.
266276

267277

268-
### How to use destrcutring in Function in javascript. Explain with example
278+
### How to use destrcutring in Function ?
279+
269280
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.
270281

271282
Here's an example of how to use destructuring in a function:

0 commit comments

Comments
 (0)