Start learning free

LESSON 195 OF 285 · PROJECTS: EVERYDAY PROGRAMS

Password Strength V5 in JavaScript: Common Passwords in an Object

Practice Read the full explanation

Use a dictionary (or set) as a fast lookup table to reject well-known weak passwords.

You start with:

const common = {"password": true, "123456": true, "qwerty": true, "letmein": true};
const password = "qwerty";

Build:

Password: qwerty
Common: True
Weak: reject

Rules:

  • Print "Password: " followed by the password.
  • Look the password up in the object common and store the result as a boolean.
  • Print "Common: " followed by the boolean. Write it as True or False with a capital letter, exactly as in the example (your language's own boolean prints differently, so choose the text yourself).
  • If it is common, print "Weak: reject"; otherwise print "Strong: accept".

Try other passwords: with Tr0ub4dor the output is

Password: Tr0ub4dor
Common: False
Strong: accept

password in common checks whether password is a key of the object common.

const common = {"password": true, "123456": true, "qwerty": true, "letmein": true};
const password = "qwerty";

console.log("Password: " + password);
const isCommon = password in common;
console.log("Common: " + (isCommon ? "True" : "False"));
if (isCommon) {
    console.log("Weak: reject");
} else {
    console.log("Strong: accept");
}

STEP 1 OF 5
    Ctrl + Enter to check

    JAVASCRIPT · PROJECTS: EVERYDAY PROGRAMS

    Password Strength V5 in JavaScript, explained

    Problem overview

    Some passwords are weak whatever their characters: everybody tries qwerty first. This version rejects passwords that appear in a list of well-known ones. The list is an object, common, whose keys are the known weak passwords. The program prints the password, whether it is common, and a verdict.

    For "qwerty" the output is:

    Password: qwerty
    Common: True
    Weak: reject

    For "Tr0ub4dor", which is not in the list, it is:

    Password: Tr0ub4dor
    Common: False
    Strong: accept

    Approach: one lookup instead of a chain of comparisons

    You could compare the password with each entry in turn: password === "password" || password === "123456" || .... That grows with every word you add, and it means scanning the whole list. An object answers "is this a key?" directly. The value stored under each key doesn't matter here; the keys are the data, and true is just a placeholder.

    The program asks the question once, saves the answer in a boolean, isCommon, and uses that boolean twice: to print the Common line and to choose the verdict.

    Step by step

    1. Print the password line: "Password: " joined with password.
    2. Look it up. password in common is true when the text is one of the object's keys, and false otherwise. Store it in the constant isCommon.
    3. Print the Common line. A boolean prints as lowercase true, but the output needs True, so choose the text with the ternary isCommon ? "True" : "False", inside parentheses.
    4. Write the verdict's if. When isCommon is true, print Weak: reject. A common password is rejected whatever else is true of it.
    5. Add the else. It prints Strong: accept.

    The solution in JavaScript

    const common = {"password": true, "123456": true, "qwerty": true, "letmein": true};
    const password = "qwerty";
    
    console.log("Password: " + password);
    const isCommon = password in common;
    console.log("Common: " + (isCommon ? "True" : "False"));
    if (isCommon) {
        console.log("Weak: reject");
    } else {
        console.log("Strong: accept");
    }

    Details worth knowing

    The condition if (isCommon) needs no comparison, because isCommon is already a boolean. Writing if (isCommon === true) would work but says the same thing in more words.

    An object finds a key in roughly constant time, however many keys it holds, so a list of thousands of common passwords costs no more per lookup than a list of four.

    The match is exact. "Qwerty" with a capital is not a key of common, so it prints Common: False. A realistic checker would lower-case the password first, with password.toLowerCase().

    A trap: inherited keys

    The in operator also looks at inherited properties. Every plain object inherits names such as toString and constructor, so "toString" in common is true, even though that word is not in the list. With password = "toString" the program prints Common: True and Weak: reject. That is harmless for this list, but it is a real difference between "is a key of this object" and "is one of my entries".

    Two safer options exist. Object.hasOwn(common, password) checks only the object's own keys: it gives true for "qwerty" and false for "toString". A Set built with new Set(["password", "123456", "qwerty", "letmein"]) holds just those words, and .has("toString") is false. The statement's mention of a set refers to this second option.

    Complexity

    The program does one lookup and a few prints, so it runs in O(1) time on average, apart from the cost of building the object, which is O(m) for m known passwords.

    Edge cases and common mistakes

    • Printing the boolean directly. "Common: " + isCommon gives Common: true, which doesn't match the required True.
    • Leaving out the parentheses around the ternary. The + would run first, and the output would lose its label.
    • Case differences. "QWERTY" is not found unless you convert it to lower case first.
    • Checking the value instead of the key. common[password] is undefined for a missing key, which is falsy, but in expresses the intent better.
    • Printing both verdicts. The else makes sure only one prints.

    Testing

    Change password in the editor above:

    passwordCommon lineVerdict
    "qwerty"Common: TrueWeak: reject
    "Tr0ub4dor"Common: FalseStrong: accept
    "Qwerty"Common: FalseStrong: accept
    Keep going with JavaScript1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free