w3resource logo


JavaScript Tutorial

JavaScript Syntax and comments

Secondary Nav

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:

w3r video tutorialsWatch write your first JavaScript video tutorial.

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");
       

Double or Single Quotes in JavaScript

There is no preferred method, you can use either. If you use one form of the quote (either single or double) in the string, you may use other as the literal.



Join our Question Answer community to learn and share your programming knowledge.

Help the community:

PHP: Loop

JavaScript: Delete duplicates in an array

Oracle: Round the number