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
| class Singleton { | |
| constructor() { | |
| if (Singleton.instance) { | |
| return Singleton.instance; | |
| } | |
| this.data = []; | |
| Singleton.instance = this; | |
| } | |
| getData() { |
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
| # Code Examples from "Top 5 Underestimated Native Browser Features You Should Be Using" | |
| ## 1. Bluetooth: Connecting to Devices Wirelessly 📡 | |
| ### Example: Connecting to a Bluetooth Device | |
| ```typescript | |
| async function connectToBluetooth() { | |
| try { |
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
| function Person(name) { | |
| this.name = name; | |
| } | |
| Person.prototype.greet = function() { | |
| console.log(`Hello, my name is ${this.name}`); | |
| }; | |
| const alice = new Person('Alice'); |
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
| const userRole = 'admin'; | |
| if (userRole === 'admin' || userRole === 'editor' || userRole === 'moderator') { | |
| console.log('Access granted'); | |
| } else { | |
| console.log('Access denied'); | |
| } | |
| const userRole = 'admin'; |
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
| # Webpack Bundle Analyzer Examples | |
| ## Install the Package | |
| ```bash | |
| npm install --save-dev webpack-bundle-analyzer | |
| const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin; |
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
| npm install apollo-server-express graphql | |
| const express = require('express'); | |
| const { ApolloServer, gql } = require('apollo-server-express'); | |
| // Define your type definitions (schema) | |
| const typeDefs = gql` | |
| type Query { | |
| hello: String | |
| } |
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
| const arr1 = [1, 2, 3, 4]; | |
| const result1 = arr1.flatMap(x => [x, x * 2]); | |
| console.log(result1); // [1, 2, 2, 4, 3, 6, 4, 8] | |
| const arr2 = ['a', 'b', 'c', 'd']; | |
| console.log(arr2.at(-1)); // 'd' | |
| console.log(arr2.at(-2)); // 'c' | |
| const arr3 = [1, 2, 3, 4, 5]; | |
| const result2 = arr3.findLast(x => x % 2 === 0); |
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
| const originalArray = [1, 2, 3, 4, 5]; | |
| const slicedArray = originalArray.slice(2, 4); | |
| console.log(slicedArray); // [3, 4] | |
| console.log(originalArray); // [1, 2, 3, 4, 5] | |
| const originalArray = [1, 2, 3, 4, 5]; | |
| const splicedArray = originalArray.splice(2, 2); |
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
| interface User { | |
| id: number; | |
| name: string; | |
| email: string; | |
| } | |
| function updateUser(userId: number, updates: Partial<User>) { | |
| // Imagine this function updates a user in the database | |
| console.log(userId, updates); | |
| } |
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'; |
NewerOlder