The prompt statment creates a simple dialog that has a text field, with a custom message between the first set of quotes and the default value in the second set.
Don't include the second value if you don't want there to be a default value.
It will return the text that you entered into the prompt.
var =
<script>
var text="Text!";
text="New text.";
var number=7;
var boolean=true;
var array=["Value 1",2,false];
</script>
When using a variable for the first time, use the 'var' prefix.
To write to a variable, put the variable's name, an equals sign (=), and then the value.
For a text value, put quotes around it. For a number value, just put the number without the quotes. For a boolean (true or false), you can either write true or false without any quotes - just make sure they're lower case.
You can actually store multiple values inside one variable! Just put an opening square bracket ([) followed by each of the values separated by a comma (,). Once you have added each value, put a closing square bracket (]).
To read the value of a variable that is not an array, just put the variable's name without any quotes. If it is an array, put the variable's name without any quotes, folowed by an opening square bracket, then the number of which item you want to access, then a closing square bracket. Note: 0 is the first item, followed by 1 which is the second item, and so on.
function(){}
<script>
function message(text){ alert(text);
}
message("This is using a function!");
</script>
The function statement can group code for using multiple times.
It is also useful if you want a button press to do multiple things.
You can pass arguments to the function by including the name between brackets when defining the function. They will be passed as a varible that only that function can access.
if, else, and else if
<script>
var name=prompt("What's your name?");
if (name=="Hop"){ alert("We have the same name!");
} else if (name=="Hip"){ alert("You have the same name as my friend!");
} else { alert("Your name is not the same as mine or my friend's.");
}
</script>
The if statement can compare two values. Use == for checking whether they're the same, > for checking whether a number is more than another, and < for checking whether a number is less then another.
You can also use >= for checking whether a number is more than or equal to another and <= for checking whether a number is less than or equal to another.
To check for more than one thing, use the else if statement.
To check whether none of the conditions are met, use the else statement at the end.