var age = prompt("please enter your age");
if (age >= 18) {
alert("you are allowed to enter");
} else {
alert("you are not allowed to enter");
}
if (age > 50) {
alert("welcome to our website!");
}
// in this case, if age is greater than 50, both alerts will be shown because both conditions are checked separately
// and both if structures are independent of each other and true conditions will be executed
<script>
var person = prompt("who's there?");
if (person === "Admin") {
var pass = prompt("password?");
if (pass === "TheMaster") {
alert("Welcome!");
} else {
alert("Wrong password");
}
}
else {
alert("I don't know you");
}
// last example with (?:, ternary if) operator
person === "Admin"
? (prompt("password?")) === "TheMaster"
? alert("Welcome!")
: alert("Wrong password")
: null;
person !== "Admin" ? alert("I don't know you") : null;
// example with switch case statement
switch (person) {
case "Admin":
var pass = prompt("password?");
switch (pass) {
case "TheMaster":
alert("Welcome!");
break;
default:
alert("Wrong password");
break;
}
break;
default:
alert("I don't know you");
break;
}
</script>