Build a password strength checker in JavaScript
Intermediate · 4 min read
Sign-up forms often show a little bar that turns from red to green as you type a password. That bar is a password strength checker. It looks at the password, gives it a score, and turns the score into a word like "weak" or "strong".
In this tutorial you will write strength, a function that scores a password out of six and labels it. Then you will test it on a long passphrase and find out that a simple meter can be confidently wrong. You need to know what functions, objects and strings are. Regular expressions are explained below, just enough for this exercise.
The six rules
The meter gives one point for each rule the password meets:
- At least 8 characters.
- At least 12 characters.
- Has a lowercase letter.
- Has an uppercase letter.
- Has a digit.
- Has a symbol, meaning anything that is not a letter or a digit.
A score of 0 to 2 is weak, 3 or 4 is fair, and 5 or 6 is strong.
Asking "does it contain...?" with a regular expression
A regular expression (regex for short) is a pattern for text, written between slashes. Square brackets mean "any one of these characters", and a dash gives a range. Every regex has a .test(text) method that answers true or false.
console.log(/[a-z]/.test('Lagos')); // true: it has a lowercase letter
console.log(/[A-Z]/.test('lagos')); // false: no capital
console.log(/\d/.test('abc1')); // true: \d means any digit
console.log(/[^A-Za-z0-9]/.test('a b')); // true: the space is not a letter or digit
A ^ at the start of the brackets flips the meaning to "any character that is not one of these". So [^A-Za-z0-9] matches symbols, and a space counts as one.
Here is the exercise. Have a go on your own first if you like, or read on and we will build it step by step.
122 A password strength meter, and what it misses
fairScore a password out of six: at least 8 characters, at least 12, a lowercase letter, an uppercase letter, a digit, a symbol. Label it weak, fair or strong — then score a long passphrase and see whether you believe the answer.
Stuck? Three hints, smallest first
- 1. Build an object of named booleans, then count the true ones.
- 2. A symbol is anything that is not a letter or a digit, so a space counts.
- 3. Return the individual checks too, or the form cannot say which rule failed.
Step 1: one named check per rule
function strength(password) {
const pw = String(password);
const checks = {
length: pw.length >= 8,
long: pw.length >= 12,
lower: /[a-z]/.test(pw),
upper: /[A-Z]/.test(pw),
digit: /\d/.test(pw),
symbol: /[^A-Za-z0-9]/.test(pw),
};
return checks;
}
console.log(strength('Lagos123'));
// { length: true, long: false, lower: true, upper: true, digit: true, symbol: false }
Each property holds true or false. Giving every rule a name matters later: the form can tell the user exactly which rule failed, instead of just "too weak".
String(password) makes sure we are working with text, even if someone passes a number.
Step 2: count the true ones
Object.values(checks) gives the six booleans as an array. filter(Boolean) keeps only the true ones, and .length counts them.
const checks = { length: true, long: false, lower: true, upper: true, digit: true, symbol: false };
const values = Object.values(checks);
console.log(values); // [ true, false, true, true, true, false ]
console.log(values.filter(Boolean).length); // 4
filter(Boolean) works because Boolean is a function that returns true for a true value and false for a false one, which is exactly the test filter wants.
Step 3: turn the score into a label
function strength(password) {
const pw = String(password);
const checks = {
length: pw.length >= 8,
long: pw.length >= 12,
lower: /[a-z]/.test(pw),
upper: /[A-Z]/.test(pw),
digit: /\d/.test(pw),
symbol: /[^A-Za-z0-9]/.test(pw),
};
const score = Object.values(checks).filter(Boolean).length;
const label = score <= 2 ? 'weak' : score <= 4 ? 'fair' : 'strong';
return { score, label, checks };
}
console.log(strength('lagos').score, strength('lagos').label); // 1 weak
console.log(strength('Lagos123').score, strength('Lagos123').label); // 4 fair
console.log(strength('Lagos123!').score, strength('Lagos123!').label); // 5 strong
The label line is two ternaries chained together. Read it left to right: if the score is 2 or less, "weak"; otherwise, if it is 4 or less, "fair"; otherwise "strong".
The function returns all three pieces: the number, the word, and the individual checks.
Step 4: score a passphrase
Now try a long phrase made of ordinary words:
function strength(password) {
const pw = String(password);
const checks = {
length: pw.length >= 8,
long: pw.length >= 12,
lower: /[a-z]/.test(pw),
upper: /[A-Z]/.test(pw),
digit: /\d/.test(pw),
symbol: /[^A-Za-z0-9]/.test(pw),
};
const score = Object.values(checks).filter(Boolean).length;
const label = score <= 2 ? 'weak' : score <= 4 ? 'fair' : 'strong';
return { score, label, checks };
}
console.log(strength('correcthorsebattery').score); // 3
console.log(strength('Lagos123!').score); // 5
console.log(strength('my pass phrase').checks.symbol); // true
console.log(strength('lagos123!').checks.upper); // false
The nineteen-letter phrase scores 3, while the nine-character Lagos123! scores 5. Yet a guessing program has far more work to do with the long phrase. The meter measures whether you followed the rules, not how hard the password is to guess. Rules like these also nudge people toward Password1!, which is one of the first things an attacker tries.
That is why a meter like this should be a hint on the screen, never the thing that blocks a sign-up. Notice too that spaces count as symbols and are allowed. Rejecting them would break passphrases and password managers.
The full solution
The answer panel has the complete strength function and prints the score and label for four passwords, the passphrase included. The checks also cover an empty password, the space, a named failed rule and the maximum score of six. Press Run to see the output.
function strength(password) {
const pw = String(password);
const checks = {
length: pw.length >= 8,
long: pw.length >= 12,
lower: /[a-z]/.test(pw),
upper: /[A-Z]/.test(pw),
digit: /\d/.test(pw),
symbol: /[^A-Za-z0-9]/.test(pw),
};
const score = Object.values(checks).filter(Boolean).length;
const label = score <= 2 ? 'weak' : score <= 4 ? 'fair' : 'strong';
return { score, label, checks };
}
for (const pw of ['lagos', 'Lagos123', 'Lagos123!', 'correcthorsebattery']) {
console.log(pw.padEnd(22), strength(pw).score, strength(pw).label);
}
Output
9 checks that prove this answer
You do not have to read these. They are part of the answer panel above, so pressing
Run there executes all 9 of them and prints ok for each.
That output is the proof, and the build fails if a single check fails.
test('a short lowercase word scores one', [strength('lagos').score, strength('lagos').label], [1, 'weak']);
test('case and digits get it to fair', strength('Lagos123').label, 'fair');
test('one symbol tips it to strong', strength('Lagos123!').label, 'strong');
test('a nineteen-character passphrase scores lower than that', [strength('correcthorsebattery').score, strength('Lagos123!').score], [3, 5]);
test('nothing typed scores nothing', strength('').score, 0);
test('a space is a symbol, and is allowed', strength('my pass phrase').checks.symbol, true);
test('the failed rule is named, so the form can point at it', strength('lagos123!').checks.upper, false);
test('six is the ceiling', strength('Lagos-Ibadan-2026!').score, 6);
test('and length alone carries two of those six', [strength('Lagos123!xxx').score, strength('Lagos123!xxx').label], [6, 'strong']);
The fourth assertion is the lesson, and it is uncomfortable: correcthorsebattery scores 3 while Lagos123! scores 5, and the nineteen-character phrase is enormously harder to guess. Composition rules measure whether you followed the rules, not how hard the password is to find — and they push people towards Password1!, which every cracking dictionary already holds. Current NIST guidance is to require length, allow everything including spaces, screen against known-breached lists, and stop mandating character classes.
So treat a meter like this as a hint in the interface, never as the gate. Two implementation notes. The checks are returned as named booleans rather than folded into a number, because "must contain an uppercase letter" is only actionable if the form can say which rule failed. And [^A-Za-z0-9] counts anything else as a symbol, which correctly includes the space — silently stripping or rejecting spaces breaks passphrases and password managers, and the user finds out at the login screen, not here.
Common mistakes
- Returning only the score. A number can't tell the user which rule they missed. Return the named checks too.
- Writing the symbol rule as a short list like
[!@#$]. It misses every other symbol, including the space. "Not a letter or digit" covers them all. - Treating the meter as security. It rewards
Lagos123!and doubts a long passphrase. Use it as a hint, not a gate.
Try it yourself
- Add a seventh rule: at least 16 characters. Change the label bands so 6 or 7 is "strong".
- Write
missing(password)that returns the names of the rules that failed, like['upper', 'symbol']. - Keep a short list of common passwords, such as
'password'and'123456', and makestrengthreturn a score of 0 for any of them.