Start learning free

LESSON 192 OF 671 · PROJECTS: EVERYDAY PROGRAMS

Password Strength V1 in JavaScript: Length Check with if/else

Practice Read the full explanation

A tiny password strength checker that looks only at the length of the password.

You start with:

const password = "Secret99";

Build:

Password length: 8
OK

Rules:

  • Print "Password length: " followed by the number of characters.
  • If the length is at least 8, print OK; otherwise print Too short.

Try other passwords: with abc the output is

Password length: 3
Too short

password.length is a property (no parentheses).

const password = "Secret99";

const length = password.length;
console.log("Password length: " + length);
if (length >= 8) {
    console.log("OK");
} else {
    console.log("Too short");
}

STEP 1 OF 4
    Ctrl + Enter to check

    JAVASCRIPT · PROJECTS: EVERYDAY PROGRAMS

    Password Strength V1 in JavaScript, explained

    Problem overview

    This is the first version of a small password checker. It starts with a password in a constant, password, and prints two lines: the number of characters, and a verdict. A password of at least 8 characters is OK, and a shorter one is Too short. The only thing the program looks at is the length.

    For "Secret99" the output is:

    Password length: 8
    OK

    For "abc" it is:

    Password length: 3
    Too short

    Approach: measure once, use the number twice

    The length is needed in two places: it goes into the first line of output, and the verdict compares it with 8. So the program reads it once, stores it in a constant named length, and uses that name in both places. Then a two-way if/else chooses which verdict to print.

    The boundary is the part to get right. The rule says "at least 8", so a password with exactly 8 characters passes. In code, "at least" is >=.

    Step by step

    1. Store the length. A string's .length property holds its number of characters. It is a property, so there are no parentheses: password.length, not password.length().
    2. Print the first line. Join the label "Password length: " and the number with +. When one side of + is a string, JavaScript turns the number into text.
    3. Write the if. Its condition is length >= 8, and its body prints OK.
    4. Add the else. It runs for every length the if did not take, which is 7 or fewer, and prints Too short. The else goes on the same line as the closing brace of the if.

    The solution in JavaScript

    const password = "Secret99";
    
    const length = password.length;
    console.log("Password length: " + length);
    if (length >= 8) {
        console.log("OK");
    } else {
        console.log("Too short");
    }

    Details worth knowing

    Calling password.length() with parentheses is a common slip. length holds a number, and a number can't be called like a function, so JavaScript throws a TypeError. In JavaScript, length is a plain property of strings and arrays alike.

    The constant is named length, which is also the name of a property. That is fine: length on its own is the constant, and password.length is the property of the string.

    The .length of a string counts UTF-16 code units, not what a person would call characters. For ordinary letters and digits the two are the same. A single emoji such as "😀" has a length of 2, so a password made of four emoji would pass the length rule of 8. The lesson's rule is about the number .length gives, so the program follows it.

    The if/else runs exactly one of its two branches. There is no input for which both lines print, or neither, and that guarantees one verdict.

    Edge cases and common mistakes

    • Using > instead of >=. A password of exactly 8 characters would be rejected, so "abcdefgh" would print Too short although it is 8 characters long.
    • Off by one the other way. length >= 7 would accept 7 characters.
    • The empty string. "" has a length of 0, so the output is Password length: 0 and Too short.
    • Printing the number and the text on separate lines. The first line is the label and the number together, so it needs one console.log.
    • Typing a space after OK. Output is compared exactly, so keep the text as given.

    Testing

    Replace the password in the editor above and compare with these results. Lengths 7 and 8 are the interesting pair.

    passwordOutput
    "Secret99"Password length: 8, then OK
    "abcdefgh"Password length: 8, then OK
    "abcdefg"Password length: 7, then Too short
    ""Password length: 0, then Too short

    Later versions of this project add more rules to the same program. Keeping the length in a named constant, as here, is the habit that makes such additions easy.

    Keep going with JavaScript650+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free