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:

  1. var country = 'France';
  2. var weather;
  3. var food;
  4. var currency;
  5. if(country === 'England') {
  6. weather = 'horrible';
  7. food = 'filling';
  8. currency = 'pound sterling';
  9. }
  10. if(country === 'France') {
  11. weather = 'nice';
  12. food = 'stunning, but hardly ever vegetarian';
  13. currency = 'funny, small and colourful';
  14. }
  15. if(country === 'Germany') {
  16. weather = 'average';
  17. food = 'wurst thing ever';
  18. currency = 'funny, small and colourful';
  19. }
  20. var message = 'this is ' + country + ', the weather is ' +
  21. weather + ', the food is ' + food + ' and the ' +
  22. '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 %}