Make delicious recipes!


Introduction to JavaScript

JS is very much like Java in syntax, although execution engine is very different.

Following constructs are exactly same:

  1. Comments: both // and /**/

  2. Loops: for, while, do-while + an extra for (<var> in <values)

  3. Statements: if, else, switch

  4. try catch, throw

  5. Operators: ++, --, += etc.

  6. Strings behave like Java objects and have all Java String functions available like length(), toUpperCase() etc.


JS does everything by referring to a variable called document which refers to the current page.

Example:

  1. document.write (“Hello World”);

  2. document also has a map called forms which enables to get any form in the page by its name. Example:


<form name="myForm" action="demo_form.asp" onsubmit="return validateForm()" method="post">

    First name: <input type="text" name="fname">
    <input type="submit" value="Submit">

</form>


<script>
function validateForm() {

    var x=document.forms["myForm"]["fname"].value;
    // do some validation here
    return true;
}
</script>



JS can basically access any field of an HTML construct as shown above.

So, this can also be used to add dynamism to web pages by calling JS scripts on events.








Like us on Facebook to remain in touch
with the latest in technology and tutorials!


Got a thought to share or found a
bug in the code?
We'd love to hear from you:

Name:
Email: (Your email is not shared with anybody)
Comment:

Facebook comments:

Site Owner: Sachin Goyal