π‘JavaScript Tips:
Remove duplicates using Set:
const unique = [...new Set([1, 2, 2, 3, 3, 4])] // [1, 2, 3, 4]
@faisalakbar.bsky.social
Full Stack Problem Solver | Data Storyteller in the making | Tech explorer bridging ideas and implementation Currently @ Bank of America Blogging @ dsfaisal.com
π‘JavaScript Tips:
Remove duplicates using Set:
const unique = [...new Set([1, 2, 2, 3, 3, 4])] // [1, 2, 3, 4]
π‘JavaScript Tips:
Optional Chaining for Safe Property Access
const user = {
address: {
street: "123 Main St"
}
}
// Old way
const street = user && user.address && user.address.street
// Modern way
const street = user?.address?.street
π‘JavaScript Tips:
Nullish Coalescing vs Logical OR
Common use cases for stacks include:
- Function call management (call stack)
- Undo/Redo operations
- Expression evaluation
- Browser history
- Syntax parsing
The implementation I provided includes all basic stack operations:
- push(): Add element to top
- pop(): Remove and return top element
- peek(): View top element without removing
- isEmpty(): Check if stack is empty
- getSize(): Get number of elements
- clear(): Remove all elements
Implementation of stack using JavaScript:
05.01.2025 22:47 β π 1 π 0 π¬ 1 π 0Key characteristics of a stack:
- Items can only be added/removed from one end (the top)
- The last element added is the first one to be removed
- Access to elements in the middle is not allowed
π Understanding Stack Data Structure
A Stack is a linear data structure that follows LIFO (Last In First Out).
Think of it like a stack of plates - you can only:
1. Add (push) a plate to the top
2. Remove (pop) a plate from the top
3. Look at (peek) the top plate without removing it
Key benefits:
β Building reusable components with default styles
β Allowing style overrides via props
β Handling conditional classes
β Ensuring Tailwind classes don't conflict
Here's a practical example:
02.01.2025 20:44 β π 0 π 0 π¬ 1 π 0Explanation of cn utility function from #shadcn/ui
The cn function combines two popular utilities:
1οΈβ£ clsx - For conditionally joining CSS class names
2οΈβ£ tailwind-merge - For merging Tailwind CSS classes intelligently, removing conflicts
π useTransition() - The performance hook you need:
β’ Non-blocking state updates
β’ Background processing
β’ Loading state management
β’ Concurrent rendering ready
#react
π― useEffect Cleanup Cheatsheet:
β’ Timers: clearInterval
β’ Events: removeEventListener
β’ Subscriptions: unsubscribe
β’ Connections: disconnect
β’ API: cancel request
π Why useEffect Cleanup?
No cleanup = Memory leaks
Missing cleanup = Bugs
Bad cleanup = Weird behavior
#ReactJS #javascript
π React Hooks: useEffect vs useLayoutEffect
useEffect:
β’ Runs after paint
β’ Async, non-blocking
β’ Use for: data fetching, subscriptions
β’ Default choice for most cases
useLayoutEffect:
β’ Runs before paint
β’ Sync, blocking
β’ Use for: DOM measurements, preventing flickers
β’ Performance sensitive
I use carbon.now.sh
28.12.2024 00:31 β π 2 π 0 π¬ 0 π 0It depends, useEffect without deps is perfectly fine if you're not updating state. It will run after every render - that's sometimes exactly what you want. Here are some example:
28.12.2024 00:10 β π 1 π 0 π¬ 1 π 0π― Let's understand useEffect's dependency array in React:
27.12.2024 23:44 β π 0 π 0 π¬ 1 π 0Itβs simple, you just need to understand what and when to use dependencies:
27.12.2024 17:51 β π 1 π 0 π¬ 1 π 0π― useEffect() Simplified:
// Run once
useEffect(() => {}, [])
// Run on changes
useEffect(() => {}, [data])
// Run & cleanup
useEffect(() => {
return () => cleanup()
}, [])
No magic, just timing!
#ReactJS #javascript
Yes those are most common use cases.
26.12.2024 18:47 β π 0 π 0 π¬ 0 π 0π useRef() Cheat Sheet:
const ref = useRef(initialValue)
3 Things to Remember:
β’ It's mutable (ref.current)
β’ Changes are instant
β’ Won't trigger re-renders
Perfect for:
π DOM references
β±οΈ Timers
ποΈ Previous values
#React #JavaScript
π React useState() Cheatsheet:
const [data, setData] = useState(initialValue)
3 Rules to Remember:
1. State updates trigger re-renders
2. State updates are async
3. Previous state? Use callback:
setData(prev => prev + 1)
#react #javascript
π§Έ useState() explained to a 5-year-old:
It's like a toy box:
- box = your state
- toys = your data
- replacing toys = setState
When you put in new toys (setState),
React shows everyone your new toys (re-render)!
#React
πͺ React Hooks Explained Simply:
useCallback vs useMemo
useCallback: Memoizes a FUNCTION
const fn = useCallback(() => doSomething(), [deps])
useMemo: Memoizes a VALUE
const value = useMemo(() => computeValue(), [deps])
#React #JavaScript
π Tech Interview Prep:
Key difference:
Pass by Value - Function can't modify original (primitive types)
Pass by Reference - Function can modify original (reference types)
π JavaScript Pro Tip:
Stop concatenating currency symbols! Use Intl.NumberFormat for proper formatting:
#javascript #webdev
JavaScript Tip π‘:
Two ways to group by JavaScript with zero dependencies:
β
using reduce()
β
using Object.groupBy()
π‘JavaScript Tip:
map() is an array method in JavaScript that:
- Creates a new array
- Transforms each element of the original array
- Returns a new array of the same length
- Does NOT modify the original array