Optionality of curly braces in JavaScript
If there is only one expression in curly
brackets in if or else, these
curly brackets can be omitted. Let, for example,
given the following code with all brackets:
javascriptif (test === 0) {
console.log('+++');
} else {
console.log('---');
}
You can shorten it like this:
javascriptif (test === 0) console.log('+++'); else console.log('---');
Or so:
javascriptif (test === 0) {
console.log('+++');
} else console.log('---');
You can also remove all the brackets, but arrange everything not in one line, but like this:
javascriptif (test === 0)
console.log('+++');
else
console.log('--');
Задание 1
Rewrite the following code in shortened form:
javascriptif (test > 0) {
console.log('+++');
} else {
console.log('---');
}
Задание 2
Rewrite the following code in shortened form:
javascriptif (test > 0) {
console.log('+++');
}