CHAPTER 8If
If
Evaluates the condition, and if the resulting value is true
, executes the code inside the braces.
if (password === '123456') {
console.log('You are now logged in.');
}
If-else
if (password === '123456') {
console.log('You are now logged in.');
} else {
console.log('Wrong user/password combination.');
}
If-else-if
if (password === '') {
console.log('The password is empty.');
} else if (password === '123456') {
console.log('You are now logged in.');
} else {
console.log('Wrong user/password combination.');
}
Boolean Conversion
When the if
condition is not a boolean
, the value is auto-converted (once again), but this is not so bad:
if (a) | number | a != 0 | if a is not zero |
if (s) | string | s.length > 0 or s !== "" | if s is not empty |
if (x) | object | s !== null or s !== undefined | if x is not null or undefined |
Switch
switch (value) {
case v1:
// ...
break;
case v2:
// ...
break;
// ... more cases ...
default:
// ...
}