Expand|Select|Wrap|Line Numbers
- if(16==16){
- window.alert("Yes, 16 does equal 16! Amazing!");
- }
- else {
- window.alert("Wait a second... 16 is not equal to 16?");
- }
"Yes, 16 does equal 16! Amazing!" will show up in an alert box. If it does not, "Wait a second... 16 is not equal to 16?" will show up in an alert box. Notice that in the parentheses next to the if the two numbers are compared using two equal signs and not just one:
Expand|Select|Wrap|Line Numbers
- if(16==16)
The code shown above, however, is not really useful. A way of making it useful is shown below:
Expand|Select|Wrap|Line Numbers
- var age=prompt("What is your age?");
- if(age<6){
- window.alert("You are under 6. You need the parental password.");
- var pass=prompt("Type in the password to continue.");
- if(pass=="parentpassword"){
- window.alert("The password you inserted is correct. Go on!");
- window.location="google.com";
- }
- else{
- window.alert("Your password is wrong");
- }
- }
- else{
- window.alert("You are over 6. You are good to go.");
- window.location="google.com";
- }