June 5, 2023

JavaScript – Program Syntax

As you know, JavaScript programs are  placed inside a web page. You place the code inside <script> </script> tag.

The <script></script> tag can be placed anywhere in the web page. So when the browser encounters a <script></script> tag, it would then interpret the code as a JavaScript code.

For example, the code below displays an alert to the screen.

 

<html>
<head>
<title> Welcome to JavaScript</title>
</head>

<body>

<script>
alert('This your first JavaScript program');
</script>

</body>

</html>

 

 

How to Run JavaScript Program

I would recomend you download NotePad++. It’s free. You use it for editing your code. Although you can use NotePad but NotePad++ have more features. Download Notepad++ from here.

Open NotePad++ and enter the code.

Now, Save it as Prog1.html.(you can also use another name). In the Save As type, choose Hypertext Markup Language. Save it in a new folder.

Open the location you saved the file and double-click to open it.

You will notice, it opens a web page and displays an alert

 

 

Attribute of the Script Tag

The script tag has two important attributes: Language and Type.

Language – The language attribute is used to specify the scripting language. In this case, it is “javascript”. However, recent versions of HTML makes this tag optional. So you can well omit it. This is as we did in the program above.

Type: Again this attribute is optional. Your program would still work without it. However, this is the attribute recommended at this time. The value is set to “text/javascript”

So if we rewrite our program, we would have:

 

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

alert('This your first JavaScript program');

</script>

 

 

Using document.write()

The document.write() function is used to write a text to the html document. This is the text the user will see in the html page. The code below uses document.write(). Create a new html file with the following content:

 

<html>

<head>
    <title>Your Second Program</title>
</head>

<body>   
   <script language = "javascript" type = "text/javascript">
	document.write("Welcome to JavaScript. Enjoy!")
   </script>      
</body>

</html>

 

 

Comments in JavaScript

There are different syntax for comment in JavaScript.

You can enclose comments using /* and */. This can also be multiple line comment

Single-line comment can be specified using //

HTML comment begins with <!– (JavaScript as well as single-line comment)

The HTML comment closing sequence is –>. However it is not recognized by JavaScript. So it is written as //–>

 

<script language = "javascript" type = "text/javascript">
   <!--
      // This is a single-line comment
   
      /*
      * This is a multi-line comment in JavaScript
      */
   //-->
</script>

I recommend you try this out.

 

 

Other Things to Note

Note the following about JavaScript:

Case Sensitivity

JavaScript is case-sensitive. This is just like the Java programming language.

 

Semicolons

Although you use semicolons at the end of a line, you can well not use them. They are optional.

 

Whitespace

Spaces, taps and newlines that appear on a JavaScript program are ignored.

 

0 0 votes
Article Rating
Subscribe
Notify of
guest
3 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments

[…] JavaScript – Program Syntax […]

trackback
Lynn

This is very helpful. Thank you so much.
Love from Burma. ကျေးဇူးပါဗျာ

3
0
Would love your thoughts, please comment.x
()
x