1

The first prompt of the following program asks for name = prompt("Who's there","");. If i press esc or cancel the prompt variable name should be null (it is null i checked in Mozilla's watch expression) and hence should execute the else if (name == '' || name == null) and alert the user as Canceled. But the program skips the above said else if and executes the last else there by showing "I don't know you". If i change variable declaration to include keyword let and modify code as let name = prompt("Who's there",""); it works as it should and on pressing esc or cancel of first prompt it displays Canceled.

What changes when i do not declare a variable using let?

code:

login();

function login(){
    name = prompt("Who's there","");
    if (name == 'Admin')
    {
        password = prompt("Password?","");
        if (password == 'TheMaster')
        {
            alert("Welcome!");
        }
        else if (password == '' || password == null)
        {
            alert("Canceled");
        }
        else
        {
            alert('Wrong password');
        }
    }
    else if (name == '' || name == null) 
    {
        alert("Canceled");
    }
    else
    {
        alert("I don't know you");
    }
} 
 
<!DOCTYPE html>
<html lang="en-US">
    <head>
        <title>Login example with JS</title>
        <meta charset="utf-8">
        <script type="text/javascript" src="../HTML/scripts/login.js"></script>
    </head>
    <body>
        <h1>Login Page using prompt</h1>
    </body>
</html>
Mod
  • 65
  • 6
  • you check for null with isnull() funciton - or something like that – Jon Nezbit Aug 22 '20 at 13:00
  • 6
    Gloabl `name` can't be `null`, since it's a "type-protected" property of `window` (always a string). – Teemu Aug 22 '20 at 13:03
  • 5
    Without `let` (or `var`), the variable is an "implicit global". In this particular case, it's especially bad: browsers have a global variable `name` that reflects the name of the window. **Always declare variables explicitly with `let` or `const`.** – Pointy Aug 22 '20 at 13:03
  • 1
    More about implicit globals on my old anemic blog: [*The Horror of Implicit Globals*](http://blog.niftysnippets.org/2008/03/horror-of-implicit-globals.html). Using `let` means you're shadowing the automatic `name` global that comes from the `window `object. – T.J. Crowder Aug 22 '20 at 13:06
  • 1
    "_(it is null i checked in Mozilla's watch expression)_" Take a careful look at the value, it's not `null`, rather it's `"null"`. – Teemu Aug 22 '20 at 13:25
  • @Teemu so as you said Gloabl name can't be null, since it's a "type-protected" property of window (always a string). "null" is a string and not a type- null.? – Mod Aug 22 '20 at 13:32
  • 1
    Yep, you can check this quickly on the console, just write `name = null` and hit enter, the console responses with a string "null". `window.name` property is actually a getter/setter pair, and the setter makes a type coercion before storing the value internally. – Teemu Aug 22 '20 at 13:33

0 Answers0