At first glance, this looks confusing: "5" - -"2" → 7 But it’s actually a clean example of how type coercion works in JavaScript. Here’s the breakdown: "2" → 2 -"2" → -2 (unary minus forces number conversion) "5" → 5 So: 5 - (-2) = 7 One important detail: There is no + operator involved, so string concatenation never comes into play. Concepts like this are frequently used in interviews to test clarity around type coercion. Once you understand the rules, these questions stop being tricky. 👉 I’ve explained this and similar concepts step-by-step in the full video (link in comments)
More Relevant Posts
-
𝗝𝗦 𝗦𝘁𝗿𝗶𝗻𝗴 𝗣𝗼𝗹𝘆𝗳𝗶𝗹𝗹𝘀 𝗔𝗻𝗱 𝗜𝗻𝘁𝗲𝗿𝘃𝗶𝗲𝘄 𝗧𝗶𝗽𝘀 Do you know how JavaScript methods work? A polyfill adds modern features to old browsers. You write the code when a browser lacks a feature. Built-in methods live in prototypes. Example: toUpperCase lives in String.prototype. Every string uses the same function. This saves memory. Strings are primitive types. Primitives do not have methods. JavaScript uses auto-boxing to solve this. It creates a temporary object. The method runs. The object disappears. Prepare these string problems for your next interview: - Reverse a string without built-in methods. - Check for palindromes. - Find the longest substring without repeating characters. Source: https://lnkd.in/dU88fG4S
To view or add a comment, sign in
-
Continuing the 10-week series where I share questions that separate familiarity from real understanding. 🔹 Engineering Depth – Week 6: Equality & Coercion Q: Why can two values that look completely different be considered equal in JavaScript? “== triggers type coercion.” “JavaScript follows abstract equality rules.” “Conversion happens before comparison.” Examples: [] == false // true "" == 0 // true null == undefined // true But: [] === false // false Why this matters in real systems: • Unexpected condition checks • Hidden bugs in form validation • Inconsistent API comparisons Understanding this helps you: – Avoid implicit coercion traps – Write predictable comparisons – Debug edge-case bugs faster JavaScript doesn’t compare values directly. Sometimes, it transforms them first. #JavaScript #Equality #Coercion #SoftwareEngineering #FrontendDevelopment #TechDepth
To view or add a comment, sign in
-
-
🧠 What is Anagram? 👉 Two strings are anagrams if: ✔ Same characters ✔ Same frequency ✔ Different order 📌 Example: listen → silent ✅ evil → vile ✅ ❌ Not Anagram: hello → world 💻 Code: const isAnagram = (a, b) => a.split("").sort().join("") === b.split("").sort().join(""); 💡 Trick: Sort & Compare #DSA #JavaScript #ProblemSolving
To view or add a comment, sign in
-
Quick JavaScript reminder: == checks value (allows type coercion) === checks value + type (strict) 5 == "5" → true 5 === "5" → false Why === is better: With ==: null == undefined // true 0 == false // true "" == false // true [] == false // true (!) With ===: All of the above → false Type coercion makes == unpredictable. Unless you need it for a specific edge case, stick with ===. Your future self (and your code reviewers) will thank you.
To view or add a comment, sign in
-
In JavaScript: 𝐯𝐚𝐥𝐮𝐞 == 𝐧𝐮𝐥𝐥 is one of the few places where 𝐥𝐨𝐨𝐬𝐞 𝐞𝐪𝐮𝐚𝐥𝐢𝐭𝐲 is intentional. Under the hood, the == operator follows the 𝐀𝐛𝐬𝐭𝐫𝐚𝐜𝐭 𝐄𝐪𝐮𝐚𝐥𝐢𝐭𝐲 𝐂𝐨𝐦𝐩𝐚𝐫𝐢𝐬𝐨𝐧 algorithm. It includes a special rule: if one value is 𝐧𝐮𝐥𝐥 and the other is 𝐮𝐧𝐝𝐞𝐟𝐢𝐧𝐞𝐝, it returns 𝐭𝐫𝐮𝐞, without further type coercion. So: 𝐮𝐧𝐝𝐞𝐟𝐢𝐧𝐞𝐝 == 𝐧𝐮𝐥𝐥 // true On the other hand, === uses 𝐒𝐭𝐫𝐢𝐜𝐭 𝐄𝐪𝐮𝐚𝐥𝐢𝐭𝐲 𝐂𝐨𝐦𝐩𝐚𝐫𝐢𝐬𝐨𝐧, which checks both type and value with no exceptions: 𝐮𝐧𝐝𝐞𝐟𝐢𝐧𝐞𝐝 === 𝐧𝐮𝐥𝐥// false That’s why this pattern works: if (value == null) { // catches both null and undefined } It’s not “loose” by accident, it’s a deliberate shortcut baked into the language.
To view or add a comment, sign in
-
-
A common misconception in JavaScript: “Objects are copied.” Let’s test that: let obj1 = { name: "John" }; let obj2 = obj1; obj2.name = "Doe"; console.log(obj1.name); // "Doe" At first, this feels unexpected. But here’s what’s really happening: JavaScript doesn’t copy the object. It copies the reference to that object. So both obj1 and obj2 point to the same memory location. That’s why changing one reflects in the other. This is one of the most common concepts tested in interviews — often with tricky variations. Understanding this clearly means you won’t rely on guesses anymore. 👉 I’ve broken this and similar concepts step-by-step in the full video (link in comments)
Why JavaScript Objects Don’t Actually “Copy”
To view or add a comment, sign in
-
DAY 14 — The Dependency Array Is a Contract THE DEPENDENCY ARRAY IS A CONTRACT Lying to useEffect's dependency array is one of the most common React mistakes. The eslint-plugin-react-hooks rule exists for a reason. If you omit a variable from the deps array, your effect runs with a stale closure. If you include everything correctly, the effect re-runs when any dep changes. The temptation is to add [] to "run once" — but if your effect uses state or props, you're creating a silent bug. The real fix is to rethink the effect's structure, not silence the linter. Empty deps [] means "I swear this effect uses nothing from the component." If that's not true, you have a bug. How many times has the linter saved you from yourself? 👇 #useEffect #ReactHooks #BestPractices #JavaScript
To view or add a comment, sign in
-
-
I used to avoid reduce method in Javascript because of how confusing it was. Today I wrote my own reduce polyfill. Handling all edge cases, it was challenging at first but then it all clicks. While writing polyfills the core flow of callback, this context and iteration is the easiest & part. But in case of reduce you need to consider following : 1. Initial values 2. Sparse arrays (holes) 3. Dynamic start indexing 4. Edge case handling with custom errors Once you work on each pointers you will understand all edge cases, reduce is not just about iteration its about the flow. #javascript
To view or add a comment, sign in
-
🚀 Mastering the Sliding Window Pattern in JavaScript One of the most powerful techniques for solving array problems efficiently is the Sliding Window + Deque approach. Today, I implemented the classic “Maximum in Sliding Window” problem in O(n) time 👇 🔍 Problem: Given an array and a window size k, return the maximum value in each window as it slides from left to right. 💡 Key Insight: Instead of recomputing the max for each window (which is expensive), we maintain a Deque (double-ended queue) that: Stores indices (not values) Keeps elements in decreasing order Ensures the front always holds the max ⚡ Result: Efficient solution with linear time complexity — each element is processed once. 📚 I’m currently building a collection of algorithm patterns in JavaScript: 👉 https://lnkd.in/ej4fNeZs #JavaScript #Algorithms #DataStructures #CodingInterview #SoftwareEngineering #100DaysOfCode
To view or add a comment, sign in
-
-
𝗦𝗽𝗿𝗲𝗮𝗱 𝗩𝗦 𝗥𝗲𝘀𝘁 𝗢𝗽𝗲𝗿𝗮𝘁𝗼𝗿𝘀 𝗜𝗻 𝗝𝗮𝘃𝗮𝗦𝗰𝗿𝗶𝗽𝘁 You've seen ... in JavaScript and wondered what it does. The same syntax is used for two purposes: - Spread operator expands values - Rest operator collects values Understanding this difference is crucial for writing clean JavaScript. The spread operator is used to expand elements from arrays or objects. It spreads elements individually and creates a shallow copy, avoiding mutation. The rest operator is used to collect multiple values into one. It gathers everything into an array. Here are key differences: - Purpose: Spread expands, Rest collects - Usage: Spread on the right, Rest on the left - Output: Spread gives individual elements, Rest gives an array Mastering spread vs rest helps you write cleaner code, handle data efficiently, and solve interview problems confidently. Source: https://lnkd.in/gmFfBhXX
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
Full Video: https://youtu.be/GUQIfc3DiBA?si=CotojEbIK-5_cpnW