Ga naar hoofdinhoud

if

De eenvoudigste voorwaarde is een if-statement en de syntaxis hiervan is if(condition){ doe dit … }. De voorwaarde moet waar (true) zijn om de code tussen de acolades uit te voeren. Je kunt bijvoorbeeld een string testen en de waarde van een andere string afhankelijk maken van die waarde:

const country = "France";
let weather;
let food;
let 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;

Opmerking: Voorwaarden kunnen ook in elkaar worden genest.

opgave

Vul de waarde van name in om aan de voorwaarde te voldoen.

Startcode

const name =

if (name === "John") {

}
Antwoord
const name = "John";

if (name === "John") {

}

Validatie

assert(name === "John");