💡 Finding the Middle of a Linked List — The Clean JavaScript Way When working with linked lists, one of the classic questions is: “How do you find the middle node?” The obvious solution is to: 1️⃣ Traverse once to count all nodes. 2️⃣ Traverse again to reach the middle. That works — but it’s not optimal. 🚀 Here’s the elegant two-pointer technique in JavaScript: function findMiddle(head) { let slow = head; let fast = head; while (fast !== null && fast.next !== null) { slow = slow.next; fast = fast.next.next; } return slow; // middle node } ✨ The idea: fast moves twice as fast as slow. When fast hits the end, slow is right in the middle. One pass. Clean logic. No extra space. This trick isn’t just useful for linked lists — it’s a great reminder that thinking in pointers often leads to the most elegant algorithms. #JavaScript #DataStructures #Algorithms #CodingTips #LinkedList #ProblemSolving #TechLearning
Finding Middle Node in Linked List with JavaScript
More Relevant Posts
-
💡 Shallow Copy vs Deep Copy — and a Hidden Browser API You Should Know 🚀 Ever faced this situation? You copy an object or array… and somehow, the original still changes. 😵💫 That’s the trap between shallow and deep copies in JavaScript 👇 🧩 Shallow Copy A shallow copy only duplicates the first level. Nested objects or arrays still share the same reference. 🧠 Tools that create shallow copies: Object.assign(), spread syntax (...), Array.slice() 🌊 Deep Copy A deep copy makes a completely new clone of all nested levels, so changes never affect the original. ✅ 1. Using the Modern Browser API — structuredClone() Deeply clones objects, arrays, Maps, Sets, Dates, etc... but removes methods/function it's always better to go for recursive deep copy it will take care functions. Supported in modern browsers (Chrome 98+, Node 17+) ✅ 2. Recursive Deep Copy (for custom control) #JavaScript #Frontend #WebDevelopment #CodingTips #structuredClone #DeepCopy #ShallowCopy #TechLearning
To view or add a comment, sign in
-
-
Understanding the structuredClone method structuredClone() — The Modern Way to Deep Copy in JavaScript Concept: structuredClone() is a native JavaScript method that creates a deep copy of objects, arrays, Maps, Sets, and more — without needing external libraries or complex hacks. 💻 Example: const original = { a: 1, b: { c: 2 } }; const copy = structuredClone(original); copy.b.c = 3; console.log(original.b.c); // 2 (remains unchanged) console.log(copy.b.c); // 3 Why It Matters: ✅ Deep copies objects safely (no shared references) 🚫 No need for JSON.parse(JSON.stringify(...)) hacks 🔄 Handles circular references gracefully ⚡ Native browser API — no dependencies! 🎨 #JavaScript #WebDev #FrontendTips #StructuredClone #JSDeepCopy #CodingTips #WebDevelopment
To view or add a comment, sign in
-
🚀 JavaScript Quick Tip: Type Coercion Made Simple! Have you ever wondered why: "1" + 2 // gives "12" "1" - 2 // gives -1 🤔 Let’s decode it 👇 JavaScript automatically converts values when needed — this is called Type Coercion. Here’s the simple rule to remember: 🧠 + → tries to make things strings (joins values together) -, *, / → try to make things numbers (do math operations) 💡 Pro Tip: Always be clear about data types — it’ll save you from some tricky JavaScript bugs! #javascript #webdevelopment #codingtips #typecoercion #frontend
To view or add a comment, sign in
-
Today I explored how JavaScript executes inside the browser, and it was truly fascinating to understand the step-by-step process behind the scenes! 💡 Here’s what I learned 👇 🔹 The browser starts by loading the HTML and identifying any <script> tags. 🔹 Once found, the code is sent to the JavaScript Engine (like Chrome’s V8). 🔹 The engine performs three key steps — ✨ Parsing: Reads and converts code into an Abstract Syntax Tree (AST). ⚙️ Compilation (JIT): Translates JS into optimized machine code for faster execution. 🧩 Execution: Runs the code in the Call Stack, while variables and objects are managed in the Memory Heap. 🔹 For asynchronous operations (set Timeout, fetch, etc.), the Web APIs, Callback Queue, and Event Loop coordinate to ensure non-blocking execution. 💬 In short: HTML Parsing → JS Engine → Call Stack → Web APIs → Callback Queue → Event Loop → Execution Understanding this flow helps in writing efficient, optimized, and clean JavaScript code. Excited to continue learning and sharing my progress each day under the guidance of Sudheer Velpula Sir. 🙌 #JavaScript #WebDevelopment #Frontend #LearningJourney #Coding #SudheerSir
To view or add a comment, sign in
-
-
🚀 Mastering JavaScript String Methods Understanding String Methods is one of the fastest ways to level up your JavaScript skills. From searching, slicing, trimming, transforming text — string functions make data handling super easy and powerful. This chart gives a quick snapshot of commonly used methods like: ✔️ charAt() ✔️ concat() ✔️ startsWith() / endsWith() ✔️ includes() ✔️ indexOf() ✔️ slice() / substring() ✔️ match() ✔️ replace() ✔️ repeat() ✔️ trim() ✔️ toLowerCase() / toUpperCase() … and more! 📌 If you're learning JavaScript or improving your frontend skills, mastering these methods is a must. 💡Pro tip: Don't just memorize these - practice them in small projects to build muscle memory. #JavaScript #WebDevelopment #Coding #Frontend #Learning #StringMethods #MERNStack #JavaScriptTips
To view or add a comment, sign in
-
-
🔥 JS Loops Demystified: for vs for…in vs for…of If you’ve ever wondered which loop to use in JavaScript, you’re not alone. Choosing the wrong one can cause subtle bugs! Here’s a quick guide: 1️⃣ Classic for loop – The old reliable const arr = [10, 20, 30]; for (let i = 0; i < arr.length; i++) { console.log(arr[i]); } Use when you need index access, or want full control over iteration. 2️⃣ for…in loop – Iterate over keys (property names) const arr = [10, 20, 30]; for (let key in arr) { console.log(key); // 0, 1, 2 } Works on objects too. ⚠️ Can be dangerous for arrays — also loops over inherited properties. 3️⃣ for…of loop – Iterate over values const arr = [10, 20, 30]; for (let value of arr) { console.log(value); // 10, 20, 30 } Perfect for arrays, strings, maps, sets. Safe, clean, and readable. 💡 Pro tip: Use for when you need the index. Use for…of for values (most array iterations). Avoid for…in on arrays — reserve it for objects. Small choice, huge impact on readability and bug prevention. #JavaScript #CodingTips #WebDevelopment #FrontendDev #CleanCode #JSProTips #ForOfVsForIn
To view or add a comment, sign in
-
🚀 Deep Clone an Object in JavaScript (the right way!) Most of us have tried this at least once: const clone = JSON.parse(JSON.stringify(obj)); …and then realized it breaks when the object has functions, Dates, Maps, or undefined values 😬 Let’s fix that 👇 ❌ The Problem with JSON.parse(JSON.stringify()) >It’s quick, but it: >Removes functions >Converts Dates into strings >Skips undefined, Infinity, and NaN >Fails for Map, Set, or circular references ✅ Option 1: Use structuredClone() (Modern & Fast) Available in most modern browsers and Node.js (v17+). It can handle Dates, Maps, Sets, and even circular references — no errors, no data loss. const deepCopy = structuredClone(originalObject); Simple, native, and reliable 💪 ✅ Option 2: Write Your Own Recursive Deep Clone Useful for older environments or if you want to understand the logic behind cloning. 💡 Pro Tip: If you’re working with complex or nested data structures, always prefer structuredClone(). It’s the modern, built-in, and safest way to deep clone objects in JavaScript. 🔥 Found this useful? 👉 Follow me for more JavaScript deep dives made simple — one post at a time. #JavaScript #WebDevelopment #FrontendDevelopment #CodingTips #LearnJavaScript #Programming #DeveloperCommunity #WebDev #ES6 #JSDeveloper #JavaScriptTips #JavaScriptObjects #JavaScriptClone #JavaScriptCloneObject
To view or add a comment, sign in
-
🚀 𝗗𝗲𝗲𝗽 𝗖𝗹𝗼𝗻𝗲 𝗢𝗯𝗷𝗲𝗰𝘁𝘀 𝗶𝗻 𝗝𝗮𝘃𝗮𝗦𝗰𝗿𝗶𝗽𝘁 (𝘁𝗵𝗲 𝗥𝗜𝗚𝗛𝗧 𝘄𝗮𝘆) Most of us have cloned objects at some point using: const clone = JSON.parse(JSON.stringify(obj)); But… this method silently breaks things 😬 It: ❌ Removes functions ❌ Converts Date objects into strings ❌ Loses undefined, NaN, and Infinity ❌ Completely fails with Map, Set, or circular references So what’s the better approach? ✅ Option 1: Use structuredClone() Modern, fast, and now available in most browsers + Node.js (v17+). It correctly handles: • Dates • Maps • Sets • Circular references No fuss. No polyfills. Just works. ✅ Option 2: Write your own deep clone (for learning) A recursive deep clone function helps understand how object copying really works. (Sharing my implementation in the code snippet images above 👆) ⚡ Pro Tip: If you're dealing with complex nested objects, just use structuredClone(). It’s native, efficient, and avoids hours of debugging later. 🔥 If you found this helpful, 👉 Follow me for more bite-sized JavaScript insights. Let’s learn smart, not hard 🚀 #JavaScript #WebDevelopment #Frontend #NodeJS #CodeTips
To view or add a comment, sign in
-
-
🤯 𝐓𝐡𝐞 𝐕𝐚𝐫𝐢𝐚𝐛𝐥𝐞 𝐕𝐢𝐛𝐞 𝐂𝐡𝐞𝐜𝐤: 𝒗𝒂𝒓 𝐯𝐬. 𝒍𝒆𝒕 𝐯𝐬. 𝒄𝒐𝒏𝒔𝒕 If you've ever felt like your JavaScript variables were causing trouble, you might be right! Choosing the wrong declaration keyword can lead to bugs, leaks, and general chaos. Here is the quick, no-nonsense guide to who these three are and when to use them: 1. 𝒗𝒂𝒓(𝐓𝐡𝐞 𝐓𝐫𝐨𝐮𝐛𝐥𝐞𝐦𝐚𝐤𝐞𝐫) 🚫 𝐒𝐜𝐨𝐩𝐞: 𝐅𝐮𝐧𝐜𝐭𝐢𝐨𝐧-𝐬𝐜𝐨𝐩𝐞𝐝 (it leaks out of loops and blocks). 𝐑𝐞-𝐚𝐬𝐬𝐢𝐠𝐧𝐦𝐞𝐧𝐭: Allowed. 𝐓𝐡𝐞 𝐋𝐞𝐬𝐬𝐨𝐧: A legacy keyword. 𝐃𝐨𝐧'𝐭 𝐮𝐬𝐞 𝐢𝐭 in modern code. 2. 𝒍𝒆𝒕 (𝐓𝐡𝐞 𝐅𝐥𝐞𝐱𝐢𝐛𝐥𝐞 𝐎𝐧𝐞) 📝 𝐒𝐜𝐨𝐩𝐞: 𝐁𝐥𝐨𝐜𝐤-𝐬𝐜𝐨𝐩𝐞𝐝 ({ } are its boundaries). 𝐑𝐞-𝐚𝐬𝐬𝐢𝐠𝐧𝐦𝐞𝐧𝐭: Allowed (you can change the value). 𝐓𝐡𝐞 𝐋𝐞𝐬𝐬𝐨𝐧: Use it for variables where the value 𝐧𝐞𝐞𝐝𝐬 𝐭𝐨 𝐜𝐡𝐚𝐧𝐠𝐞 (like a counter in a loop or a score). 3. 𝒄𝒐𝒏𝒔𝒕 (𝐓𝐡𝐞 𝐃𝐞𝐟𝐚𝐮𝐥𝐭 𝐂𝐡𝐨𝐢𝐜𝐞) ✅ 𝐒𝐜𝐨𝐩𝐞: 𝐁𝐥𝐨𝐜𝐤-𝐬𝐜𝐨𝐩𝐞𝐝. 𝐑𝐞-𝐚𝐬𝐬𝐢𝐠𝐧𝐦𝐞𝐧𝐭: 𝐍𝐨𝐭 𝐚𝐥𝐥𝐨𝐰𝐞𝐝 (the binding is permanent, though object properties can still change). 𝐓𝐡𝐞 𝐋𝐞𝐬𝐬𝐨𝐧: This should be your 𝐝𝐞𝐟𝐚𝐮𝐥𝐭 𝐜𝐡𝐨𝐢𝐜𝐞 for almost everything. Use it unless you know the value will be updated. 𝐓𝐡𝐞 𝐆𝐨𝐥𝐝𝐞𝐧 𝐑𝐮𝐥𝐞: Start with const. If you get an error because you need to update the variable later, and only then, switch to let. Save yourself the debugging headache! What percentage of the time do you find yourself using const? I'm aiming for 90% plus! #JavaScript #WebDevTips #CodingBestPractices #FrontEndDevelopment #constletvar
To view or add a comment, sign in
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