Introduction


JavaScript is a very powerful client-side scripting language. It is used mainly for enhancing the interaction between the user and the webpage. In other words, you can make your webpage more lively and interactive with the help of javascript. Javascript is also being used widely in the field of game and Mobile application development.

Here is a small overview of javascript

  •    Javascript is an object-based scripting language.
  •    Javascript gives the user more control over the browser.
  •    Javascript handles dates and time.
  •    Javascript detects the user's browser and OS.
  •    Javascript is case sensitive.
  •    Javascript is object based language as it provides predefined objects.
  •    Every statement in javascript is terminated with a semicolon (;).
  •    Most of the javascript control statements syntax is same as the syntax of control statements in C language.
  •    Javascript also has the ability to create new functions within scripts.

History

Originally developed by Brendan Eich under the name Mocha, it was then renamed to Livescript before finally being named JavaScript. JavaScript was first introduced and deployed in the Netscape browser in the December of 1995. When JavaScript was added to Internet Explorer, it had to be officially called "JScript", because Netscape owns the name "JavaScript". The choice of name proved to be a source of much confusion.

Features of JavaScript

  •    It gives the user more control over the browser.
  •    It has ability to detect the user's browser and OS, it also allows the script to perform platform-dependent operations, if necessary.
  •    It can perform simple computations on the client side.
  •    It can enable the browser to perform input validation, on making a mistake in the input the user can see the message immediately instead of waiting for a server response.
  •    It can handle date and time.
     

Tools needed

  •    Text Editor to write code: Notepad ++, Komodo Edit or any other text editor
  •    Web Browser to display web pages: Google Chrome, Mozilla Firefox etc

Running of JavaScript

    Being a scripting language, javascript cannot run on its own. A web browser is responsible for running the javascript code. When the user requests an HTML page with javascript in it, the script is loaded into the browser and the browser renders it. One of the biggest advantages of javascript is that it is supported by all the modern web browsers like Google Chrome, Mozilla Firefox etc. Javascript is also not limited by the operating system, thus, it runs seamlessly on Windows, Linux or Mac.

Commenting JavaScript code

A comment is the portion of a program that exists only for the human reader and stripped out before displaying the programs result. There are two commenting formats in JavaScript.

Single-line comment: Single line comments start with //. Any text between // and the end of the line is ignored by JavaScript (is not executed).

Multiline comment: Multi-line comments start with /* and end with */. Any text between them is ignored by JavaScript.

Displaying Data

  •    JavaScript can "display" data in different ways:    
  •    Writing into an HTML element, using innerHTML.
  •    Writing into the HTML output using document.write().
  •    Writing into an alert box, using window.alert().
  •    Writing into the browser console, using console.log().
     

Using innerHTML

To access an HTML element, JavaScript uses document.getElementById(id) method. The id attribute defines the HTML element. The innerHTML property defines the HTML content:

Example:

<!DOCTYPE html>
<html>
<body>
<p>innerHTML</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = 5 + 6;
</script>
</body>
</html>


Output:

innerHTML
11

Using document.write()

It is convenient to use document.write() for testing purposes.

Example:

<!DOCTYPE html>
<html>
<body>
<p>document.write</p>
<script>
document.write(5 + 6);
</script>
</body>
</html>


Output:

document.write
11 
 

Using window.alert()

An alert box is generally used to display data as an alert in the browser.

Example:

<!DOCTYPE html>
<html>
<body>
<p>window.alert</p>
<script>
window.alert(5 + 6);
</script>       
</body>
</html>


Output:

11 //Displays in a new window
 

Using console.log()

Console.log() is used to display data in the console of the browser. This method is resourceful especially in debugging.

Example

<!DOCTYPE html>
<html>
<body>
<script>
console.log(5 + 6);
</script>
</body>
</html>


Output:

11 //Displays output in the console, press F12 to view the result