6. Where to place the <script> element?

6.2. Common rules of syntax and conventions that work their way through all JavaScript.


  1. 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. 
  2. If white space, like tabs and spaces, is not part of a text string and is enclosed in quotations, it will be ignored. 
  3. 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:


  1. <html>
  2. <head>
  3. <body>
  4. <script>
  5.        function compute() {
  6.          var fee = 20.00;
  7.          var tax = 0.21;
  8.          var totalFee = fee + (fee * tax);
  9.          return totalFee;
  10.        }
  11.        var firstName = "Juan",  lastName = "dela Cruz";

  12.        document.write("Customer name: " + firstName + " " + lastName + "</br>");
  13.        document.write("Your Total Fee is:" + compute());
  14. </script>
  15. </body>
  16. </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