First, we need to understand what do we mean by Destructuring the array. Destructuring of javascript was introduced in ES2015 (ES6). It is a special type of syntax which allows us to extract the data from an array or objects. We can extract many elements from the array or objects using destructuring the array. let us look at the examples.

```
const arr = [20,34,22];
```

in the above code snippet, we declared an array with name arr with some values in it, now if we have to assign these values into the variables, traditionally we would use the following syntax.

```
let a = arr[0];
let b = arr[1];
let c = arr[2];
```

But in the ES6 javascript introduced us to array destructuring, by using that simple syntax.

```
const [a,b,c] = arr;
console.log(arr);
```

this will make three variables in javascript and will assign values to them too. the output of the above example will be

a= 20; b= 34; c = 22;

If we need to skip some values then we have to skip that value in the array destructuring syntax.

```
let [a, , b] = arr;
console.log(a, b);
```

the output will be a=20 and b = 22. they will skip the 34 in this process. Let's make an object and learn more from this.

```
const restaurant = {
name: 'Matini Kitchen',
address : 'Dadar, Mumbai',
categories : ['south indian', 'north indian','itallian','Chinese'],
starterMenu : ['soup','dahi-vada','onion salad','green salad'],
mainMenu:['paneer butter masala', 'Malai Kofta', 'Onion Sabji', 'Malai 2 Pyaza'],
order: function(starterIndex, mainIndex){
return [this.starterMenu[starterIndex], this.mainMenu[mainIndex]);
}
openingHours: {
thu: {
open: 12,
close: 22,
},
fri: {
open: 11,
close: 23,
},
sat: {
open: 0, // Open 24 hours
close: 24,
},
}
```

Suppose we have to choose two categories for the food items. Currently, we have 4 categories if we want to select main and secondary food categories for some purpose. so how we can do that is by doing this.

```
const [main, secondary] = restaurant.categories;
console.log(main,secondary);
```

if look at the console we will get the following output.

South Indian, north Indian.

suppose if we want to give main value to secondary and secondary value to main then traditionally we would have required third variable to do so. this is how we will do it in the older way.

```
const temp = main;
main = secondary;
secondary = temp;
```

But it can be done easily by array destructuring.

```
let [main, secondary] = [secondary,main]
```

in the above example, the restaurant variable has one object method called to order. in which we pass the index value of the starter menu and main menu by array destructuring we can get two return values from this function.

```
const [starter, mainCourse] = restaurant.order(3,1);
console.log(starter, mainCourse);
```

in this above example, we will get index number 3 from restaurant.starterMenu which is a green salad and index number 1 from restaurant.mainMenu. which is Malai Kofta.

It is possible to destructure nested arrays and objects. here we will talk about arrays only.

```
const nested = [2, 4, [5, 6]];
const [f1, , f2] = nested;
console.log(f1, f2);
```

in the above situation, we will get the value in variable f1 is 2 and in f2 we will get the array [5,6]. We can also assign variables to the nested array. for example

```
const [i, , [j, k]] = nested;
console.log(i, j, k);
```

in this example, we will get the following values in the variables i,j, and k will be 2,5,6 respectively.

In some cases it is possible that the array on which we are taking the values does not have values in them in this case we can give them default values too.

```
const [p = 1, q = 2, r = 3] = [8, 9];
console.log(p, q, r);
```

In this case, p and q will have values 8 and 9 and r will have a default value in it.

We will talk about object destructuring in another post.

]]>Set of steps required to solve a problem is called an algorithm. For example, you want to make a recipe. What will you do? what steps you will follow. The algorithm has certain steps to follow and after following those steps we get the result.

In the software world, the term algorithm means the steps taken in solving the programming problem. Such as searching some of the array elements or searching the nearest path between two points are examples of the algorithm.

Let us talk about one example of searching the element from the array of 10 elements.

```
[1,2,3,4,5,6,7,8,9,10]
```

In this array of the 10 digits suppose we are finding the digit 4. Now we talk about what approach we can take to search the digit 4. Let's talk about Linear Search

in Linear Search, we check the digit against all elements of the array. so if the value is 4 linear searches will take 4 steps to search the item. it will work like this

4 != 1

4!= 2

4!=3

4 = 4

Hence, in the above example, the program took 4 steps in completing the task of searching the number. To solve any given problem there may be more than one algorithms. We need to know which algorithm will be best to solve our problem in a given time.

Let's talk about the features of the algorithm. There are few points which we should consider while making an algorithm.

- Clearly defined problem statement, input and output
- The steps in the algorithm should be in a very specific order.
- The steps also need to be distinct.
- The Algorithm should produce a result
- The Algorithm should complete in finite time

An algorithm should take exact steps to solve the problem for the same input and the result should be the same each time. for example in the above example when we were doing a linear search and we searched for 4 in the array. each time it should take 4 steps and produce the same result. If sometimes it takes 4 steps and some times 5 steps then the algorithm is not correct.

As we know that any given problem can be solved using multiple algorithms, now how do know which algorithm is good? how we should choose the best one for our problem. well, there are two criteria on which an algorithm is considered.

- Correctness
- Efficiency

The algorithm should produce the exact output which is required for our program. if it is not correct then the algorithm is not correct. for example, if have made an algorithm to add two numbers and we give it two inputs suppose we give it 5 and 3 as inputs the exact result which should come is 8 but if it does not then the algorithm is not correct.

After getting the correct algorithm we need to test it in terms of its efficiency.

There are basically two ways to determine the efficiency when it comes to the algorithms.

- Time
- Space

**Time **means the amount of time taken by an algorithm to produce a result or in completion of the algorithm. It should take minimum time to finish the task. it is called time complexity. If an algorithm takes infinite time to finish its job it is a bad algorithm.

**Space** if the algorithm is fast but it takes all the space which is available in the RAM then it is also a bad algorithm. An algorithm should take minimum space to complete its task.

*This is the first part of the series Algorithms and Data structure next part will be published next week.*