Skip to content

Instantly share code, notes, and snippets.

@BiosBoy
Created August 7, 2024 10:10
Show Gist options
  • Select an option

  • Save BiosBoy/88a670617a68e0dc33f8c3d394bb833b to your computer and use it in GitHub Desktop.

Select an option

Save BiosBoy/88a670617a68e0dc33f8c3d394bb833b to your computer and use it in GitHub Desktop.
// 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