JavaScript Syntax, Comments, Where To

05 October, 2020

Related Blogs

JavaScript Syntax

A JavaScript consists of JavaScript statements that are placed within the <script>……</script> HTML tags in web page.

You can place the <script> tag containing your JavaScript anywhere within your web page but it is preferred was to keep it within the <head> tags

The <script> tag alert the browser program to begin interpreting all the text between these tags as a script. So simple syntax of your JavaScript will be as follows.


JavaScript code


  The script tag takes two important attributes Language

  • This attribute specifies what scripting language you are using. Typically, its value will be javascript. Although recent versions of HTML(and XHTML, its successor) have phased out the use of this attribute.
Learn JavaScript by Tekslate - Fastest growing sector in the industry. Explore Online "JavaScript Training" and course is aligned with industry needs & developed by industry veterans. Tekslate will turn you into JavaScript Expert.



  • This attribute is what is now recommended to indicate the scripting language in use and its value should be set to “text/javascript”.

So your javascript segment will look like:

<script language =”javascript” type=”text/javascript”>

JavaScript code


First JavaScript Program

Let us write our class example to print out “Hello World”.



<script language=”javascript”  type=”text/javascript”>

document.write(“Hello World!);




Next, we call a function document .write which writes a string into our HTML document. This function can be used to write text, HTML, or both. So the above code will display the following result: Hello World! Whitespace and Line Breaks

JavaScript ignores spaces, tabs, and newlines that appear in JavaScript programs. Because you can use spaces, tabs, and newlines freely in your program so you are free to format and indent your program in a neat and consistent way that makes the code easy to read and understand.

Semicolons are optional

Simple statements in JavaScript are generally followed by a semicolon character, just as they are in C, C++, and Java, JavaScript, however, allows you to omit this semicolon if your statements are each placed on a separate line, For example, the following code could be written without semicolons

<script language=”JavaScript” type=”text/JavaScript”>

<!— var1=10 var2=20 //à


But when formatted in a single line as follows, the semicolons are required:

<script language=”javascript” type=”text/javascript”>

<!— var1=10; var2=20; //à



Note It is a good programming practice to use semicolons.

Case Sensitivity

JavaScript is a case-sensitive language. This means that language keywords, variables, function names, and any other identifiers must always be typed with a consistent capitalization of letters. So identifiers Time, Time, and TIME will have different meanings in JavaScript.

Check Out JavaScript Tutorial

NOTE Care should be taken while writing your variable and function names in JavaScript

JavaScript Placement in HTML File

There is a flexibility given to include JavaScript code anywhere in an HTML document. But there are the following most preferred ways to include JavaScript in your HTML file.

Script in <head> … </head> section

Script in <body> …  <.body>

Section Script in <body> …. </body>

and <head> …..  </head> sections.

Script in and external file and then include in <head>--- </head> section.

In the following section we will see how we can put JavaScript in different ways:  

JavaScript in <head>…</head> section:

If you want to have a script run on some event, such as when a user clicks somewhere, then you will place that script in the head as follows:



<script type=”text/javascript”>

<!— Function sayHello() alert(“Hello World”) } //-- >




<input type=”button” onclick=”sayHello()”  value=”Say  Hello”   />



JavaScript in <body>…  </body< section:

if you need a script to run as the page loads so that script generates content in the page, the script goes in the <body> portion of the document. In this case, you would not have any function defined using Javascript:




<script type=”text/javaScript”>

<!— document.wirte(“Hello Word”) //-- > </script>

<p>This is webpage body </p>



JavaScript in <body> and <head> sections:

You can put your JavaScript code in <head> and <body> section altogether as follows: <html> <head> Javascript is untyped language. This means that a javascript variable can hold a value of any data type. Unlike many other languages. you don’t have to tell javascript during variable declaration what type of value the variable will hold. The value type of a variable can change during the execution of a program and JavaScript takes care of it automatically.