Thursday, 19 February 2015

The first thing you should know about JavaScript is that it is a case-sensitive language. Thus, the terms PART, Part and part will be considered three distinct and totally independent terms in JavaScript. So, to avoid being annoyed we suggest, developing any consistent approach and sticking with it.
JavaScript’s case sensitivity can be even more confusing because HTML is not case sensitive. <IMG> and <img> are the same tags.
JavaScript is similar to HTML in that it ignores most white space (spaces, tabs, and new lines).
In some of the programming languages that JavaScript is related to, a semicolon is used to mark the end of each program statement. JavaScript relaxes this rule: you can end a statement with a semicolon but do not have to as long as each statement is placed on a separate line. If you want to put more than one statement in a line, semicolons are required. For example, this is correct:
a=1;
b=2;
So is this:
a=1
b=2

And this is correct too:
a=1; b=2;
But this is not correct:
a=1 b=2

Comments in JavaScript

Just as with HTML, you can enter comments in your JavaScript code. The syntax is different, however. You can enter single-line comments using a pair of slashes. Anything following // on a line is a comment, as in this example:

//This is comment.
a=1; //This is also a comment.

You can also create comments using /* and */ character pairs. Anything following /* up to next */ is a comment.

Literals
  • A literal is nothing more than a data value typed directly into the program code. They fall into three categories.
  • Numbers are typed normally, such as 1234 or0.55.
  • Strings, or text, are entered enclosed in single or double quotes. Some examples are “Ankit’s Salary” and ‘Rourkela’.

The special keywords true and false representing the corresponding Boolean values, and null representing “no data.”

Identifiers
An identifier is a name used in a JavaScript program to identify something, typically either a variable or a function. The rules for creating identifiers are simple:
  • The first character must be a letter, an underscore (_), or a dollar sign ($).
  • Subsequent characters can be any letter, any digit, an underscore, or a dollar sign.

The following are all legal JavaScript identifiers:

newid
$fname
Golu_delhi
These are not legal:

age%
1day
New-user

Also, you cannot use a JavaScript reserved keywords as an identifier. These reserved words are:

finally, for, function, if, in, instanceof, new, null, return, switch, break, case, catch, continue, default, delete, do, else, false, this, throw, true, try, typeof, var, void, while, with.

Variables
A variable is a location to store data in a script. As the name suggests, the data in a variable can change. When you create a variable, you give it a name; you then use that name to refer to it in your code. To create a variable (declare a variable) use the var keyword.

var variablename

variablename is any eligible JavaScript identifier as described in the last topic. It must be unique within the program or, if it is declared in a function, within that function. You can declare variables one at a time like this:

var sum;
var diff;

Or, you can declare more than one in a single var statement:

var add, diff;

You can initialize (assign a value to) a variable at the time you are declaring it:

var name=”Author”;

If you declare a variable without initializing it, the variable’s value will be undefined until the program stores a value in the variable.

Arrays
An array is a type of variable that can use store multiple data items under the same name, distinguishing them by a numerical index. For example, you could create an array named months that has 12 elements, one for each month. Then array element 1 could be used for January, element 2 for February, and so on. To create an array, you use the Array() function. Example:

var myArray = new Array(10);

This declaration creates an array with 10 elements. You can also specify the data in the array when you declare it:

var myArray = new Array(2, 4, 6, 8, 10);
var my Array = [2, 4, 6, 8, 10];

Both codes given above, creates an array with 5 elements and there 5 values. To access an array element, you use the array name followed by the element number in square brackets:

myArray[5] = “Happy Website Desgigning!”;
document.write(myArray[4]);

JavaScript has the handy features that you can change the size of an array at any time. You can both increase and decrease an array’s size. To add elements to an array, all you need do is assign a value to one or more new elements as shown here:

var myArray = new Array(“green”, “yellow”, “red”);

This creates an array with three elements with indexes with indexes 0, 1, and 2. Then suppose your program executed the following code:

myArray[3] = “white”;
myArray[4] = “blue”;

JavaScript will automatically increase the array to five elements, adding the two new elements with indexes 3 and 4. If value is greater than the current array size, new elements are added as needed. If value is smaller than the current array size, elements are removed from the end of the array.

Functions
Functions are an important part of JavaScript programming, particularly for code that performs actions that are needed frequently. Rather than repeating the code at various locations in your page, you can put it in a function and call it as needed. A function is generally assigned a name. You can execute the code in a function at any time by referring to it by name. A function definition consists of the following.
  • The function keyword
  • The function name (same rules as were for JavaScript Variables).
  • A pair of parentheses that can optionally contain a list of one or more function parameters
  • An opening curly brace ({ )
  • One or more lines of JavaScript code that constitute the function body
  • A closing curly base ( })

JavaScript functions are usually placed the head section of an HTML document. A function can contain essentially any JavaScript code, the one exception being that a function definition cannot contain another function definition. Code within a function can, though, call another function.
There’s no real limit, but good programming practice says that functions should be kept fairly short. So, well exactly how much short, we can’t give you an exact answer, but in our experience once a separate sunctions. More important, each function should carry out a single discrete task.

Function Arguments
A JavaScript function can take arguments that let you pass information to the function when you call it. Arguments (also called parameters) are optional but are essential for many functions. To include arguments in a function definition, you simply include the argument names, separated by commas if there is more than one, in the parentheses. Example of a function with two arguments:

function myfunction(arg1, arg2)
{
…
}
When you call the function, you must include the argument values in the cell:

myfunction(arg1Value, arg2Value)

Let’s look at an example. Here’s a very simple function that displays a message to the user.
function alertUser(msg)
{
alert(msg);
}

Note:
That alert is a built-in function that displays a message to the user. JavaScript code elsewhere on the page would call the function like this:
alertUser(“Please enter your name”);

Function Return Value
Functions become even more useful when you consider their ability to return a value to the calling code. There are some differences when you are defining and using a function that return a value. First, you must use the return statement in the function to specify what is returned. The syntax is

return value

where value is the data to be returned. For example, next function calculates the square of a number:

function squareOf(a)
{
return a*a;
}

The function multiples its one argument by itself and uses the return statement to return the value.
Second, the calling code must retrieve the returned value in some way. This is often done by placing the function call on the right side of an equal sign, thereby assigning the return value to a variable:

var squareOfFour;
squareOfFour = squareOf(4);

You can also use a value-returning function anywhere a value can be used. Here’s an example:

alert('The square of 4 is' + squareOf(4));

Variable Scope
This term refers to area in a script a variable is visible. In other words where it is accessible? This applies to arrays as well. There are two kinds of scope:
  • Local scope: A variable declared in a function has local scope and is visible within that function only.
  • Global scope: A variable declared outside a function has global scope and is visible throughout the entire file, both within and outside of functions.

When you declare a visible within a function, it is completely hidden from the JavaScript code elsewhere on the page. This means that other code cannot alter the variable. By isolating each function’s variables in this manner, you can prevent a lot of hard-to-find script bugs. You can use the same variable name more than once in different functions. Let’s look at some examples so that you get aware of these concepts:
In this first example, a global variable name is declared. When the function executes, the value “Ankit” is displayed because there is no local variable name so the code accesses the global variable:

var name = “Ankit”;
function myFunction()
{
alert(name)
}

In the second example, the function declares a local variable name. The result is that “Akash” is displayed because the code in the function accesses the local variable and not the global one of the same name:

var name = “Ankit”;
function myFunction()
{
var name = “Akash”;
alert(name);
}

In the final example, f1 displays “Aman” because it uses the local variable name while f2 displays “Vishal” because it accesses the global variable name:

var name = “Vishal”;
function f1()
{
var name = “Aman”;
alert(name);
}
function f2()
{alert(name);
}


0 comments:

Post a Comment