# Destructuring

Extract values from array or object using literal syntax

```javascript
var {color, position} = {
    color: "blue",
    name: "hemant",
    state: "USA",
    position: "UI Dev"
};

console.log(color, position); // blue UI Dev
```

## Use Case 1

```javascript
function generateObj(){
    return {
        color: "blue",
        name: "hemant",
        state: "USA",
        position: "UI Dev"
    };
}

var {name, state} = generateObj();
console.log(name, state); // hemant USA
```

### rename variable name

```javascript
function generateObj(){
    return {
        color: "blue",
        name: "Paa",
        state: "India",
        position: "UI Dev"
    };
}

var {name:firstName, state: location} = generateObj();
console.log(firstName, location); // Paa India
```

## Use Case 2 -  ignoring some values

```javascript
var [first,,,,last] = [1,2,3,4,5];

console.log(first, last); // 1 5
```

## Use Case 3 - get few item only

```javascript
var friends = [
    {
        name: "Hemant",
        location: "Hyd",
        status: "Cool",
        email: "hemant@gmail.com"
    },
    {
        name: "Vinay",
        location: "USA",
        status: "NOO",
        email: "vinay@gmail.com"
    }
];

friends.forEach(function({name, email}){
    console.log(name +", "+ email);
});

// output
Hemant, hemant@gmail.com
Vinay, vinay@gmail.com
```

## Use Case 3 ( set default value )

let {name='Anoymous', loc='Hyd'} = {name: "hemant"}; console.log(name, loc); // hemant Hyd
