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:
if (test === 0) {
console.log('+++');
} else {
console.log('---');
}
You can shorten it like this:
if (test === 0) console.log('+++'); else console.log('---');
Или так:
if (test === 0) {
console.log('+++');
} else console.log('---');
You can also remove all the brackets, but arrange everything not in one line, but like this:
if (test === 0)
console.log('+++');
else
console.log('--');
Rewrite the following code in shortened form:
if (test > 0) {
console.log('+++');
} else {
console.log('---');
}
Rewrite the following code in shortened form:
if (test > 0) {
console.log('+++');
}