⚡ Lecture 2: Advanced Array Methods You’re Probably Misusing 🔹 Title: JavaScript Array Methods You Think You Know (But Don’t) 🔹 Post Content: Most developers use array methods. Very few understand them. Let’s go deeper: 1️⃣ find() vs filter() const users = [{id:1}, {id:2}, {id:3}]; const user = users.find(u => u.id === 2); 👉 Returns ONE object const usersList = users.filter(u => u.id === 2); 👉 Returns ARRAY Rule: Need one result? → find() Need multiple? → filter() 2️⃣ some() & every() – Boolean Logic const nums = [2,4,6]; nums.some(n => n > 5); // true nums.every(n => n > 1); // true Insight: These replace messy conditional loops. 3️⃣ includes() – Cleaner Checks const fruits = ["apple", "banana"]; fruits.includes("apple"); // true Stop writing unnecessary loops. 🚨 Brutal Truth: If your code has too many loops, you're not thinking functionally. 📌 Final Thought: Clean code isn’t about fewer lines — it’s about better logic. 🔖 Hashtags: #JavaScriptTips #CleanCode #WebDev #ProgrammingLife #FrontendDeveloper #CodeBetter
JavaScript Array Methods You Think You Know
More Relevant Posts
-
Most JavaScript bugs aren’t caused by complex logic. They come from choosing the wrong array method. The difference between find() and filter(), map() and forEach(), or some() and every() looks small—until it reaches production. That’s where performance, readability, and hidden bugs start to matter. Examples: filter()[0] instead of find() → unnecessary full array scan Using forEach() when map() should return transformed data → broken data pipelines Calling sort() directly in React state → silent mutation bugs Using filter().length > 0 instead of some() → extra work for no reason Overusing reduce() for simple logic → clever code, poor readability Great developers don’t just know array methods. They know: ✔ When to use them ✔ When not to use them ✔ Their performance cost ✔ Their side effects ✔ Their production impact Simple rule: Code should not only work. It should be readable, predictable, and efficient. Because in real applications, small method choices create big system behavior. The engineers I respect most don’t use reduce() to look smart. They use find() instead of filter()[0]. They spread before sort(). They know forEach() returns nothing. That’s the difference between writing JavaScript and engineering with JavaScript. #JavaScript #FrontendDevelopment #WebDevelopment #ReactJS #SoftwareEngineering #Programming #CodeQuality #TechLeadership
To view or add a comment, sign in
-
-
📒 𝐉𝐚𝐯𝐚𝐒𝐜𝐫𝐢𝐩𝐭 𝐡𝐚𝐬 𝐚 𝐜𝐥𝐞𝐚𝐧𝐞𝐫 𝐫𝐮𝐧𝐧𝐢𝐧𝐠 𝐢𝐧 𝐭𝐡𝐞 𝐛𝐚𝐜𝐤𝐠𝐫𝐨𝐮𝐧𝐝… 𝐚𝐧𝐝 𝐦𝐨𝐬𝐭 𝐝𝐞𝐯𝐞𝐥𝐨𝐩𝐞𝐫𝐬 𝐢𝐠𝐧𝐨𝐫𝐞 𝐢𝐭. 𝑌𝑒𝑠, 𝐼’𝑚 𝑡𝑎𝑙𝑘𝑖𝑛𝑔 𝑎𝑏𝑜𝑢𝑡 𝐺𝑎𝑟𝑏𝑎𝑔𝑒 𝐶𝑜𝑙𝑙𝑒𝑐𝑡𝑖𝑜𝑛 (𝐺𝐶) ♻️ Every time you create: ✅ objects ✅ arrays ✅ functions 𝐽𝑎𝑣𝑎𝑆𝑐𝑟𝑖𝑝𝑡 𝑎𝑙𝑙𝑜𝑐𝑎𝑡𝑒𝑠 𝑚𝑒𝑚𝑜𝑟𝑦. 𝐵𝑢𝑡 𝑡ℎ𝑒 𝑟𝑒𝑎𝑙 𝑞𝑢𝑒𝑠𝑡𝑖𝑜𝑛 𝑖𝑠… When does JS free that memory? ➡️ Only when the value becomes unreachable. 🔥 Reachable = Alive If something can be accessed from: 1️⃣ global variables 2️⃣ current function variables 3️⃣ call stack …it stays in memory. Example: 𝗹𝗲𝘁 𝘂𝘀𝗲𝗿 = { 𝗻𝗮𝗺𝗲: "𝗝𝗼𝗵𝗻" }; 𝘂𝘀𝗲𝗿 = 𝗻𝘂𝗹𝗹; Now no one can access it → GC removes it. ⚡ 𝐵𝑖𝑔𝑔𝑒𝑠𝑡 𝑚𝑖𝑠𝑐𝑜𝑛𝑐𝑒𝑝𝑡𝑖𝑜𝑛: “Objects are deleted when they have no references.” Not exactly. Because two objects can reference each other and STILL get deleted. 📌 If the main root reference is gone, the whole connected group becomes an unreachable island → GC clears everything. 🚀 𝑊ℎ𝑦 𝑠ℎ𝑜𝑢𝑙𝑑 𝑦𝑜𝑢 𝑐𝑎𝑟𝑒? Because this is how memory leaks happen: ❌ event listeners not removed ❌ unused variables still holding references ❌ large objects stored unnecessarily Result: 🐢 slow apps 💥 crashes 📉 poor performance 💡 Simple rule: If you don’t need it → remove the reference. Because JavaScript will clean it… only after you let go. #JavaScript #WebDevelopment #Frontend #ReactJS #Programming #Coding #SoftwareEngineering #Tech #Learning #interview
To view or add a comment, sign in
-
𝗝𝗮𝘃𝗮𝗦𝗰𝗿𝗶𝗽𝘁 𝗦𝗵𝗮𝗹𝗹𝗼𝘄 𝗖𝗼𝗽𝘆 𝘃𝘀 𝗗𝗲𝗲𝗽 𝗖𝗼𝗽𝘆 📦 If you’ve ever updated state and something weird happened… this might be why 👇 🔹 Shallow Copy → copies only the first level 🔹 Nested objects are still referenced (same memory) Example: ➡️ Using { ...obj } or Object.assign() 💡 Problem: Change a nested value… and you might accidentally mutate the original object 😬 🔹 Deep Copy → copies everything (all levels) 🔹 No shared references 🔹 Safe to modify without side effects Example: ➡️ structuredClone(obj) ➡️ or libraries like lodash ⚠️ The common pitfall: You think you made a copy: ➡️ { ...user } But inside: ➡️ user.address.city is STILL linked So when you update it: ❌ You mutate the original state ❌ React may not re-render correctly ❌ Bugs appear out of nowhere 🚀 Why this matters (especially in React): State should be immutable ➡️ Always create safe copies ➡️ Avoid hidden mutations ➡️ Keep updates predictable 💡 Rule of thumb: 🔹 Flat objects? → shallow copy is fine 🔹 Nested data? → consider deep copy Understanding this difference = fewer bugs + cleaner state management And yes… almost every developer gets burned by this at least once 😄 Sources: - JavaScript Mastery - w3schools.com Follow 👨💻 Enea Zani for more #javascript #reactjs #webdevelopment #frontend #programming #coding #developers #learnjavascript #softwareengineering #100DaysOfCode
To view or add a comment, sign in
-
-
🧠 Day 25 — JavaScript Destructuring (Advanced Use Cases) Destructuring isn’t just syntax sugar — it can make your code cleaner and more powerful 🚀 --- 🔍 What is Destructuring? 👉 Extract values from arrays/objects into variables --- ⚡ 1. Object Destructuring const user = { name: "John", age: 25 }; const { name, age } = user; --- ⚡ 2. Rename Variables const { name: userName } = user; console.log(userName); // John --- ⚡ 3. Default Values const { city = "Delhi" } = user; --- ⚡ 4. Nested Destructuring const user = { profile: { name: "John" } }; const { profile: { name } } = user; --- ⚡ 5. Array Destructuring const arr = [1, 2, 3]; const [a, b] = arr; --- ⚡ 6. Skip Values const [first, , third] = arr; --- 🚀 Why it matters ✔ Cleaner and shorter code ✔ Easier data extraction ✔ Widely used in React (props, hooks) --- 💡 One-line takeaway: 👉 “Destructuring lets you pull out exactly what you need, cleanly.” --- Master this, and your code readability improves instantly. #JavaScript #Destructuring #WebDevelopment #Frontend #100DaysOfCode 🚀
To view or add a comment, sign in
-
🧠 JavaScript Array Methods — Complete Cheat Sheet While working with JavaScript daily, I realized one thing… 👉 Strong fundamentals = Faster development + Better code So I created a quick breakdown of the most useful array methods 👇 🔹 Creation Create arrays from different sources → Array.from(), Array.of(), Array.isArray() 🔹 Add / Remove Modify array elements → push(), pop(), shift(), unshift() 🔹 Modify Control structure of arrays → splice() (mutates) → slice() (non-mutating) 🔹 Searching Find values quickly → indexOf(), includes() 🔹 Find ( Important) → find(), findIndex() → findLast(), findLastIndex() 🔹 Transform / Loop → map() → transform data → filter() → select data → reduce() → build single result 🔹 Conditions → some() → at least one true → every() → all true 🔹 Sorting → sort() (mutates) → toSorted() (immutable) 🔹 Flatten / Combine → concat(), flat(), flatMap() 🔹 Modern ( Must Know) → toSpliced(), toReversed(), with() 👉 Immutable operations = cleaner code 🔹 Access → at() (supports negative index 👀) 💡 Key Learning: JavaScript arrays are not just lists — they are powerful tools to write clean, efficient, and scalable code. Understanding when to use: → map vs forEach → filter vs find → mutable vs immutable methods …can completely change your coding style 🚀 📌 Tip: Start using more immutable methods — they help avoid bugs in large applications. Which array method do you use the most? 🤔 #JavaScript #WebDevelopment #FrontendDevelopment #Coding #SoftwareDevelopment
To view or add a comment, sign in
-
-
Stop writing JavaScript like it’s still 2015. 🛑 The language has evolved significantly, but many developers are still stuck using clunky, outdated patterns that make code harder to read and maintain. If you want to write cleaner, more professional JS today, start doing these 3 things: **1. Embrace Optional Chaining (`?.`)** Stop nesting `if` statements or using long logical `&&` chains to check if a property exists. Use `user?.profile?.name` instead. It’s cleaner, safer, and prevents those dreaded "cannot read property of undefined" errors. **2. Master the Power of Destructuring** Don't repeat yourself. Instead of calling `props.user.name` and `props.user.email` five times, extract them upfront: `const { name, email } = user;`. It makes your code more readable and your intent much clearer. **3. Use Template Literals for Strings** Stop fighting with single quotes and `+` signs. Use backticks (`` ` ``) to inject variables directly into your strings. It handles multi-line strings effortlessly and makes your code look significantly more modern. JavaScript moves fast—make sure your coding habits are moving with it. What’s one JS feature you can’t live without? Let’s chat in the comments! 👇 #JavaScript #WebDevelopment #CodingTips #SoftwareEngineering #Frontend #Programming
To view or add a comment, sign in
-
🚀 **Day 4 – Scope Chain & Lexical Environment in JavaScript** In Day 3, we learned about Execution Context… But now the real question is: 👉 **How does JavaScript find variables when executing code?** 🤔 Let’s understand 👇 --- 💡 **What is Scope?** Scope defines **where a variable can be accessed** 👉 Simple: Scope = where is variable available ? --- 💡 **What is Scope Chain?** When JavaScript tries to access a variable: 👉 It searches in this order: * Current scope * Parent scope * Global scope 👉 This is called **Scope Chain** --- 💡 **Example:** ```js let name = "Aman"; function outer() { let city = "Indore"; function inner() { console.log(name); console.log(city); } inner(); } outer(); ``` --- 💡 **Behind the scenes:** When `inner()` runs: * looks for `name` → not in inner * goes to parent → not found * goes to global → found * looks for `city` → found in outer 👉 JavaScript climbs the **scope chain** --- 💡 **What is Lexical Environment?** 👉 It means: Scope is decided by where code is written, not where it is called --- ⚡ **Key Insight** JavaScript uses: * Scope * Scope Chain * Lexical Environment 👉 to resolve variables --- 💡 **Why this matters?** Because this is the base of: * Closures * Variable access * Debugging scope issues --- 👨💻 Continuing my JavaScript fundamentals series 👉 Next: **Hoisting (most misunderstood concept)** 👀 #JavaScript #WebDevelopment #FrontendDevelopment #Coding #SoftwareEngineer #Tech
To view or add a comment, sign in
-
-
🤔 Wait… shouldn’t variables be deleted? How is it possible that a function still remembers a variable…even after the function where it was created has already finished executing? 😳 👉 Sounds impossible, right? 🚀 JavaScript’s one of the most powerful concepts: Closures 🧠 What is a Closure? A closure is created when a function is defined inside another function, and the inner function can access variables from its parent scope — even after the parent function has finished execution. ⚡Shouldn’t variables be deleted? Normally, yes ✅ 👉 Once a function finishes, its local variables are removed from memory (thanks to garbage collection) But closures change the game 👇 👉 If an inner function is still using those variables, JavaScript keeps them alive in memory 🔍 What’s really happening? The inner function “closes over” its parent’s variables 💡 That’s why it’s called a closure Even though the outer function is done… the inner function still has access to its scope 😮 🔐 Why Closures Matter (Real Use Cases) Closures aren’t just theory — they’re used everywhere: 👉 Encapsulation (Private Variables) Keep data hidden from global scope 👉 Debouncing & Throttling Control function execution (very common in React apps) 👉 State management patterns Maintain data across function calls 💡 Real Developer Insight Closures are powerful… but can be tricky 👉They can also hold memory longer than expected 👉 Misuse can lead to memory leaks 🚀 Final Thought: Most developers use closures unknowingly… But great developers understand how and why they work. #JavaScript #FrontendDevelopment #WebDevelopment #Closures #CodingTips #LearnJavaScript #BuildInPublic #100DaysOfCode #LearnInPublic
To view or add a comment, sign in
-
-
🚀 Day 20 – Deep vs Shallow Copy in JavaScript Ever changed a copied object… and accidentally modified the original too? 😅 Yeah, that’s the shallow copy trap. Let’s fix that today 👇 🔹 Shallow Copy Copies only the first level 👉 Nested objects still share the same reference 🔹 Deep Copy Creates a fully independent clone 👉 No shared references, no unexpected bugs 💡 Real-world example (Angular devs 👇) When working with forms, APIs, or state (NgRx), a shallow copy can silently mutate your original data — leading to hard-to-debug UI issues. ⚡ Best Ways to Deep Copy ✔️ structuredClone() (modern & recommended) ✔️ JSON.parse(JSON.stringify(obj)) (with limitations) ✔️ _.cloneDeep() (lodash) 🔥 TL;DR Shallow Copy → Shares references Deep Copy → Fully independent Prefer structuredClone() whenever possible 💬 Have you ever faced a bug because of shallow copying? Drop your experience 👇 #JavaScript #Angular #WebDevelopment #Frontend #Programming #100DaysOfCode
To view or add a comment, sign in
-
-
𝗝𝗮𝘃𝗮𝗦𝗰𝗿𝗶𝗽𝘁 𝗠𝗮𝗽() 𝘃𝘀 𝗙𝗼𝗿𝗘𝗮𝗰𝗵() 🔥 Confused about when to use map() and when to use forEach()? Here’s the difference that every JavaScript developer should know 👇 🔹 forEach() → executes a function for every element 🔹 It does NOT return a new array 🔹 Mainly used for side effects (logging, API calls, DOM updates) Example: ➡️ arr.forEach(item => console.log(item)) 🔹 map() → also loops through every element 🔹 BUT returns a brand new array 🔹 Perfect for transforming data Example: ➡️ const names = users.map(user => user.name) ⚠️ The common mistake: Using forEach() when you actually need transformed data 😬 Example: ❌ const result = arr.forEach(item => item * 2) Why? Because forEach() returns: ➡️ undefined Correct way: ✅ const result = arr.map(item => item * 2) 💡 Simple rule: Need a new array? ➡️ use map() Need to just “do something” with each item? ➡️ use forEach() That small difference can save you from weird bugs and messy code. 🚀 Understanding this = cleaner logic + better interviews + stronger JavaScript fundamentals If you're learning JS, React, or preparing for technical interviews… this is essential. 📚 Sources: • JavaScript Mastery • w3schools.com Follow for more: 👨💻 Enea Zani #javascript #webdevelopment #frontend #reactjs #coding #developers #programming #100DaysOfCode #learnjavascript #softwareengineer
To view or add a comment, sign in
-
More from this author
Explore content categories
- Career
- Productivity
- Finance
- Soft Skills & Emotional Intelligence
- Project Management
- Education
- Technology
- Leadership
- Ecommerce
- User Experience
- Recruitment & HR
- Customer Experience
- Real Estate
- Marketing
- Sales
- Retail & Merchandising
- Science
- Supply Chain Management
- Future Of Work
- Consulting
- Writing
- Economics
- Artificial Intelligence
- Employee Experience
- Workplace Trends
- Fundraising
- Networking
- Corporate Social Responsibility
- Negotiation
- Communication
- Engineering
- Hospitality & Tourism
- Business Strategy
- Change Management
- Organizational Culture
- Design
- Innovation
- Event Planning
- Training & Development