Tag: Web development

  • DevDose: Understanding JavaScript Equality Operators in Practice

    Day Eight of Devdose

    Introduction
    A recent debugging session reminded me how JavaScript’s equality operators can trip up even experienced developers. The confusion between == and === often leads to subtle bugs, so I wanted to share a practical perspective on their differences.

    Real-World Example

    In a Node.js application, I encountered a bug where a user authentication check was failing unexpectedly. The code compared a user input string with a database value using ==. The input “123” was being compared to the database value 123, and the loose equality operator coerced the types, allowing the comparison to pass when it shouldn’t have. This exposed a security flaw, as invalid inputs slipped through.

    Code Snippet

    Consider this example:

    const userInput = "123";
    const dbValue = 123;
    
    console.log(userInput == dbValue);  // true (loose equality, type coercion)
    console.log(userInput === dbValue); // false (strict equality, no coercion)

    The == operator converts operands to the same type before comparison, which can lead to unexpected results, like 0 == false evaluating to true. The === operator checks both value and type, ensuring precision.

    Insight and Reasoning

    The key difference lies in type coercion. Loose equality (==) tries to be forgiving by converting types, which can obscure bugs in dynamic systems like JavaScript applications. Strict equality (===) enforces type safety, making code behavior predictable and explicit. In the authentication bug, switching to === ensured inputs matched both value and type, closing the vulnerability. While == might save a few keystrokes in rare cases, its unpredictability outweighs the convenience in most production scenarios. My rule: always use === unless there’s a specific, well-documented reason to allow coercion.

    Conclusion

    Choosing between == and === isn’t just a style preference; it’s about reliability and clarity. Defaulting to strict equality reduces debugging time and strengthens code integrity.

  • DevDose: Three Ways to Centre a div with CSS

    Day Seven of Dailydose

    This came up while mentoring a junior developer who was puzzled by layout inconsistencies. We explored different centring techniques and why they behave the way they do.

    One real-world case was a login box that wouldn’t stay centred in all screen sizes. We tested a few methods to find the one that best respected responsiveness and minimal CSS.

    Here are three standard techniques for centring a div both vertically and horizontally:

    /* Flexbox */
    .parent {
      display: flex;
      justify-content: center;
      align-items: center;
    }
    
    /* Grid */
    .parent {
      display: grid;
      place-items: center;
    }
    
    /* Margin Auto (horizontal only, needs fixed width) */
    .child {
      margin: 0 auto;
      width: 200px;
    }

    Each method solves a slightly different problem. margin: auto works well for horizontal centring of fixed-width elements. display: flex is more flexible and widely used, especially when vertical alignment is also needed. grid with place-items: center is concise and powerful, but less commonly used outside of fully grid-based layouts.

    In mentoring or code review, I prefer starting with flexbox for its balance of clarity and control, then switching based on project constraints.

    Choose the method that matches the layout’s intent and scale.