|
| 1 | +let name1 = "Bilge"; |
| 2 | +let surname = "Demir"; |
| 3 | + |
| 4 | +const fullName = `${surname} |
| 5 | +${name1}` // just go next line if you want outputs to be next line |
| 6 | + |
| 7 | +console.log(fullName); |
| 8 | + |
| 9 | + |
| 10 | + |
| 11 | +// Destructuring objects |
| 12 | + |
| 13 | +const player = { |
| 14 | + name:'Lebron James', |
| 15 | + club:'LA Lakers', |
| 16 | + adress: { |
| 17 | + city:'Los Angeles', |
| 18 | + neighbour:'Santa Monica', |
| 19 | + } |
| 20 | +}; |
| 21 | + |
| 22 | +console.log(player.name); /// Lebron James |
| 23 | +console.log(player.city); /// undefined |
| 24 | + |
| 25 | +console.log(player.adress.neighbour); /// Santa Monica |
| 26 | +// but you can do that in a better way : |
| 27 | + |
| 28 | +const {name, club} = player; |
| 29 | + |
| 30 | + |
| 31 | +// now you can do: |
| 32 | + |
| 33 | +console.log(`${name} is playing for ${club}`); // we broke down player.name structure |
| 34 | + |
| 35 | +// to do it for city for example: |
| 36 | + |
| 37 | +const {adress: {city, neighbour}} = player; |
| 38 | +console.log(`${neighbour} is the neighbor of the ${city}`); |
| 39 | + |
| 40 | + |
| 41 | +/// Destructuring Arrays |
| 42 | + |
| 43 | +let names = ['Bilge','Asli','Minnos']; /// we can create a pointer element to destructure |
| 44 | + |
| 45 | +console.log(names[1]) // normally we do that but lets create a pointer: |
| 46 | + |
| 47 | +let [person1] = ['Bilge','Asli','Minnos'] |
| 48 | + |
| 49 | +console.log(person1) /// Bilge |
| 50 | + |
| 51 | +let [personx,persony,person3] = ['Bilge','Asli','Minnos'] |
| 52 | + |
| 53 | +console.log( personx, persony) /// Bilge Asli |
| 54 | +console.log(person3) /// Minnos |
| 55 | + |
| 56 | + |
| 57 | +// we can override the value |
| 58 | + |
| 59 | +person3 = 'noMinnos' |
| 60 | + |
| 61 | +console.log(person3) /// noMinnos |
| 62 | + |
| 63 | + |
| 64 | + |
0 commit comments