Rewrite the function using '?' or '||'
importance: 4
The following function returns true if the parameter age is greater than 18.
Otherwise it asks for a confirmation and returns its result.
function checkAge(age) {
if (age > 18) {
return true;
} else {
return confirm('Did parents allow you?');
}
}
Rewrite it, to perform the same, but without if, in a single line.
Make two variants of checkAge:
- Using a question mark operator
? - Using OR
||
Using a question mark operator '?':
function checkAge(age) {
return (age > 18) ? true : confirm('Did parents allow you?');
}
Using OR || (the shortest variant):
function checkAge(age) {
return (age > 18) || confirm('Did parents allow you?');
}
Note that the parentheses around age > 18 are not required here. They exist for better readability.