Like me, many have a love/hate relationship with JavaScript. Now a days, JavaScript is very popular with the rich internet applications (RIA). So, it really pays to know JavaScript. JavaScript is a client side (and server side with node.js) technology that is used to dynamically manipulate the DOM tree. There are a number of JavaScript based frameworks like jQuery available to make your code more cross browser compatible and simpler. Firstly, it is much easier to understand JavaScript if you stop comparing it with Java or understand the key differences. Both are different technologies.

If you need more motivation to learn JavaScript, look at the Node.js, which is a software system designed for writing highly-scalable internet applications in JavaScript, using event-driven, asynchronous I/O to minimize overhead.

Q. What is the difference between Java and JavaScript?A. Don't be fooled by the term Java in both. Both are quite different technologies. The key differences can be summarized as follows:

JavaScript function can take variable arguments. You can call the function shown below as myFunction( ), myFunction(20), or myFunction(20,5).

function myFunction( value ) {
//.…. do something here
}

JavaScript has an implicit keyword known as the "arguments", which holds all the passed arguments. It also has a "length" property as in arguments.length to display the number of arguments. Technically an "arguments" is not an array as it does not have the methods like push, pop, or split that an array has. Here is an example.

JavaScript functions are objects as well. Like objects, the functions can be stored to a variable, passed as arguments, nested within each other, etc. In the above example, nameless functions are attached to variables "printFullName" and "printAge" and invoked via these variables. A function that is attached to an object via a variable is known as a "method". So, printFullName and printAge are methods.

Technically, what is done with the "add" and "sum" functions is that we have created a new function object and attached them to the variables "add" and sum. As you can see in the example below, the "add" variable is assigned to variable "demo", and the function is invoked via demo(2,5) within the "sum" function.

This demonstrates that the functions in JavaScript are objects, and can be passed around. Every function in JavaScript also has a number of attached methods including toString( ), call( ), and apply( ). For example, The temp2.js is stored under js_tutorial/js.

Java does not support closure till atleast version 6. A closure is a function plus a binding environment. closures can be passed downwards (as parameters) or returned upwards (as return values). This allows the function to refer to variables of its environment, even if the surrounding code is no longer active. JavaScript supports closure.

In JavaScript a closure is created every time you create a function within a function. When using a closure, you will have access to all the variables in the enclosing (i.e. the parent) function.

Java programs can be single threaded or multi-threaded. JavaScript engines only have a single thread, forcing events like onClick, etc.

Asynchronous UI Events like mouse click, focus, etc. It is asynchronous because you don't know when a user is going to click or change a text.

Timer functions like

var id = setTimeout(function, delay); // Initiates a single timer which will call the specified function after the delay. The function returns a unique ID with which the timer can be canceled at a later time.
var id = setInterval(function, delay); // Similar to setTimeout but continually calls the function (with a delay every time) until it is canceled.
clearInterval(id); // Accepts a timer ID (returned by either of the aforementioned functions) and stops the timer callback from occurring.
clearTimeout(id);

Ajax responses asynchronously invoking a callback function when the response is sent back from the server. It is asynchronous because, you don't know how long a server will take to process the ajax request and then to send the response.

to execute within the same thread. Even though all the above time based events appear to be run concurrently, they are all executed one at a time by queuing all these events. This also mean that if a timer is blocked from immediately executing, it will be delayed until the next possible point of execution making it to wait longer than the desired delay. This might also cause the intervals execute
with no delay.

Having said this, HTML5 specifies Web Workers, which is a standardized API for multi-threading JavaScript code.

Here are the key points to remember:

JavaScript variables are dynamically typed.

In JavaScript properties and methods are dynamically added.

JavaScript function can take variable arguments.

JavaScript objects are basically like name/value pairs stored in a HashMap<string,object>.

JavaScript functions are objects as well.

Here are some working examples: For the examples shown below, the HTML files are stored under /html sub-folder and JavaScript files are stored under /js sub-folder. The console.log(…) statements are written to your browser's web console. For example, in firefox, you can view the web console via tools - Web Developer - Web Console. Make sure that the version of Firefox you use has this option. The Web Console is handy for debugging as well.

Example 1: two input values are either added or concatenated depending on their types. If both values of type number then add them, otherwise just concatenate the values. Take note of the JavaScript keywords and functions like typeof, isNaN(..), Number(..), parseInt(..), etc. The "document" is part of the DOM tree representing an HTML document in memory. The method "getElementById(...)" will return the relevant "input" element from the DOM tree, and "value" will return the input value that was entered.

Example 2: Very similar to Example 1, but uses objects, and passes the relevant values from the call within HTML itself. It also uses the toString( ) method to convert a function object to display its source code.