Javascript Lesson Three

A primitive calculator.



The "if" construction is used by a program to make choices about which bit of code should be run next. It usually takes the form: if(n==4){do something} else {do something else}, or: if(word=="yes"){do something}, though the numbers and variables I've used here are just examples. The curly {} brackets can be left out if there is only one item between them, so if(x==2){y=x+6} could equally be written as if(x==2) y=x+6; though the semicolon is vital if the curly brackets are missing. I always use the curly brackets even if there is only one item between them because it makes it easier to read the code and to avoid making mistakes. The "else" part of the code is only used if there is some alternative code which should only be run if the condition tested by the "if" is false.