Most developers learn JavaScript math like this: 👉 `0.1 + 0.2 === 0.3` But in JavaScript, that is false. And the reason is bigger than JavaScript itself: 👉 Floating point math is inherently imprecise This is not a bug. It is how computers represent decimal numbers. Some numbers cannot be stored exactly in binary, so JavaScript keeps the closest possible value instead. That is why this happens: • `0.1 + 0.2` becomes `0.30000000000000004` • `0.3 - 0.2` can produce unexpected decimals • Comparisons like `a === b` may fail when you expect them to pass This matters a lot in real code: • Money calculations • Measurements • Scientific values • Threshold checks • UI rounding bugs So the lesson is simple: Don’t assume decimal math is exact. When precision matters, round carefully, compare with tolerance, or use a numeric strategy designed for the job. JavaScript is not bad at math. It is just honest about the way computers store numbers. And once you understand that, a whole class of “weird bugs” suddenly makes sense. #JavaScript #Programming #SoftwareEngineering #WebDevelopment #CodingTips #LearnToCode #ComputerScience #FrontendDevelopment #DeveloperLife
JavaScript's Floating Point Math Imprecision Explained
More Relevant Posts
-
Today, I learned something interesting about numbers in JavaScript. For example: 0.1 + 0.2 = 0.30000000000000004 But 1.1 plus 1.2 equals 2.3 In simple terms: JavaScript doesn't store numbers in decimal form; it stores them in binary (0s and 1s). Binary can't store some decimal numbers exactly, like 0.1 and 0.2. This means that a very small error is added when you do math. That's why you might see results like 0.30000000000000004. Temporary Fixes : Use Math.round : Math.round((0.1 + 0.2) * 100) / 100 Use toFixed: (0.1 + 0.2).toFixed(2) Use Math.floor or Math.ceil: Math.floor((0.1 + 0.2) * 100) / 100 //when rounding off lower value Math.ceil((0.1 + 0.2) * 100) / 100 //when rounding off to upper value This is not a bug in JavaScript. It happens because of how most programming languages store floating-point numbers. #JavaScript #Coding #WebDevelopment #Programming #100DaysOfCode
To view or add a comment, sign in
-
-
Why JavaScript Floating Point Math Breaks Your App (And How to Fix It) JavaScript's IEEE 754 floating point format means 0.1 + 0.2 !== 0.3 — and that's just the obvious case. This post covers why it happens, where it silently breaks production code, and four concrete strategies: toFixed for display, integer arithmetic for money, a scale-round-ceil pattern for computed floats, and decimal.js for complex chains....
To view or add a comment, sign in
-
Why JavaScript Floating Point Math Breaks Your App (And How to Fix It) JavaScript's IEEE 754 floating point format means 0.1 + 0.2 !== 0.3 — and that's just the obvious case. This post covers why it happens, where it silently breaks production code, and four concrete strategies: toFixed for display, integer arithmetic for money, a scale-round-ceil pattern for computed floats, and decimal.js for complex chains....
To view or add a comment, sign in
-
30 Days JavaScript Challenge : Day 27 ✅ Today’s problem was about creating a compact object basically removing all falsy values from an object or array, even if they are nested. At first glance it looks easy, but once nested structures come in, it gets interesting. This problem really tests your understanding of: Falsy values (null, 0, false, "", etc.) Recursion for nested objects/arrays Treating arrays like objects (since indices are keys) It’s one of those questions that feels very practical like cleaning API responses or filtering unwanted data before using it. Definitely helped me think more deeply about how JavaScript handles data structures. Almost at the end now… consistency paying off 🚀 #javascript #leetcode #webdevelopment #frontenddeveloper #codingchallenge #learninginpublic #developers #programming #buildinpublic
To view or add a comment, sign in
-
-
🚀 Day 8 of My JavaScript Learning Journey Today I learned about Strings in JavaScript and explored various built-in methods to manipulate text. 📌 What I learned: • A String is a sequence of characters used to store text data • Strings are immutable (cannot be changed directly) • It is a primitive data type • String operations always return a new string ⚙️ String methods I practiced: ✔ length ✔ toUpperCase() / toLowerCase() ✔ trim() ✔ slice() / substring() ✔ replace() ✔ includes() ✔ indexOf() ✔ split() ✔ concat() 💡 I also practiced template literals: Hello ${name} → makes string formatting easier and cleaner. Understanding strings is very important because text handling is used in almost every application. Step by step, I’m improving my JavaScript fundamentals and coding skills. 💻✨ #JavaScript #WebDevelopment #FrontendDevelopment #CodingJourney #LearningInPublic #DeveloperJourney #ProgrammingBasics
To view or add a comment, sign in
-
-
𝗔 𝗦𝘁𝗿𝘂𝗰𝘁𝘂𝗿𝗮𝗹 𝗘𝗻𝗴𝗶𝗻𝗲𝗲𝗿 𝗮𝗰𝗾𝘂𝗶𝗿𝗶𝗻𝗴 𝗪𝗲𝗯 𝗗𝗲𝘃 𝗦𝗸𝗶𝗹𝗹𝘀. 𝗖𝗵𝗮𝗹𝗹𝗲𝗻𝗴𝗲 𝗗𝗮𝘆 𝟴/𝟵 ✅ - 𝗙𝘂𝗹𝗹 𝗦𝗶𝘁𝗲 𝗕𝘂𝗶𝗹𝗱 🟢 Python → 🟢 HTML & CSS → 🟢 Web page Deployment/PHP → 🔘 JavaScript → 🔘 React → 🔘 React Flow → 🔘 Fast API Today was a full website build — all from scratch using HTML, CSS, and a bit of JavaScript. It's fun knowing the building blocks of the web. Yes, AI can do all of this in a glimpse, but understanding what's actually happening underneath means you can direct it, fix it, and build on top of it properly. Tomorrow is the final day of the HTML & CSS course — then on to the most important part: JavaScript. #WebDevelopment #CSS #HTML #JavaScript #Python #FullStack #StructuralEngineering #BridgeEngineering #DigitalEngineering #Upskilling #ResponsiveDesign
To view or add a comment, sign in
-
Assalam o Alaikum everyone, JavaScript Lesson 29 is here: Reference vs Value, Shallow Copy, Deep Copy & Immutable Patterns. This lesson covers one of the most important JavaScript concepts for writing clean and predictable code: understanding how values are stored, copied, and updated in memory. I explained the difference between primitive values and reference values, then demonstrated what happens when you assign objects and arrays directly. From there, I showed how to create shallow copies using spread syntax and Object.assign(), and why shallow copy can still cause problems with nested objects. I also covered deep copy using both: - JSON.parse(JSON.stringify()) - structuredClone() After that, I moved into immutable programming patterns, where I showed how to update arrays and objects without mutating the original data using: - filter() - map() - spread syntax - destructuring This is a very practical topic for JavaScript developers because immutability helps prevent bugs and makes code easier to maintain in real projects. Watch the lesson: https://lnkd.in/dSeTxYPP #JavaScript #ReferenceVsValue #ShallowCopy #DeepCopy #ImmutablePatterns #JavaScriptTutorial #WebDevelopment #FrontendDevelopment #Programming #CodingTutorial #DeveloperMaroof #LearnJavaScript #JavaScriptBasics #ModernJavaScript #SpreadOperator #ObjectAssign #StructuredClone #Immutability #JSConcepts #JavaScriptLessons
To view or add a comment, sign in
-
-
🚀 Understanding the this Keyword in JavaScript Read full article here: https://lnkd.in/dYgJ6F_Y The this keyword is one of the most confusing concepts in JavaScript—but once you understand it, everything starts to click. In simple terms, this refers to the caller of a function. 📌 What I covered in this article: • this in the global context • this inside objects (methods) • this inside functions • How the calling context changes the value of this 💡 Key Insight: The value of this is not fixed—it depends on how a function is called, not where it is written. 🙏 Special thanks to my mentors and teachers from Chai Aur Code — Hitesh Choudhary Sir, Piyush Garg Sir, Suraj Kumar Jha Sir, and Akash Kadlag Sir for their amazing guidance and teaching. If you're learning JavaScript or preparing for interviews, mastering this is a must. Let me know your biggest confusion about this 👇 #JavaScript #WebDevelopment #FrontendDevelopment #BackendDevelopment #FullStackDevelopment #Coding #Programming #LearnToCode #Developers #ChaiAurCode
To view or add a comment, sign in
-
-
🚀 Day 4 of my JavaScript Coding Practice Today’s problem: Two Sum var twoSum = function(nums, target) { const map = new Map(); // Store: { value : index } for (let i = 0; i < nums.length; i++) { const complement = target - nums[i]; // If the needed number is already in our map, we found the pair! if (map.has(complement)) { return [map.get(complement), i]; } // Otherwise, save the current number and its index map.set(nums[i], i); } return []; // Return empty if no pair is found }; 💡 Instead of using brute force (O(n²)), I used a HashMap approach to solve it in O(n) time. Key takeaway: Understanding how to trade space for time can significantly optimize performance. Small steps every day → Big improvements over time 📈 #JavaScript #DSA #CodingPractice #100DaysOfCode #FrontendDevelopment
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