• USA : +1 973 910 5725
  • INDIA: +91 905 291 3388
  • info@tekslate.com
  • Login

JavaScript Syntax, Comments, Where To

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 you 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.

<script>

JavaScript code

</script>

 

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.

Type:

  • 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

</script>

First JavaScript Program

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

<html>

<body>

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

document.write(“Hello World!);

</script>

</body>

</html>

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 above code will display following result:

Hello World!

Whitespace and Line Breaks

JavaScript ignore 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

//à

</script>

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

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

<!—

var1=10; var2=20;

//à

</script>

 

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.

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 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:

<html>

<head>

<script type=”text/javascript”>

<!—

Function sayHello()

alert(“Hello World”)

}

//– >

</script>

</head>

<body>

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

</body>

</html>

 

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:

<html>

<head>

<body>

<script type=”text/javaScript”>

<!—

document.wirte(“Hello Word”)

//– >

</script>

<p>This is webpage body </p>

</body>

</html>

 

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 it automatically.

Summary
Review Date
Reviewed Item
JavaScript Syntax, Comments, Where To
Author Rating
5

“At TekSlate, we are trying to create high quality tutorials and articles, if you think any information is incorrect or want to add anything to the article, please feel free to get in touch with us at info@tekslate.com, we will update the article in 24 hours.”

0 Responses on JavaScript Syntax, Comments, Where To"

    Leave a Message

    Your email address will not be published. Required fields are marked *

    Site Disclaimer, Copyright © 2016 - All Rights Reserved.

    Support


    Please leave a message and we'll get back to you soon.

    I agree to be contacted via e-mail.