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>