Start learning free

LESSON 195 OF 289 · PROJECTS: EVERYDAY PROGRAMS

Password Strength V1 in TypeScript: Checking the Length

Practice Read the full explanation

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

You start with:

const password: string = "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: string = "Secret99";

const length: number = 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

    TYPESCRIPT · PROJECTS: EVERYDAY PROGRAMS

    Password Strength V1 in TypeScript, explained

    Problem overview

    This is the first version of a small password strength checker, and it looks at one thing only: how many characters the password has. The starter gives a constant, const password: string = "Secret99";. The program prints the length on one line, then a verdict on the next: OK when the length is at least 8, Too short otherwise.

    Password length: 8
    OK

    With the password abc the output is Password length: 3 followed by Too short.

    Approach: measure once, then decide

    The length is needed twice, once to print it and once to compare it with 8. Storing it in a variable avoids asking the string twice and gives the number a name. After that, a single if/else chooses which of the two words to print.

    The comparison is length >= 8, because the rule says "at least 8", so a password of exactly 8 characters passes. The else branch needs no condition of its own: it runs for every length the if rejected, which is 7 or fewer.

    A length rule is the weakest possible check, and that is the point of starting here. A password like aaaaaaaa passes it, and so does 12345678. The next version builds on this one.

    Step by step

    1. Store the length. Create a const named length, typed as number, from password.length. In TypeScript length is a property of a string, so it has no parentheses.
    2. Print the first line. Join the label "Password length: " and the number with +, which converts the number to text because the other operand is a string.
    3. Write the if. When length >= 8, print OK.
    4. Add the else. It prints Too short. Write else on the same line as the closing brace of the if.

    The solution in TypeScript

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

    Some details for beginners:

    • const is right for length because it is assigned once and never changes.
    • The types, string and number, are checked by the compiler and then erased. The JavaScript that runs has no types.
    • "Password length: " + length joins text and a number into text. If both operands were numbers, + would add them instead, so the text always has to be on one side.

    Printing the first line before the verdict matters too: the output is two lines in a fixed order, so the first console.log must come before the if, and the verdict is printed by exactly one of the two branches.

    Edge cases and common mistakes

    • Writing > 8 instead of >= 8. The sample password has exactly 8 characters and would be rejected.
    • Writing password.length(). length is a number, not a function. The compiler reports This expression is not callable, and the JavaScript that still gets written fails at run time with TypeError: password.length is not a function.
    • The empty string. With "" the output is Password length: 0 and Too short.
    • Spaces count. "Pass word" has 9 characters, and the output is Password length: 9 and OK.
    • Length counts UTF-16 code units, not what you see as characters. A single emoji such as the grinning face has length 2, while [...password].length counts it as 1. Plain letters and digits, like the ones in this lesson, always count as 1 each.

    Testing

    Change password in the editor above and compare:

    passwordoutput
    Secret99Password length: 8, then OK
    abcPassword length: 3, then Too short
    empty stringPassword length: 0, then Too short
    Pass wordPassword length: 9, then OK
    Keep going with TypeScript1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free