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.
Why Use Strict Equality in JavaScript
More Relevant Posts
-
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
-
-
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)
JavaScript Interview Trap: "5" - -"2" 😵💫
To view or add a comment, sign in
-
Day9:3-04-2026 Here is the simple and main point explanation (no code): 1. confirm() It is a popup dialog box in JavaScript. It shows a message with OK and Cancel buttons. It is used to get confirmation from the user. If the user clicks OK → returns true. If the user clicks Cancel → returns false. Commonly used for: Delete confirmation Form submission confirmation Exit warning 2. eval() It is used to evaluate or execute a string as JavaScript code. It converts a string expression into actual code. Mostly used for: Calculating mathematical expressions written as strings. It is not recommended for regular use because: It is a security risk It slows down performance It can execute harmful code 3. Number Convert to String There are ways to convert a number into a string. Why we convert? To display numbers as text. To combine numbers with text. For formatting purposes. Main Points: A number can be converted into a string type. After conversion, it behaves like text. You cannot perform arithmetic operations directly on it unless converted back to number. The data type changes from number → string. If you want, I can also explain: String to number conversion Difference between parseInt() and Number()
To view or add a comment, sign in
-
-
𝗝𝗦 𝗦𝘁𝗿𝗶𝗻𝗴 𝗣𝗼𝗹𝘆𝗳𝗶𝗹𝗹𝘀 𝗔𝗻𝗱 𝗜𝗻𝘁𝗲𝗿𝘃𝗶𝗲𝘄 𝗧𝗶𝗽𝘀 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
-
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
-
𝗧𝗵𝗲 𝗕𝗮𝘀𝗶𝗰𝘀 𝗼𝗳 𝗖𝗮𝗹𝗹𝗯𝗮𝗰𝗸 𝗙𝘂𝗻𝗰𝘁𝗶𝗼𝗻𝘀 You pass a function to another function as an argument. This passed function is called a callback function. It gets executed later. A function can accept another function as an argument. This allows one function to call another at a later time. In JavaScript, you can pass a function to another function. This passed function will be executed after the first function finishes. JavaScript runs in a single thread. It can only execute one task at a time. You can use the forEach method on arrays. This method executes a provided function for each element in an array. You can use normal functions or arrow functions with forEach. The forEach method automatically creates values for the element, index, and array. You can use one, two, or all three of these values. Source: https://lnkd.in/grA7h2vr
To view or add a comment, sign in
-
Part 2 : " this " in JS continued Arrow functions work differently in JavaScript. ✅ They do not have their own `this`. Instead, they use `this` from the surrounding scope. This is called lexical `this`. Example: let person = { name: "Alice", greet: function() { setTimeout(() => { console.log(this.name); }, 1000); } }; person.greet(); // Here, the arrow function uses `this` from greet(), // so it still points to person. ⚠️ Important exception: If you write an arrow function directly as an object method, it will not see the object as `this`. Example: let obj = { name: "Sam", greet: () => { console.log(this.name); } }; obj.greet(); // This will not use obj as `this`. 🧠 The rule is: - Normal function: `this` depends on how you call it. - Arrow function: `this` is taken from the surrounding scope and stays fixed.
To view or add a comment, sign in
-
𝗠𝗮𝘀𝘁𝗲𝗿𝗶𝗻𝗴 𝗝𝗮𝘃𝗮𝗦𝗰𝗿𝗶𝗽𝘁 𝗶𝗻 𝟮𝟬𝟮𝟲 JavaScript changed. Tools are better now. You still fall into old traps. Avoid these 5 mistakes. - Top-level await It is risky in libraries. It creates race conditions. Use a singleton pattern. This protects your bundle size. - The == operator Stop using ==. It uses confusing logic. It ruins type safety. Use === for all checks. - let in async loops let is block-scoped. Async loops capture live values. Your logs will show the same number. Use a const inside the loop. - The this keyword Arrow functions do not bind this. They use the outer scope. This leads to undefined. Use regular functions for methods. - JSON and Dates JSON.parse returns strings. It does not create Date objects. Your code will crash. Use a reviver function. Source: https://lnkd.in/g4yDDjNe
To view or add a comment, sign in
-
#javascript #javascriptTips Converting input type number by using parseInt or Number() function or using a plus operator for instance const num = +value. We can instead use the valueAsNumber property which gives the value as number type simple 🪄 Picture Credit: Steve Sewell 🙌
To view or add a comment, sign in
-
-
JavaScript - Debouncing for Search Input Problem: When typing in a search bar, multiple API requests are sent for each keystroke, leading to performance issues. Solution: Use a debounce function to limit the frequency of API requests, triggering the search only after the user has stopped typing for a specified amount of time.
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
I was trying to buy your course node.js on namaste dev but I am facing a problem...