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
JavaScript Floating Point Precision Issue
More Relevant Posts
-
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
To view or add a comment, sign in
-
-
🚀 Today I Practiced JavaScript: Quick Sort Algorithm: Sorting is one of the most fundamental concepts in programming, and Quick Sort is a powerful algorithm every developer should know. 💡 Core Concept: Quick Sort follows a divide and conquer approach: 1. Divide the array into smaller parts based on a pivot 2.Conquer by recursively sorting the smaller parts 3.Combine the results to get the final sorted array 🎯 Key Insight: In my JavaScript implementation: 1.I used push() to partition elements into smaller and larger groups 2.I used the spread operator (...) to combine the results efficiently 3.This approach keeps the logic simple while clearly demonstrating how the algorithm works internally. ⚡ Time Complexity: 1. Average: O(n log n) 2. Worst: O(n²) Quick Sort is widely used because of its efficiency and elegant approach to problem-solving. #JavaScript #DSA #Algorithms #Coding #Programming #WebDevelopment #Quick Sort 😊
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
-
-
A Structural Engineer acquiring Web Dev Skills — Day 11/100 JavaScript Day 2/25 ✅ 🟢 Python → 🟢 HTML & CSS → 🟢 JavaScript → ⚫ React → ⚫ ReactFlow → ⚫ FastAPI ______________________________ Java script Fundamentals Part 2 — done. Another 4.5 hours, another single sitting. JavaScript keeps feeling closer to Python than it has any right to. Different worlds on paper — one lives in the browser, the other in data analysis — but the logic underneath is the same. That familiarity is dangerous in a good way. It makes you move faster than you expected. Fundamentals are now fully covered. Tomorrow the real thing begins — actual web page development. The goal with these posts is simple: Some days the only reason I open the course is knowing this update is due. That's enough. #WebDevelopment #JavaScript #Python #FullStack #StructuralEngineering #BridgeEngineering #DigitalEngineering #Upskilling
To view or add a comment, sign in
-
🚀 Day 2 of Logic Building in JavaScript Most beginners ignore this… but pattern problems actually build real programming logic 🤯 Today I focused on strengthening my fundamentals through pattern-based questions 👇 ✅ What I practiced: • Right-angled star pattern • Inverted right-angled star pattern • Pyramid star pattern • Diamond shape ⭐ & inverted pattern (odd numbers) • Hollow square star pattern • Hollow pyramid triangle • Alternate binary triangle (0-1 pattern) 🧠 What I actually learned: • How 2*i - 1 controls pyramid width • How spacing (n - i) creates perfect alignment • Difference between boundary vs inner logic in hollow patterns • Importance of print vs println (small thing, big impact!) Consistency 💯 Building logic one day at a time 🚀 👉 What was the toughest pattern you faced while learning? #Day2 #LogicBuilding #JavaScript y #DSA #100DaysOfCode #LearnInPublic #Developers
To view or add a comment, sign in
-
What is the difference between let, var, and const? `var`, `let`, and `const` are used to declare variables in JavaScript, but they behave differently. `var` is the older way and has function scope, which can lead to unexpected behavior. It also allows redeclaration and can be updated freely. `let` is block-scoped, meaning it only exists within the block where it’s defined. It can be updated but not redeclared in the same scope, making it safer than `var`. `const` is also block-scoped but cannot be reassigned after it’s declared. It’s used for values that should not change. Overall, `let` and `const` are preferred in modern JavaScript. #webdeveloper #tech #coding #programming
To view or add a comment, sign in
-
-
Not all code is compiled. Some code runs instantly. No build. No install. No waiting. 👉 That’s called interpretation. When you open a website, the code isn’t pre-compiled like apps. Your browser reads it and runs it on the spot. Same with some languages: They don’t convert everything before running. They execute it line by line. That’s why: - You can run JavaScript instantly - You can execute Python without building apps - Websites load without installation But there’s a trade-off: More flexibility… less raw performance. Compiled → faster Interpreted → more flexible Both exist. Both matter. This is Part 4 of the series. Tomorrow: the hidden layer that makes both work smoothly (runtimes). Follow if you want to actually understand how code runs in real systems. #Programming #Coding #WebDevelopment #JavaScript #Python #SoftwareDevelopment #TechExplained #Developers #ComputerScience
To view or add a comment, sign in
-
-
Excited to share a web based project — PocketLab 🧪 A Django-based web application designed to help users log, manage, and review scientific experiments efficiently. Key Features: • Full CRUD functionality • Responsive modern UI • Experiment detail tracking • Form validation and progress indicators • Clean and organized dashboard layout Built With: Python | Django | HTML | CSS | JavaScript This project helped me strengthen my understanding of Django CRUD operations, template inheritance, static files management, and frontend UI design. #Django #Python #WebDevelopment #SoftwareDevelopment #PortfolioProject #FullStackDevelopment #Programming
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
-
How language work on website? Websites use different languages for different tasks: HTML → creates structure CSS → adds design JavaScript → adds functionality Backend (like Python) → handles data Together, they make a website work and look interactive.
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