w3resource

JavaScript Syntax and comments

JavaScript Syntax

JavaScript syntax refers a set of rules which define how the language is written and interpreted by the browser. Here are some basic rules for JavaScript syntax:

JavaScript: Case sensitivity

JavaScript is case sensitive. Therefore when we write any JavaScript word (for example "else") we must maintain the proper case. As JavaScript is case sensitive , a variable name "empcode" is not the same as "Empcode" or a function name "PayCalculation" is not the same as "paycalculation".
Always remember everything within <script type="text/javascript">and <script> tags (which are HTML tags) are case sensitive.

Whitespace

In general, whitespace is not visible on the screen, including spaces, tabs and newline characters. Whitespace enhances the readability of the JavaScript code. If used with a string constant, JavaScript considers it, but if not so, it ignores whitespace.

The following codes are equal.

HTML Code

<script language="JavaScript">
empcode="emp123";
<script>

HTML Code

<script language="JavaScript">
empcode = "emp123";
<script>

The following codes are not equal.

HTML Code

<script language="JavaScript">
empcode="emp123";
<script> 

HTML Code

<script language="JavaScript">
empcode = "emp  123";
<script>

The Semi-Colon

In general, JavaScript does not require a semi-colon at the end of a statement. If you write two statements in a single line then a semi-colon is required at the end of the first statement. However, experienced programmers prefer to use a semi-colon at the end of each statement to make the code more readable and fix errors easily.

The following codes are equal.

JS Code

alert("JavaScript Semi-Colon")
alert("JavaScript Semi-Colon")

JS Code

alert("JavaScript Semi-Colon");
alert("JavaScript Semi-Colon")

JavaScript Comments

Code written within comments are not processed by the interpreter. Comments are good way to write notations to explain what a script does. JavaScript supports the following two different ways of commenting, single line comment and multiple line comments.

Single line comment

// This is a single line comment.

Example of single line comments

JS Code

// This is a single line comment
alert("We are learning JavaScript single line comment");
//alert("Good Morning.");

Multiple line comments

In JavaScript multi-line comments start with /* and end with */.

Example of multiple line comments

JS Code

/* Multiple line comments start here
alert("Good Morning");
Multiple line comments end here*/
alert("We are learning JavaScript multiple
line comments.");

Example of comments at the end of a line

JS Code

var empcode // declaring employee code 
var empname // declaring employee name
alert("We are learning how to put
comments at the end of a line");

Previous: Embedding JavaScript in HTML
Next: JavaScript: Tools to create Web Application

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.