JavaScript

JavaScript: Syntax

JavaScript: Syntax

Understanding the syntax of JavaScript is important for having a better understanding of the language. Let us start by learning how to implement JavaScript. A typical JavaScript code starts with <script> HTML tag and ends with </script> HTML tag. The code or the JavaScript statements are written in between the <script>___</script> HTML tags. 

The function of the <script>___</script> HTML tag is independent of the tag’s positioning in the code, meaning these HTML tags can be used freely in the code (according to your liking). Enclosing <script> HTML tags within the <head> is advised to practice uniformity across the code and avoid any unnecessary confusions that can turn into potential errors. The function of <script> HTML tags is to initiate the process of interpretation by informing the browser to consider all the text in between the <script>___</script> HTML tags as script and start interpreting it.

Following is an example of a JavaScript code describing how to implement the <scripts>___ </scripts> HTML tags in the JavaScript code: 

<script ___>
   Code of your JavaScript application
</script>

The important attributes defined when implementing the <script> HTML tag are:

  • <script language= “___” …>: The language attribute is used to define the scripting language being used for programming. When developing an application using JavaScript programming, the value of the language attribute will be “javascript”. Recently, the language attribute has been phased out in the updated versions of HTML and XHTML. 
  • <script … type=“___”>: As language attributes are being phased out from recent updates of HTML versions, the attribute that is now being used to define the scripting language used for programming is the type attribute. The value of the type attribute is “text/javascript”, when developing applications using JavaScript programming.

Following is an example of a code describing how to define the attributes within the script HTML tag when using JavaScript programming to develop applications:

<script language = "javascript" type = "text/javascript">
   Code of your JavaScript application
</script>

JavaScript Program: “This is my first attempt.”

You will write your first program. Let us use JavaScript for printing “This is my first attempt.” as an output of a program. You will also write comments to make your program more readable and understandable. In this case, a comment is used to prevent the code from being read by browsers incompatible with JavaScript. The double slashes “//” indicates the start of the comment, whereas —> is used to separate the JavaScript code from the HTML comment, which helps the browser to differentiate between them easily. To print the text “This is my first attempt.”, the function document. Write is used, in which the string required to be printed is written. The function can be used by enclosing both text and HTML. 

Following is an example of a code that can be used to print the text “This is my first attempt.”: 

<html>
   <body>   
      <script language = "javascript" type = "text/javascript">
         <!--
            document.write("This is my first attempt.")
         //-->
      </script>      
   </body>
</html>

The output for the program is:

This is my first attempt. 

About Whitespace and Line Breaks

JavaScript ignores the use of spaces, tabs, or any newlines. But, cultivating a practice to use them is undoubtedly an excellent habit to have, as they allow you to format your program, make it more readable and presentable, and it also helps yourself as well as others to understand the written program when any future references are made to the program. 

The Use of Semicolons 

Semicolons are used to end the JavaScript statements, and they assist the browser in differentiating between one JavaScript statement from another JavaScript statement. But, there is a way to terminate the use of semicolons in JavaScript; this can be done by writing each JavaScript statement in a separate line. 

Following are two examples of code that can be substituted in place of each other. 

When the JavaScript statements are written in separate lines, then the use of semicolons can be eliminated.

<script language = "javascript" type = "text/javascript">
   <!--
      vara = 3
      varb = 4
   //-->
</script>

When the JavaScript statements are written in the same line, using semicolons becomes essential, as the browser needs to differentiate between the two statements.

<script language = "javascript" type = "text/javascript">
   <!--
      vara =3; varb =4;
   //-->
</script>

Practicing the use of semicolons is considered to be a good habit in the world of programming. 

About Case Sensitivity

It is essential to pay attention to the case of the keywords used, variables declared, functions deployed, etc.; JavaScript is a case-sensitive language; always remember to type the identifiers in appropriate cases; otherwise, it becomes difficult for the browser to understand the written JavaScript code. 

About Comments

Comments can be used in JavaScript, as discussed earlier. JavaScript supports HTML comment opening and closing sequence, C style, and C++ style comments. 

When you want only to explain a certain fraction of a program, it is best to use single-line comments. Single-line comments begin with a (//) double slash and have to be written in a single line because if the comment is continued in the following line, the compiler will not ignore the comment, and the program will fail to execute. Single-line comments also mention the beginning and end of a particular program block, promoting a proper structuring of the embedded program.

There are points in the program where an explanation cannot suffice in a single line. Then you can use multiline comments. A multiline comment starts with a slash (/), then is followed by an asterisk (/*), and ends with an asterisk (*) mark followed by a slash (*/). The compiler ignores the whole block of code where the multiline comments are written, meaning you can explain the code in multiple lines, and the compiler will ignore these lines of explanation. 

In simpler words, JavaScript will consider any text written after the double slashes until the line ends as a comment, and the browser will ignore it and not consider it as part of the JavaScript code. They can only be used to write single-line comments. Also, any text written between /* and */ will also be considered a comment and will be ignored by the browser. At the same time, you can write multiple lines of comments. 

Similar to // comment, HTML comment opening sequence <!— is considered as a single line comment by JavaScript. Whereas HTML closing sequence —> has to be modified to //—>, as JavaScript cannot understand the former. 

Following is an example of a code comprising the use of all the comments mentioned above in JavaScript: 

<script language = "javascript" type = "text/javascript">
   <!-- (HTML comment opening sequence)
      // A single line comment. They can be compared to the single line comments that we use in C++ language. 
   (For presentation, whitespaces are ignored by the browser)
      /*
      * A multiline comment that can be used to explain a segment of JavaScript code in more detail. 
      * They can be compared to the multi-line comments that are used in C++ language. 
      */
   //--> (HTML comment closing sequence)
</script>