Rohan kumar’s Post

10 JavaScript Questions That Reveal How Well You Really Understand the Language 1. Why does adding decimals sometimes give strange results? JavaScript stores numbers using a binary floating-point system. Because values like 0.1 can’t be represented exactly in binary, small inaccuracies appear during calculations. These tiny errors add up and show unexpected results in decimal math. 2. Why does "" == 0 evaluate to true? When using loose equality, JavaScript converts both sides to a common type. An empty string becomes 0, and the number 0 stays the same so the comparison succeeds. This is a classic example of type coercion at work. 3. Why isn’t NaN equal to anything even itself? NaN represents an undefined numeric result, like dividing zero by zero. Since it indicates a failed computation, JavaScript treats it as incomparable, making every equality check with NaN return false. 4. Why does accessing a let variable before declaration throw an error? Variables declared with let and const exist in memory but remain inaccessible until execution reaches their declaration. This prevents accidental use before initialization and results in a runtime error if accessed too early. 5. How can functions store data like objects? Functions in JavaScript are callable objects. This means you can attach properties directly to them, allowing a function to remember values, store configuration data, or keep internal state across calls. 6. Why do Promises run before setTimeout? Promise callbacks are placed in the microtask queue, which has higher priority than the macrotask queue used by timers. After the current code finishes, JavaScript clears all microtasks before handling any scheduled timers. 7. What really happens when you create an object with a constructor? Using a constructor links a new object to a prototype, assigns properties through this, and sets up inheritance automatically. This process allows methods to be shared efficiently across instances. 8. Why is chaining functions considered a good practice? Chaining encourages small, single-purpose functions that work together. This makes code easier to test, reason about, and reuse, especially in data transformation pipelines. 9. Why does "5" + 3 return "53" instead of 8? When one operand is a string, JavaScript switches to string concatenation. The number is converted into a string, and both values are joined together instead of being added numerically. 10. How can template literals be customized with logic? By attaching a function to a template literal, JavaScript lets you intercept and modify how strings and expressions are combined. This is useful for localization, escaping user input, or building custom DSLs. For help and guidance in you carrier path https://lnkd.in/gH3paVi7 Join my dev community for resources📚, tech talks🧑🏻💻and learning 🧠 https://lnkd.in/gt8WeZSt #frontend #interview #javascript

To view or add a comment, sign in

Explore content categories