JAVASCRIPT


JavaScript

Simply, JavaScript or JS is the language which is responsible for the things that the user sees as it is a client-side scripting language.As JavaScript is a client-side scripting language, It is used for web development along with other front-end development tool such as HTML and CSS. JavaScript helps to give dynamic behavior to our web pages such as adding animation, drop down menus, client-side validation etc. More over JS can be used for mobile apps development and game development. JavaScript is known as scripting language because it does not need to be compiled before execution, it executes in run-time environment through a web browser. Several libraries of JavaScript such as React JS, Angular JS etc can be found and used to make more interactive, intuitive and advanced web pages. JS is not only used for front-end rather it can be used in the server side also. Node JS is an example of server-side JavaScript.


Feature of JavaScript:
  1. It is supported by all the web browser since 97% of all web sites use JS.
  2. It is also a structural programming language since it support control structure such as branching and looping.
  3. It is prototype based programming language which means we can create object without creating classes, so it is also a Object Oriented programming.
  4. JavaScript is a case-sensitive language, small and upper case differs.
  5. Every operating system such as Windows, MacOS supports JS.

Uses of JavaScrip:
  1. JS is used for client-side validation.
  2. JS can be used to make dynamic drop-down menus.
  3. JS can be used to display date, time and even clock.
  4. JS can be used for game development.
  5. JS can be used to generate pop-up windows, alert message, dialog box etc.

Inline JavaScript code

This is the method of adding JS code directly inside the HTML tag. We don’t have to create separate JS file or even we don’t have to place JS code with in script tag. Simple events like onclick, mouseover, keypress can easily be added through this method. But, its very much inconvenient to add large JS code inline. JavaScript code can be added in HTML document inline as follows:

<HTML> 
<HEAD>
<TITLE>Sample</TITLE>
</HEAD>
 <BODY> <BUTTON onclick = “alert(‘Your message’)”>Click me</BUTTON> </BODY> 
</HTML>



Internal (Embedding) JavaScript code


This is the method of adding JS code within the HTML document. JS code is added internally with in the script tag inside body of the HTML document. JavaScript code can be embedded within HTML document as follows:

<HTML> 
<HEAD>
<TITLE>Sample</TITLE>
</HEAD> 
<BODY> 
<BUTTON onclick = “display( )”>Click me</BUTTON> 
 <SCRIPT> 
function display( ) 
alert(“Your message”); 
</SCRIPT> 
</BODY> 
</HTML>

External JavaScript file

This is the most popular methods of adding JS in our web pages. To add external JavaScript we have to create separate JS file which is linked with our HTML document as:
<SCRIPT src = “name.js”></Script>

Where, name.js is the JavaScript file that we create to write all our JS code. It should be in same location with our HTML document. It is most convenient way of adding JS in our web page as JS code don’t get messed with other HTML and CSS code. JavaScript code can be externally added with HTML document as follows:

Create a HTML document with any name

<HTML> 
<HEAD>
<TITLE>Sample</TITLE>
</HEAD> 
<SCRIPT src = “name.js”></Script> 
<BODY> 
<BUTTON onclick = “display( )”>Click me</BUTTON> 
 </BODY>
 </HTML>

Also create a JS file with .js extension and add following code:

function display( ) 
{
 alert(“Your message”); 
}

Here, we have created separate HTML and JS file in same location. Since, we have linked our JS file with our HTML document, every code which is written in JS file will be implemented on HTML document.


Variables in JavaScript

Variables in JavaScript are declared by using keyword 'var'. for eg,
var a=3,b=4;
var fruit = "apple";
Types of variable in JavaScript

a) Local variable

Those variable which are declared inside the block or function is called local variable. Local variable can only be accessed and used within the block or function.

<HTML> 
<HEAD>
<TITLE>sample</TITLE>
</HEAD>
 <BODY> <BUTTON onclick = "display()">Click me</BUTTON> 
<SCRIPT> 
function display()
 { 
var a=5; 
document.write(a); 
}
 </SCRIPT> 
</BODY>
 </HTML>


In above example, the variable 'a' is declared inside the function display(). So, it can be used only within the function block. Other function or block cannot use the value of 'a'. Hence, to overcome this limitation we have global variable.

b) Global variable

Those variable which are declared outside the block or function is called global variable. Global variable can be accessed and used within any other function or the block.

<HTML>
 <HEAD>
<TITLE>sample</TITLE>
</HEAD> 
<BODY> 
<BUTTON onclick = "display()">Click me</BUTTON> 
<BUTTON onclick = "replay()">Push me</BUTTON> 
<SCRIPT>
 var a=5;
 function display() 
document.write(a);
 } 
function replay()
 {
 document.write(a); 
}
 </SCRIPT>
 </BODY> 
</HTML>

In above example, the variable 'a' is declared outside the two function display() and replay(). So, it can be used by both function block. When user press Click me then, display() function is called, this function will display the value of 'a' i.e. 5 which is declared outside of the function. Similarly, when user press Push me then, replay() function is called, this function will also display the value of 'a' i.e. 5. This is because variable ‘a’ is declared outside of function or block which can be used by any number of function or block.

Comments

Popular Posts