Skip to content

Instantly share code, notes, and snippets.

@SoxFace
Forked from robinsonlam/JSStructure.md
Created November 13, 2015 01:58
Show Gist options
  • Select an option

  • Save SoxFace/6d4712ed66a8889b251e to your computer and use it in GitHub Desktop.

Select an option

Save SoxFace/6d4712ed66a8889b251e to your computer and use it in GitHub Desktop.
Structuring your code in Javascript - Notes for WDI students to help with syntax

Code Structure

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 (if blocks, for, while loops 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) )

Variables

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.

Control Flow Statements

If Statement

if (condition) {
  doWhatever;
}

If & Else Statement

if (condition) {
  doWhatever;
} else {
  doThisInstead;
}

If & ElseIf & Else Statement

if (condition) {
  doWhatever;
} else if (condition2) {
  doWhateverInstead;
} else {
  doThisInstead;
}

Example:

var hungry = true;
if (hungry === true) {
  eatFood;
} else if (hungry === false) {
  doWhateverInstead;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment