As with every language, JavaScript has conventions on punctuation and how spacing affects readability. Let's take a look at a few ideas:
- Python:
def add_one(x):
x += 1
return x
- Java:
int add_one(int val) {
val += 1;
return val;
}
- C++:
int add_one (int val)
{
val += 1;
return val;
}
- JavaScript:
function addOne(val) {
return ++val
}
In JavaScript, the conventions of the preceding example are as follows:
- No space between the function name and the parentheses.
- A single space before the curly brace, which is on the same line.
- The closing curly brace is on its own line, aligned with the opening statement of function.
There's also one more modern point to make here about JavaScript and the examples we'll be using in this book versus what you may encounter in the field and examples online: semicolons.
With few exceptions, in modern JavaScript, semicolons at the end of statements are optional. It used to be a best practice to always terminate...