mirror of
https://github.com/Fechin/reference.git
synced 2026-11-20 21:23:11 +03:00
Merge pull request #748 from hwisnu222/destructuring-object
Add destructuring, object lookup and optional chaining javascript
This commit is contained in:
1 file changed
+51
-6
@@ -210,6 +210,17 @@ false ?? 'I lose'; // false
|
||||
'' ?? 'Damn it'; // ''
|
||||
```
|
||||
|
||||
#### Optional Chaining ?.
|
||||
|
||||
```javascript
|
||||
const obj = {
|
||||
name: 'John Doe',
|
||||
age: 8
|
||||
};
|
||||
|
||||
console.log(obj?.address);
|
||||
```
|
||||
|
||||
### else if
|
||||
|
||||
```javascript
|
||||
@@ -244,6 +255,19 @@ switch (food) {
|
||||
}
|
||||
```
|
||||
|
||||
### Object Lookup
|
||||
|
||||
```javascript
|
||||
const food = 'salad';
|
||||
|
||||
const foods = {
|
||||
oyster: 'The taste of the sea',
|
||||
pizza: 'A delicious pie'
|
||||
};
|
||||
|
||||
console.log(foods[food] || 'Enjoy your meal');
|
||||
```
|
||||
|
||||
### == vs ===
|
||||
|
||||
```javascript
|
||||
@@ -491,10 +515,10 @@ console.log(myArray[1]); // 200
|
||||
|
||||
| | add | remove | start | end |
|
||||
| :-------- | :-: | :----: | :---: | :-: |
|
||||
| `push` | ✔ | | | ✔ |
|
||||
| `pop` | | ✔ | | ✔ |
|
||||
| `unshift` | ✔ | | ✔ | |
|
||||
| `shift` | | ✔ | ✔ | |
|
||||
| `push` | ✔ | | | ✔ |
|
||||
| `pop` | | ✔ | | ✔ |
|
||||
| `unshift` | ✔ | | ✔ | |
|
||||
| `shift` | | ✔ | ✔ | |
|
||||
|
||||
{.show-header}
|
||||
|
||||
@@ -562,6 +586,15 @@ numbers.concat(newFirstNumber);
|
||||
|
||||
If you want to avoid mutating your original array, you can use concat.
|
||||
|
||||
### Destructuring
|
||||
|
||||
```javascript
|
||||
const [name, age] = ['John Doe', 8];
|
||||
|
||||
console.log(name);
|
||||
console.log(age);
|
||||
```
|
||||
|
||||
## JavaScript Set
|
||||
|
||||
### Create Set
|
||||
@@ -750,7 +783,6 @@ for (let index in fruits) {
|
||||
// => 1
|
||||
// => 2
|
||||
|
||||
|
||||
// 2. Print only values
|
||||
for (let index in fruits) {
|
||||
console.log(fruits[index]);
|
||||
@@ -759,7 +791,6 @@ for (let index in fruits) {
|
||||
// => orange
|
||||
// => banana
|
||||
|
||||
|
||||
// 3. Print index with value
|
||||
for (let index in fruits) {
|
||||
console.log(index, fruits[index]);
|
||||
@@ -1055,6 +1086,20 @@ console.log(myCat.name);
|
||||
myCat.name = 'Yankee';
|
||||
```
|
||||
|
||||
### Destructuring
|
||||
|
||||
```javascript
|
||||
const obj = {
|
||||
name: 'John Doe',
|
||||
age: 8
|
||||
};
|
||||
|
||||
const { name, age } = obj;
|
||||
|
||||
console.log(name);
|
||||
console.log(age);
|
||||
```
|
||||
|
||||
## JavaScript Classes
|
||||
|
||||
### Static Methods
|
||||
|
||||
Reference in new issue
Block a user