-
-
Save BiosBoy/88a670617a68e0dc33f8c3d394bb833b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Old way | |
| if (user && user.address && user.address.city) { | |
| console.log(user.address.city); | |
| } | |
| // Modern way with Optional Chaining | |
| console.log(user?.address?.city); | |
| // Nullish Coalescing | |
| const userName = user.name ?? 'Guest'; | |
| // Using an object | |
| const obj = {}; | |
| obj['key1'] = 'value1'; | |
| delete obj['key1']; | |
| // Using a Map | |
| const map = new Map(); | |
| map.set('key1', 'value1'); | |
| map.delete('key1'); | |
| const arr = [1, 2, 3, 4, 5]; | |
| // Using forEach | |
| arr.forEach(num => console.log(num)); | |
| // Using for loop | |
| for (let i = 0; i < arr.length; i++) { | |
| console.log(arr[i]); | |
| } | |
| // Dynamic import | |
| import('path/to/module').then(module => { | |
| module.default(); | |
| }); | |
| const list = document.getElementById('list'); | |
| // Inefficient | |
| list.style.backgroundColor = 'blue'; | |
| list.style.color = 'white'; | |
| // Efficient | |
| list.style.cssText = 'background-color: blue; color: white;'; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment