Here are some notes I've made to help you guys with your syntax. Think of it like a "cheatsheet". Some tips to remember:
- Keep an eye on semicolons, usually control flow blocks (
ifblocks,for, whileloops etc.) do not need a semicolon. - Keep an eye on your opening and closing brackets (
{ & }). - When checking conditions with logical operators (
&&, ||, !), you cannot combine your conditions.- Incorrect:
if ( chicken === yummy && tasty ) - Correct:
if ( (chicken === yummy) && (chicken === tasty) )
- Incorrect:
var variableName = value; // Numbers, Booleans
var variableName = "value"; // Strings
var variableName = [element, element]; // Arrays
var variableName = { // Objects
key: value,
key: value
};
variableName: The name for your variable. value: The value you want to assign to your variable.
Arrays: element: The item you want to put into the array (collection).
Objects: key: The "label" you wish to give to your item. value: The actualy item you want to put in your object.
Example:
var age = 32
This creates a variable named age with a value of 32.
if (condition) {
doWhatever;
}
if (condition) {
doWhatever;
} else {
doThisInstead;
}
if (condition) {
doWhatever;
} else if (condition2) {
doWhateverInstead;
} else {
doThisInstead;
}
Example:
var hungry = true;
if (hungry === true) {
eatFood;
} else if (hungry === false) {
doWhateverInstead;
}