6. Where to place the <script> element?
6.2. Common rules of syntax and conventions that work their way through all JavaScript.
- Case-sensitive. The function names, variables, language keywords, and all other identifiers have to be written continuously with a consistent capitalization of letters. For example, the variable named "myVariable", "myvariable", and "MYVariable" are treated as three different objects.
- If white space, like tabs and spaces, is not part of a text string and is enclosed in quotations, it will be ignored.
- When you use JavaScript, there are several coding conventions that you need to consider:
- Naming Variable
- For identifiers names, use camelCase.
- Names should all begin with a letter or underscore.
firstName = "Juan";
lastName = "dela Cruz";
fee = 20.00;
tax = 0.21;
- Line Length
Be sure to avoid lines longer than 80 characters for readability. But if your JavaScript statement doesn’t fit into a single line, after a comma or an operator, the best place to split it.
document.getElementById("idName").innerHTML = "Hello Pisay.";
or
idName.innerHTML = "Hello Pisay";
- Spaces around operators
Often put spaces in your JavaScript code between operators (=++-*/) because it makes it look nice and easy to interpret.
var totalFee = fee + (fee * tax);
- To avoid confusion, always use lowercase filenames.
- Statement Rules
- Always terminate or end a simple statement with a semicolon.
- At the end of the first line, placed an opening bracket.
- Before the opening bracket, use one space.
- Without leading spaces, placed the closing bracket on a new line.
Refer to the given example below:
- <html>
- <head>
- <body>
- <script>
- function compute() {
- var fee = 20.00;
- var tax = 0.21;
- var totalFee = fee + (fee * tax);
- return totalFee;
- }
- var firstName = "Juan", lastName = "dela Cruz";
- document.write("Customer name: " + firstName + " " + lastName + "</br>");
- document.write("Your Total Fee is:" + compute());
- </script>
- </body>
- </html>
At the end of the statement, a semicolon informs JavaScript that it is the end of a command.
Ending each statement with a semicolon is a good practice, while line break can also trigger a
command’s end based on the JavaScript standard.
The following is the result of the sample code above