JAVASCRIPT

What is JavaScript?

JavaScript is a programming language that adds interactivity to your website (for example: games, responses when buttons are pressed or data entered in forms, dynamic styling, animation).

JavaScript (“JS” for short) is a full-fledged dynamic programming language that, when applied to an HTML document, can provide dynamic interactivity on websites.

Importance of  JavaScript?

  • An HTML page is a static one and without JavaScript it would be static still.
  • JavaScript could make a web page dynamic and also we can create special effects on web pages like rollover, roll out and many types of graphics that would leave an amazing impression on the user’s mind.
  • For example, for a flash content on a web page to be executed in a web browser the JavaScript must be enabled.
  • JavaScript is mainly used in all websites for the validation purpose.
  • The Validation would be for a contact form or a registration form where a user fills all there information and there the page has to validate the filled data and that validation could be done with JavaScript other than server side scripting languages.
  • JavaScript is not only supports the web pages but also it supports the external applications like PDF documents, running widgets, supporting for flash applications etc.

History of JavaScript

  • Designed by Brendan Eich
  • First appeared May 23, 1995; 21 years ago
  • Developer : Netscape Communications Corporation, Mozilla Foundation, Ecma International
  • Current version : ECMAScript
  • URL : https://www.javascript.com/

Basic Structure of JavaScript

JavaScript can be placed in the <body> and in the <head> section of any HTML page.

  • The <script> Tag

<script>

document.getElementById(“demo”).innerHTML = “My First JavaScript”;

</script>

  • JavaScript in <body>
<!DOCTYPE html>

<html><body> 

<h1>A Web Page</h1>

<p id="demo">A Paragraph</p>

<button type="button" onclick="myFunction()">Try it</button>

<script>

function myFunction() {

   document.getElementById("demo").innerHTML = "Paragraph changed.";

}

</script>

</body>

</html>

 

  • External JavaScript
<!DOCTYPE html>

<html>

<body>

<script src="javascript.js"></script>

</body>

</html>

File Extension of JavaScript

  • JavaScript files have the file extension .js.

Comment Line in JavaScript

  • Single Line Comments

Single line comments start with //.

Any text between // and the end of the line will be ignored by JavaScript (will not be executed).

Example

// Change heading:

document.getElementById(“myH”).innerHTML = “My First Page”;

  • Multi-line Comments

Multi-line comments start with /* and end with */.

Any text between /* and */ will be ignored by JavaScript.

Example

/*

The code below will change

the heading with id = “myH”

and the paragraph with id = “myP”

in my web page:

*/

document.getElementById(“myH”).innerHTML = “My First Page”;

document.getElementById(“myP”).innerHTML = “My first paragraph.”;

Variable Declaration

  1. Declaration: The variable is registered using a given name within the corresponding scope (explained below – e.g. inside a function).
  2. Initialization: When you declare a variable it is automatically initialized, which means memory is allocated for the variable by the JavaScript engine.
  3. Assignment: This is when a specific value is assigned to the variable.

Syntax:

var x; // Declaration and initialization
x = “Hello World”; // Assignment
// Or all in one
var y = “Hello World”;