What is the correct output ? function addsum() { let sum = 0; for (let i = 0; i < arr.lenght; i++) { sum = sum + arr[i]; } return sum; } const arr = [1, 2, 3]; console.log(addsum(arr)); Everyone thinks its very simple and output is 6 ✅ Most people glance at this and immediately say its 6. We read code the same way we read a newspaper. Our brain auto-corrects mistakes without us realizing it. Your eyes didn’t catch it. and move on. Your mind assumed it was correct. You saw "length". But the code actually has "lenght". - Don’t trust your eyes. - Don’t trust assumptions. - Trust execution and attention to detail. - On the first read, your brain auto-corrects the mistake; on the second read,your eyes finally notice the error. #JavaScript #ReactJS #FrontendDevelopment #WebDevelopment #SoftwareEngineering #Debugging #CleanCode #DeveloperMindset #CodingMistakes #Programming #BugHunting #InterviewQuestions #JavaScriptInterview #TechCareers #LearnToCode #CodeQuality #ReactJS #JSX #WebSecurity #XSS #FrontendTips #JavaScript #SafeCoding #ReactInterview
JavaScript Bug: Auto-Corrected Mistake in Length Property
More Relevant Posts
-
Most developers use Mapped Types. Few realize they can use them to 'filter' keys on the fly. Imagine you have a type with 50 properties. You need a new type that only includes keys matching a specific pattern, like those containing the word 'Id'. Not keys you manually list. Not keys you hardcode. But keys that match a rule. This is where selective remapping in mapped types shines. We know we can use the 'as' keyword to rename keys. But the real 'pro move' is knowing what happens when you remap a key to 'never.' In TypeScript, if a key is remapped to 'never', it is removed from the resulting type entirely. And that's an important point to keep in mind because this is what we can use to do a selective remapping in mapped types. By iterating over every key and applying a conditional check: 1. If the key matches your rule - Keep it. 2. If it doesn’t - Remap to 'never.' This pattern is extremely useful in real-world scenarios where you need only specific prefixed keys or you’re building reusable utility types. When you're building utility types, API response handlers, or form schemas, you often need specific subsets of properties. This pattern lets you extract them programmatically instead of maintaining duplicate type definitions. Once you realize you can conditionally 'filter' keys during transformation, you stop fighting the type system and start making it work for you. #TypeScript #JavaScript #Programming #Coding #WebDevelopment
To view or add a comment, sign in
-
-
𝐐𝐮𝐢𝐜𝐤 𝐉𝐚𝐯𝐚𝐒𝐜𝐫𝐢𝐩𝐭 𝐂𝐡𝐚𝐥𝐥𝐞𝐧𝐠𝐞 – 𝐂𝐚𝐧 𝐘𝐨𝐮 𝐆𝐮𝐞𝐬𝐬 𝐭𝐡𝐞 𝐎𝐮𝐭𝐩𝐮𝐭? Sometimes it’s not about complex algorithms… It’s about understanding how small string methods work together. 👀 Here’s the snippet 👇 const result = ["genetech", "solutions"] .map(word => word.charAt(0).toUpperCase() + word.slice(1)) .join(" "); console.log(result); What will be the output? 🤔 A) genetech solutions B) GENETECH SOLUTIONS C) Genetech Solutions D) ["Genetech", "Solutions"] This one tests your understanding of: - map() - charAt() - toUpperCase() - slice() - join() Small fundamentals. Big difference. 💡 Drop your answer in the comments 👇 Let’s see who reads code carefully 😄 #JavaScript #WebDevelopment #FrontendDeveloper #CodingChallenge #LearnToCode #Programming #Developers #TechCommunity #JS #CodeDaily
To view or add a comment, sign in
-
-
In JavaScript, the switch statement is a clean and organized way to handle multiple scenarios from a single expression — perfect when you’d otherwise end up chaining several if/else statements. - Improves readability when there are many possible cases - Use case for each condition and break to avoid “falling through” to the next case - Include default to guarantee a fallback behavior - Great for menus, status, action types, simple routing, and value mapping 💡 Practical tip: when cases start getting complex, consider using functions or mapping objects to keep the code even more scalable. #JavaScript #JS #Frontend #WebDevelopment #Programming #CleanCode #DevTips #Coding #SoftwareDevelopment #Tech
To view or add a comment, sign in
-
-
Why it is recommended that "let" is used in for loops instead of "var" while output is same in both cases ? .......................... Because 1. Scope difference (main reason) var → function scoped let → block scoped With var, the variable exists outside the loop block. 2. Major difference with async code This is where let becomes very important. Using var 3. Prevents accidental bugs With var ✅ Rule used in modern JavaScript Use let for variables that change Use const for variables that don't change Avoid var in modern code #webdevelopment #webdev #webdeveloper #frontend #frontenddeveloper #backend #backenddeveloper #fullstack #fullstackdeveloper #coding #programming #javascript #html #css #reactjs #nodejs #webdesign #softwaredeveloper #tech #devlife
To view or add a comment, sign in
-
-
JavaScript isn’t evolving through hype anymore. It’s evolving through small, practical upgrades that quietly make your code cleaner, safer, and easier to reason about. From Object.hasOwn() and private class fields to toSorted() and Object.groupBy() to Promise.withResolvers() and Iterator Helpers… Modern JS is pushing toward: • Less mutation • Clearer intent • Safer async handling • More functional data processing And honestly? That’s the kind of evolution that makes senior engineers dangerous. Before you search for a solution, you first need to know it exists. Which of the modern features are you already using in production? #JavaScript #WebDevelopment #Frontend #NodeJS #ECMAScript #SoftwareEngineering #CleanCode #Programming #FullStack #DevCommunity
To view or add a comment, sign in
-
-
Simple code but yet explain the deep Js concept........................... Most people confidently say that the function overrides the variable. And tbh it's not even completely wrong. There's a little more to it though. You actually get a "TypeError" saying "a is not a function". That's because JavaScript runs in two phases, creation and execution. During the creation phase, function declarations are hoisted before var declarations. So initially, a points to the function. But once execution starts, this line runs: var a = 10; That assignment overwrites the function reference. So by the time we reach a(), we’re essentially doing 10(); And that's when it breaks. #JavaScript #JS #FrontendDevelopment #WebDevelopment #Programming #Coding #TechInterview #InterviewPreparation
To view or add a comment, sign in
-
-
⚠️ JavaScript Array “𝑴𝒆𝒎𝒐𝒓𝒚 𝑳𝒆𝒂𝒌” You Probably Didn’t 𝐍𝐨𝐭𝐢𝐜𝐞 constarr= [1, 2, 3, 4, 5]; arr[-1] =123; 🚨 // check console.log(arr.length); // 𝟓 😲 for (const element of arr) { console.log(element); // 𝟏𝟐𝟑 𝐢𝐬 𝐍𝐎𝐓 𝐢𝐭𝐞𝐫𝐚𝐭𝐞𝐝 😵 } Because in JavaScript: ---------------------------- Arrays are just 𝐬𝐩𝐞𝐜𝐢𝐚𝐥 objects with numeric indexes. 😬 -1 is not a 𝐯𝐚𝐥𝐢𝐝 array index, so JS treats it as a 𝐧𝐨𝐫𝐦𝐚𝐥 object key. #JavaScript #JS #WebDevelopment #Frontend #Backend #FullStack #SoftwareEngineering #Programming #Coding #Developer #DevTips #CodeQuality #CleanCode #BestPractices #Debugging #Bug #HiddenBug #Gotchas #ProgrammingTips #LearnToCode #CodeNewbie #NodeJS #V8 #ECMAScript #TechEducation #DailyCoding #100DaysOfCode #Performance #MemoryLeak #DataIntegrity #SoftwareBugs
To view or add a comment, sign in
-
Regular vs Arrow Functions: The Ultimate Showdown ⚔️.......................................... In JavaScript, regular functions and arrow functions may look alike, but their behavior is fundamentally different. Regular functions have dynamic this binding, can be used as constructors with new, and come with their own arguments object. Arrow functions, by contrast, lexically inherit this from their surrounding scope, cannot be used as constructors, and have no arguments object. Use regular functions for object methods and when you need dynamic context; opt for arrow functions in callbacks and to preserve this. Understanding these distinctions is key to writing cleaner, bug-free code! #javascript #webdev #coding #programming #functions #arrowfunctions #js #frontend #backend #developer #tech #webdevelopment
To view or add a comment, sign in
-
-
JavaScript isn’t truly “multithreaded”… it just fakes it brilliantly. The Event Loop is the brain behind that illusion. Here’s the real game happening under the hood: • Call Stack → Executes synchronous code first • Microtask Queue → Promises, queueMicrotask, process.nextTick() • Macrotask Queue → setTimeout, setInterval, I/O, UI events The rule most devs miss: Microtasks always run before Macrotasks. Which leads to something interesting called Starvation. If microtasks keep getting added endlessly (like chained Promises), the event loop keeps prioritizing them… and macrotasks like setTimeout might wait longer than expected. So the order looks like this: Sync Code → Microtasks → Macrotask → repeat. Understanding this isn’t just theory. It explains why your async code sometimes behaves like it’s possessed. JavaScript looks simple on the surface. Underneath, it’s a tiny scheduler juggling tasks like a caffeinated circus performer. #javascript #webdevelopment #frontend #reactjs #nodejs #coding #programming #eventloop #asyncjavascript #developers
To view or add a comment, sign in
-
-
🚀 Simplifying JavaScript Promises! Understanding asynchronous operations can be tricky, but Promises make it easier to handle tasks like API calls, error handling, and chaining multiple operations. This infographic breaks it down step by step, from creating a Promise to handling results and chaining them. Perfect for developers looking to strengthen their JS fundamentals! 💻✨ #JavaScript #WebDevelopment #Coding #Programming #LearnToCode #FrontendDevelopment #AsyncProgramming #Promises #API #TechTips #Developers #SoftwareEngineering #CodeNewbie #JS
To view or add a comment, sign in
-
Explore related topics
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