JavaScript Js Advantages And Rules Of Writing Program|First Program



JavaScript(js): in this article, you will learn about JavaScript Advantages and what is the rules of writing the program in JavaScript.


Script means screen play (or Play). A script is a small program (code). It can be inserted into html document to perform a specific task (action). A web page may contain scripts.

Typically, scripts are added to web pages to create dynamic effects. Scripts are written in scripting languages. JavaScript(js) and VBScript are example of scripting  languages.

Scripts are divided into two categories:

  1. Client-side scripts
  2. Server-side scripts

Client-Side Scripts:

A small program that is executed on the user’s computer is called client-side script. For example, script written in JavaScript(js) are widely used on web pages that are executed on the client computer.

Usually, client-side script is used to change the contents of web page dynamically. Client-side Script are written in scripting language such as JavaScript (client-side JavaScript ) and vbScript.

Client-side scripts are often embedded within an html document. In addition, client-side scripts may also be stored on the server in a separate file. This file is loaded into the client computer, when the user send a request to the web server. A specific script stored in a separate file can be called for execution in the html page with the reference to script name (such as name of function). The web browser executes the script and then displays the visible output of the script.

Server-side Scripts:

A script that is executed on the web server is called server-side script. The web server also has a script interpreter. It interprets (translates) the script code stored on the server and generates html code dynamically. The web server then sends this code to the web client (internet browser).

The advantage of the server-side script is that it allows you to access the server resources. For example, you can write and read data to and from the database stored on the server. You can also access the file system of server. Similarly, you can access a mail server in order to receive or send e-mail.


JavaScript(js) is the most popular scripting language. It is an interpreted language. It is a third generation language. It is often used for writing client-side scripts. It is used in millions of web pages to add interactivity to html pages. Usually, it is embedded directly into html pages.

JavaScript(js) was developed by Netscape company. This language was originally named as liveScript. In 1995, it was named as JavaScript( in a co-marketing deal between Netscape & sun Microsystems). The syntax to write script in JavaScript language is similar to java language. However, java (developed by sum Microsystems) and JavaScript(js) are two entirely different languages in both concept and design.

JavaScript(js) was implemented on Netscape navigator 2.0. now all major web browsers support the JavaScript(js). The examples of these browsers are internet explorer, Netscape Navigator, Mozilla and opera etc.

Microsoft decided to develop its own scripting language; it was called jscript (JavaScript version of Microsoft company). Microsoft implemented jscript in its internet explorer version 3.0, and also in its web server, and internet information services(IIS).

In 1997, ECMA (European Computer Manufacturers Association) published a version of standard JavaScript(js). This standard version was named as ECMAScript. ECMAScript standard  is based on JavaScript (Netscape) and jscript(Microsoft). It means that ECMAScript defines the most general features of JavaScript(js) and jscript.

Like java, JavaScript(js) allows you to create object classes so that you can create and handle your own objects. It also allows you to use the existing objects that the language offers. JavaScript(js) offers eight such objects. However, JavaScript has limited features for inheritance and polymorphism. Therefore, JavaScript is said to be object-based, rather than object-oriented.

Advantages of JavaScript(js):

The major advantages of JavaScript(js) are:

Interpreted Language:

JavaScript(js) is an interpreted language. Like HTML tags, JavaScript’s scripts are directly interpreted by web browser. So no separate language processor is required to translate the JavaScript’s scripts.

Easy to Learn:

JavaScript(js) is very easy to learn and to write scripts. This language is similar to C/C++. It supports the syntax and structure of C/C++. If you know the C or C++ you can easily learn the JavaScript.

Easy to Debug and Test:

Being an interpreted language, scripts in JavaScript(js) are translated and executed line by line. If an error occurs on any line of the code, an error message along with the line of code is displayed on the client computer. So it is easy to find and removes errors. The script can be tested on your local browser.

Embedded within html:

JavaScript(js) does not require any special editor program for writing scripts. It can be embedded in html document using any text editor like Notepad, or notepad++. The html document along with scripts can be tested in any browser that supports the JavaScript.

Complete Language:

JavaScript(js) is a complete language. It also has capabilities of a structured programming language and object-oriented programming language.

Platform Independence:

JavaScript(js) is platform independent. It means that JavaScript is independent of the hardware. Scripts written in JavaScript can be executed on any machine.

React to Events:

JavaScript(js) can react to events. For example, a script can execute automatically when a page starts to load and another can execute when a page loading process completes. Similarly, a script can be executed when a user clicks on an html element.

Input Data Validation:

In most of the online applications, the data is entered into different fields of the form. The form is submitted to the server for processing or to store the data into database on web server. In this case, a script written in JavaScript(js) can be used to validate the data entered by the user before it is sent to the server for processing. For example, a script is embedded within the html document to verify if the user has entered data (or has selected other options) in all required fields before submitting the data to the web server. This saves the time of server form extra processing.

Detecting Visitor’s Browser:

JavaScript(js) can be used to detect the visitor’s browser. In this way, you can load another version of web page specifically designed for that browser.

Creating Cookies:

JavaScript(js) can be used to create cookies. A cookie is a short message. It is given to a web browser (or visitor) by web server (or website). The browser stores the message in a text file on visitor’s computer hard disk. The message is then sent back to the server each time the browser requests a page from the server.

The main purpose of cookies it to identify users and possibly prepare customized web pages for them. When you enter a website using cookies, you may be asked to fill out a form providing such information as your name and interests. This information is packaged into a cookie and sent to your web browser, which stores it for later use. The next time you go to the same website, your browser will send the cookie to the web server. The server can use this information to present you with custom web pages. So, for example, instead of seeing just a generic welcome page you might see a welcome page with your name on it something like: “welcome to Programming Digest”.

JavaScript(js) Security

JavaScript(js) and the DOM provide the potential for malicious authors to deliver scripts to run on a client computer via the Web. Browser authors minimize this risk using two restrictions. First, scripts run in a sandbox in which they can only perform Web-related actions, not general-purpose programming tasks like creating files. Second, scripts are constrained by the same-origin policy: scripts from one Web site do not have access to information such as usernames, passwords, or cookies sent to another site. Most JavaScript-related security bugs are breaches of either the same origin policy or the sandbox.

There are subsets of general JavaScript(js)—ADsafe, Secure ECMAScript (SES)—that provide greater levels of security, especially on code created by third parties (such as advertisements).Caja is another project for safe embedding and isolation of third-party JavaScript and HTML.

Rules of Writing Program Statements:

JavaScript(js) statement is a command to the browser. It tells the browser what to do. A Script is a set of statements. The following rules are followed while writing  program (script) statements in JavaScript:

  • Like C/C++ and java, each executable statement of JavaScript(js) ends with semicolon(;).

The use of semicolon is optional (according to the JavaScript standard), if you write a statement in a single line. However, if you write multiple statements in a single line, then you must write the semicolon at the end of each statement.

  • Like C/C++ and java, JavaScript(js) is also case sensitive language.
  • Variables must be defined before they are used in the code.
  • JavaScript(js) statements can be grouped together in blocks. A block is a group of statements. A block starts with a left curly bracket (‘{‘) and ends with a right curly bracket(‘}’). Usually blocks are defined in control structures (such as loops and conditional structures) and function etc.

Example how to write JavaScript(js) first program:

JavaScript(js) first program

Example how to use external JavaScript file in html:

To use external JavaScript file first of all create a .js file in your directory in my case I created Js_code.js file and then call this file in the your html file.

Js_code.js file code

“demo.html file code


JavaScript(js) external file using

Example how to use confirm dialog box in Javasrcipt and html:

JavaScript(js) alert box

If you click on ok the following message will be displayed

JavaScript(js) how to create confirm box

And if click on cancel then it will show the following

JavaScript(js) how to create alert confirm box

Related Article:

Html Form, Form Tags, Form Attributes, Textbox, Buttons, Textarea

Related Articles

Leave a Reply

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

Back to top button