Skip to content

Instantly share code, notes, and snippets.

@nifl
Last active August 29, 2015 13:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nifl/9959427 to your computer and use it in GitHub Desktop.
Save nifl/9959427 to your computer and use it in GitHub Desktop.
JS Conditionals

Conditionals

Conditionals execute certain code that meet specific conditions

Conditional Statements

If, Else

if (someColor == blue) {
  alert("The color is " + someColor);
} else {
  alert("The color is not blue.")
}

If, Else If, Else

Check multiple conditions

if (someColor == blue) {
  alert("The color is blue.");
} else if (someColor == red) {
  alert("The color is red.")
} else {
  alert("The color is neither red nor blue.")
}

Conditional Expressions

var x = y >= 0 ? y : -y;

Conditional expressions can be used as a function argument, statements cannot.

myFunction(y >= 0 ? y : -y)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment