Most JavaScript developers have seen this but never understood why. Type 0.1 + 0.2 in your console. You expect 0.3. You get 0.30000000000000004. This is not a JavaScript bug. It is IEEE 754 — the standard for how ALL computers store numbers in binary. 0.1 and 0.2 cannot be represented exactly in binary, just like 1/3 cannot be represented exactly in decimal. The tiny rounding errors accumulate. This silently breaks: → Currency calculations → Percentage comparisons → Any equality check with decimals The fix: → Use .toFixed() for display → Store currency as integers (cents) Were you aware of this? 1️⃣ Yes / 2️⃣ No #JavaScript #WebDev #Coding #SoftwareEngineering #Frontend
IEEE 754: Why 0.1 + 0.2 != 0.3 in JavaScript
More Relevant Posts
-
🤯 Why did JavaScript change my number? let a = 9999999999999999; console.log(a); // 10000000000000000 I didn’t change the value… But JavaScript did. 💡 Reason? JavaScript stores numbers as 64-bit floating point (IEEE 754) Because of this, it can only safely represent numbers up to: 👉 2^53 - 1 Beyond that → precision is lost ❌ So: 👉 9999999999999999 becomes 10000000000000000 ✅ Fix: Use BigInt let a = 9999999999999999n; 💭 Did you know this before? #JavaScript #Coding #Developers #WebDevelopment #Programming
To view or add a comment, sign in
-
𝗡𝗼𝗱𝗲.𝗷𝘀 𝐓𝐨𝐩𝐢𝐜 𝟐: 𝐓𝐡𝐞 𝐓𝐡𝐫𝐞𝐚𝐝 𝐏𝐨𝐨𝐥 Under the hood, libuv maintains a pool of 4 threads (by default) that runs in parallel with your JavaScript. Every time you call certain Node.js APIs, the work is offloaded to this pool - silently, invisibly. 📂 What goes to the thread pool: ➡️ fs.readFile / fs.writeFile — all file system operations ➡️ crypto.pbkdf2, crypto.randomBytes, crypto.scrypt ➡️ dns.lookup (not dns.resolve — that uses the network directly) ➡️ zlib compression and decompression What goes directly to the OS kernel, bypassing the pool entirely: ➡️ TCP / UDP sockets ➡️ HTTP / HTTPS requests ➡️ Unix pipes ❗𝘛𝘩𝘦 𝘱𝘳𝘰𝘥𝘶𝘤𝘵𝘪𝘰𝘯 𝘣𝘶𝘨 Default pool size: 4 threads. If your server receives 8 simultaneous requests that each call crypto.pbkdf2 (common in authentication flows), 4 of them will wait - blocked - until a thread is free. Your Event Loop stays responsive. But your response times double. This is invisible in development. It surfaces under load in production. 💡𝐓𝐡𝐞 𝐅𝐢𝐱: Set this before your process starts: UV_THREADPOOL_SIZE=8 The maximum is 1024. The right value depends on your workload - profile before guessing. Node.js is single-threaded where it matters - your JavaScript. But the infrastructure running beneath it is not. Knowing the boundary between the two is what separates application developers from systems developers. #nodejs #javascript #backend #performance #softwaredevelopment #systemdesign
To view or add a comment, sign in
-
-
JavaScript Logical Series – Day 1 👉 What will be the output? console.log(1 + "2" + "2"); console.log(1 + +"2" + "2"); console.log(1 + -"1" + "2"); console.log(+"1" + "1" + "2"); Output : 122 32 02 112 Explanation • "2" → string → concatenation happens • +"2" → converts string to number • -"1" → converts to number (-1) • Order of operations matters 🔥 Key Point JavaScript type coercion can change output unexpectedly. Always be careful with + operator. #JavaScript #NodeJS #WebDevelopment #Programming #CodingChallenge #FrontendDevelopment #SoftwareEngineering
To view or add a comment, sign in
-
-
🧵 "Node.js is single threaded" everyone says that. But this is only half of the truth. Yes, Node.js is single threaded in its main execution the Event Loop and your code run on one thread. But using many other things, we can make it behave like a multiprocess or multithreaded system that performs perfectly even for CPU intensive tasks: 1-libuv (the underlying engine) It is the underlying C++ library in Node. Through it we can handle: A)Network I/Os : by dealing with the OS kernel directly So the kernel handles it asynchronously and notifies Node when done, so the main thread never blocks. B)File I/Os and DNS operations : through a thread pool that abstracts our main thread from that heavy load. 2-Event Loop Partitioning: But if the load is heavier than we thought, we need to handle it differently. -We can do partitioning making heavy operations done in multiple phases of the Event Loop so we don't starve any phase. -Using setImmediate(), I can distribute my work across different check phase iterations, so other phases keep running alongside the heavy operation without being starved. 3-Worker Threads & Child Processes Okay but what if the task is even heavier CPU intensive work? -Then we have Node Workers I can make my main thread do some work and offload intensive CPU tasks to other threads. (Yes, other threads just like a multithreading system.) Or we can even use Child Processes to offload work onto entirely different processes, each with their own memory and dedicated everything. So now we get it Node.js is single threaded in its code execution, but we can still make it behave like any concurrent parallel system. It's not a limitation. It's a deliberate design choice. #nodejs #backend #javascript #SoftwareEngineering
To view or add a comment, sign in
-
-
VS Code 1.115 is out and it's packed with improvements you'll actually use 🔧 Background terminals now notify agents automatically. SSH remotes set themselves up. File edits are tracked and restorable. There's a lot here. I broke down every update in plain English so you don't have to dig through the changelog yourself 👀 New post on hamidrazadev.com — link in bio! #vscode #webdev #javascript #programminglife #devtools #codenewbie #100daysofcode
To view or add a comment, sign in
-
-
Just realized something interesting about JavaScripts memory optimisation behaviour with primitives while digging into runtime behavior 👇 Values like true, false, undefined, null, and even common string literals (like "") aren’t recreated every time you declare them. JS engines (like V8) treat these as shared, immutable values: • They exist only once internally • Variables just hold references to them So when you write: let a = true; let b = true; You’re not creating two true values — both just point to the same underlying value. Same idea applies to: • false • undefined • null • frequently used string literals 👉 Small detail, but shows how much memory optimization happens under the hood, a neat reminder that JavaScript does more for performance than we often think. #JavaScript #V8 #Performance #Programming #Backend
To view or add a comment, sign in
-
So a question was bothering me for a long time . The letter "A" and the number 65 are literally the same thing inside your computer? when we do the following in javascript : let num = 65 console.log(String.fromCharCode(num)) // prints "A" You just converted a number into a letter. Because to the computer, they were always the same thing(!) Here is what is actually happening: When you store the number 65, the computer saves it as 01000001 in memory. When you store the letter "A", the computer saves it as...(drum rolls) 01000001. Exact same bits. Exact same memory. The only difference is context. When you declare a number, the program reads those bits as 65. When you declare a string, the same bits get read as "A". The computer does not decide this. You do, as the programmer. #javascript
To view or add a comment, sign in
-
🚀𝐈 𝐛𝐮𝐢𝐥𝐭 𝐦𝐲 𝐨𝐰𝐧 𝐂 𝐂𝐨𝐦𝐩𝐢𝐥𝐞𝐫 — 𝐚𝐧𝐝 𝐢𝐭'𝐬 𝐧𝐨𝐰 𝐋𝐈𝐕𝐄! I'm excited to share my latest project: Mini C Compiler — a complete web-based C compiler with Lexical, Syntax, Semantic Analysis, and Code Generation! 🔥 💡 𝗪𝗵𝗮𝘁 𝗺𝗮𝗸𝗲𝘀 𝘁𝗵𝗶𝘀 𝘀𝗽𝗲𝗰𝗶𝗮𝗹? This compiler processes C code through all 5 major compilation stages: ✅ 𝗟𝗲𝘅𝗶𝗰𝗮𝗹 𝗔𝗻𝗮𝗹𝘆𝘀𝗶𝘀 — Converts code into tokens with line & column tracking ✅ 𝗦𝘆𝗻𝘁𝗮𝘅 𝗔𝗻𝗮𝗹𝘆𝘀𝗶𝘀— Builds an Abstract Syntax Tree (AST) ✅ 𝗦𝗲𝗺𝗮𝗻𝘁𝗶𝗰 𝗔𝗻𝗮𝗹𝘆𝘀𝗶𝘀 — Generates Symbol Table with type checking ✅ 𝗖𝗼𝗱𝗲 𝗚𝗲𝗻𝗲𝗿𝗮𝘁𝗶𝗼𝗻 — Produces optimized output code ✅ 𝗠𝗼𝗱𝗲𝗿𝗻 𝗨𝗜 — Clean interface with 5 structured output tabs 🌐 𝗟𝗶𝘃𝗲 𝗗𝗲𝗺𝗼: 👉 https://lnkd.in/gEHeU8xU ⚙️ 𝗧𝗲𝗰𝗵 𝗦𝘁𝗮𝗰𝗸: 🔹 C++ (Core compiler logic) 🔹 Node.js + Express (Backend API) 🔹 React.js (Frontend UI) 🔹 Render (Hosting) 📂 𝗚𝗶𝘁𝗛𝘂𝗯 𝗥𝗲𝗽𝗼𝘀𝗶𝘁𝗼𝗿𝘆: 👉 https://lnkd.in/gNJqEtcf This project helped me deeply understand how compilers work under the hood — from tokenization to execution. I’d love to hear your feedback and suggestions! 🙌 #CCompiler #WebDevelopment #ReactJS #NodeJS #CPlusPlus #CompilerDesign #SideProject #Programming
To view or add a comment, sign in
-
Console.log([1,2,3] + [4,5,6]). What is the output? 🤔 💬 Comment your answer first… then click “more” 👇 Most developers think the result is: [1,2,3,4,5,6] ❌ Nope 😏 In JavaScript the output is: "1,2,34,5,6" 💥 👉 Data type: string 👉 Why? Arrays convert to strings before using +. Then JavaScript concatenates the strings. 🔥 Rule: + with arrays → string concatenation Did you guess the correct output and data type? 😄 #javascript #codingtips #programming #developer #webdev #100daysofcode
To view or add a comment, sign in
-
Intel Report [CRITICAL] A critical remote code execution (RCE) vulnerability has been identified in protobuf.js, a widely used JavaScript implementation of Google's Protocol Buffers with approximately 50 million weekly downloads on npm. The vulnerability, tracked as GHSA-xq3m-2v4x-88gg, stems from unsafe dynamic code generation... https://lnkd.in/dgmDAvyh
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
Developers new to this often find it shocking, but it underscores why we must treat numerical precision with extreme caution across the entire stack