JavaScript
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax and Types
-
Section 3: Strings and Numbers in Depth
-
Section 4: Control Flow
-
Section 5: Functions
-
Section 6: Objects and Arrays
-
Section 7: Maps, Sets, and Symbols
-
Section 8: Asynchronous JavaScript
-
Section 9: Object-Oriented and Prototypes
-
Section 10: The DOM
-
Section 11: Browser APIs
-
Section 12: Modern JavaScript (ES2015-ES2025)
-
Section 13: Functional Programming Patterns
-
Section 14: Error Handling and Debugging
-
Section 15: Testing
-
Section 16: Accessibility for JavaScript Developers
-
Section 17: Internationalization and Localization
-
Section 18: Performance
-
Section 19: Node.js Fundamentals
-
Section 20: Regular Expressions
-
Section 21: Design Patterns in JavaScript
-
Section 22: Security Basics
-
Section 23: Data Structures and Algorithms in JavaScript
-
Section 24: Practical Projects
-
Section 25: More Advanced Async Patterns
-
Section 26: More Object and Class Practice
-
Section 27: Working with Dates and Internationalization
-
Section 28: Web Components
-
Section 29: More DOM and Browser Practice
-
Section 30: Build Tooling for Vanilla JavaScript
-
Section 31: More Practice Projects
-
Section 32: Interview and Algorithm Practice
-
Section 33: Error Objects (MDN Reference)
-
Section 34: TypedArrays and Binary Data
-
Section 35: Reflection and Metaprogramming (MDN Reference)
-
Section 36: More Global Functions (MDN Reference)
224: Building a Password Strength Meter
Look, I've seen this a dozen times in code reviews: a developer builds a password strength meter and thinks they've nailed it because they're checking the length of the string. They write a simple if (password.length > 8) block and call it "Strong."
The "Length Equals Security" Fallacy
Here is why that approach fails miserably in the real world. Imagine a user enters 1234567890. That's ten characters—well over the typical eight-character threshold. By a length-only logic, your app would tell the user their password is "Strong," even though any basic brute-force script would crack that in milliseconds. Length is important, sure, but it's only one piece of the puzzle. Security is actually about entropy—the measure of unpredictability.
Measuring Entropy through Character Diversity
To build a meter that actually means something, you need to check for a variety of character sets. I usually recommend a weighted scoring system. Instead of a binary "strong or weak," we assign points for each criterion met. If the password has uppercase letters, it gets a point. Numbers? Another point. Special characters? Another point.
The most efficient way to handle this in JavaScript is using Regular Expressions (Regex). You don't need to loop through every single character manually; you just test the string against a pattern.
const checkStrength = (password) => {
let score = 0;
if (password.length > 8) score++;
if (password.length > 12) score++; // Bonus for extra length
if (/[A-Z]/.test(password)) score++;
if (/[0-9]/.test(password)) score++;
if (/[^A-Za-z0-9]/.test(password)) score++;
return score;
};
Now, a 10-digit string of zeros will only score 1 point (for length), while P@ssw0rd123! will score a perfect 5. This gives the user a much more honest representation of their security.
Connecting Logic to Visual Feedback
A strength meter isn't helpful if it's just a number in the console. You need to map that score to a visual element, like a color-coded bar. I prefer using a data-attribute or a CSS class to handle the styling, keeping the JavaScript focused on the logic rather than the pixels.
You'll want to attach an event listener to the password input—specifically the input event, not change. The change event only fires when the user leaves the field, but the input event fires on every single keystroke, giving that "real-time" feel users expect.
const passwordInput = document.querySelector('#pwd');
const meterBar = document.querySelector('#meter');
passwordInput.addEventListener('input', () => {
const score = checkStrength(passwordInput.value);
// Map the score (0-5) to a CSS class
const strengthLevels = ['very-weak', 'weak', 'fair', 'strong', 'very-strong'];
const level = strengthLevels[score] || 'very-weak';
meterBar.className = `meter-bar ${level}`;
});
One last tip: don't forget to handle the empty state. If the input is empty, the bar should probably disappear or reset to a neutral state, rather than showing "Very Weak," which can feel like the app is yelling at the user before they've even started typing.
📋 Practical Task
Build a Real-Time Password Entropy Validator
Create a password input field and a strength indicator bar. Your task is to implement a JavaScript function that calculates a strength score based on the following criteria:
- Minimum 8 characters (1 point)
- Contains at least one uppercase letter (1 point)
- Contains at least one lowercase letter (1 point)
- Contains at least one number (1 point)
- Contains at least one special character (e.g., !, @, #, $) (1 point)
- 0-2 points: Red (Weak)
- 3-4 points: Yellow (Medium)
- 5 points: Green (Strong)
input event.There are no comments for now.