Friday, December 28, 2012

Learning JavaScript



JavaScript is basically a scripting language that makes browsers more interactive. Browsers have limited functionality that is used to understand only text, images, tables or frames. It is used to respond to user input and take action based on the input. It allows interactivity through improved appearance, site navigation, performing calculations, validation of inputs. It is embedded on Html pages and does not need separate installation. It is an interpreted and not compiled language which means that it is executed directly on client side. It is generally misinterpreted that JavaScript is similar to Java but it is not true. In fact, it has nothing to do with Java.

Let us learn start with basic JavaScript code in any text editor:

<body>
<script language="JavaScript">
document.write('This is first  JavaScript Page');  /* it simply outputs the text inside parentheses*/
</script>
</body>

JavaScript is very good at processing user input in the web browser. HTML <form> elements receive input.

Comments in JavaScript is similar to any programming language such as C++, Java, C# i.e. for single line "//" and for multiline "/* your code here */".

Data type declaration in JavaScript is very unique in the sense that it uses only "var" as the declaration for the irrespective of kind of data.

E.g.

var x=2; /*Here x acts as integer. */

var x="Mitesh"; /*Here x acts as string */.

So, it dynamically takes data based on the input. But, you must note one point that unlike other programming language, the variable must be declared at declaration time unless data type is an array, then you must give the size of that array. JavaScript code is embedded inside the Html code. There is also escape character "\" used to tell the JavaScript that the code just after this tag should not be parsed E.g. \n  //new line.


You must write the JavaScript code within the Html by using the following code within the head or body section of the code to include the JavaScript in your program.

<script type="text/javascript" >
//Your JavaScript code here
</script>

Or you can include JavaScript code externally in Html code by using the code as below:  

<script type="text/javascript" src="mitesh_file_name.js" ></script>

You can also include functions inside JavaScript by using code:

//This code is inside script tag
function  function_name()
{
//Your code here
}

//To call the above function you use the code below on Html

<button name="submit" value="Submit" onClick="mitesh_function_name()"/>

There are also various kinds of interactive functions that are built in the JavaScript

i) alert("pop up to user");
This code will give an alert box to the user saying "pop to user".

ii) prompt("Enter number");
This code is used to get the input from the user. So, the user will provide the number. It increases interactivity with user.

iii) confirm("Do you want to continue?");
A confirm box is often used if you want the user to verify or accept something. When a confirm box pops up, the user will have to click either "OK" or "Cancel" to proceed. If the user clicks "OK", the box returns true. If the user clicks "Cancel", the box returns false.

For more on Sql or software development India visit NevonProjects.com