Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
59 lines (47 loc) · 1.15 KB

File metadata and controls

59 lines (47 loc) · 1.15 KB
Copy raw file
Download raw file
Edit and raw actions

Condition If

The easiest condition is an if statement and its syntax is if(condition){ do this … }. The condition has to be true for the code inside the curly braces to be executed. You can for example test a string and set the value of another string dependent on its value:

var country = "France";
var weather;
var food;
var currency;

if (country === "England") {
  weather = "horrible";
  food = "filling";
  currency = "pound sterling";
}

if (country === "France") {
  weather = "nice";
  food = "stunning, but hardly ever vegetarian";
  currency = "funny, small and colourful";
}

if (country === "Germany") {
  weather = "average";
  food = "wurst thing ever";
  currency = "funny, small and colourful";
}

var message =
  "this is " +
  country +
  ", the weather is " +
  weather +
  ", the food is " +
  food +
  " and the " +
  "currency is " +
  currency;

Note: Conditions can also be nested.

{% exercise %} Fill up the value of name to validate the condition. {% initial %} var name =

if (name === "John") {

} {% solution %} var name = "John";

if (name === "John") {

} {% validation %} assert(name === "John"); {% endexercise %}

Morty Proxy This is a proxified and sanitized view of the page, visit original site.