Computer Seince
Computer Seince
Computer Seince
There are two ways to include JavaScript code - in a separate file or embed the
code in xhtml file.
OR
Reserved Words
break delete function return typeof
case do if switch var
catch else in this void
continue finally instanceof throw while
default for new try with
Types
JavaScript is a weakly typed language. It has five primitive types - Number,
String, Boolean, Undefined, and Null. The Undefined and Null are trivial types. The
type Undefined has exactly one value undefined. The type Null has exactly one value
null. There are two values for Boolean type - true and false.
JavaScript provides three 3 wrapper objects - Number, String, and Boolean. These
objects have attributes and methods. The objects are created using the new
expression and hold references (addresses)of where the objects are created in
memory. The primitive types hold literal values.
Operators
Arithmetic Operators: +, -, *, /, %
Increment and Decrement Operators: ++, --
Comparison or Relational Operators: ==, !=, <, <=, >, >=, ===, !===. The double ==
compares values and the triple === compares both values and types. Thus "3" == 3 is
true but "3" === 3 is false.
Boolean Operators: && (AND), || (OR), and ! (NOT). Both the AND and OR operators
are short circuit operators like they are in Java and C.
JavaScript has bitwise operations - & (AND), | (OR), and ~ (NOT). There are also
the left shift (<<) and signed right shift (>>) and unsigned right shift (>>>). But
these operators are hardly used in JavaScript.
switch (expression)
{
case value1: // statement(s)
case value2: // statement(s)
...
default: // statement(s)
}
The expression can be numbers, strings, or booleans. The value of the expression
should match one of the values in the case statements otherwise the default
statements get executed. The break statement gets you out of the switch block.
Loops
The standard loops are available in JavaScript - while, do-while, and for.
while (expression)
{
// statement(s)
}
do
{
// statement(s)
} while (expression);
Arrays
In Java and C once an array is created its length remains fixed. Moreover arrays in
those languages have elements only of one type. In JavaScript, however, the length
of an array is dynamic and the elements of an array could be of mixed types. That
is an array can contain numbers, strings, booleans, or references to other arrays
or objects.
An array can be created in two ways - by using the constructor for an array or by
explicitly enumerating the elements in the array. Here are examples of both:
join method concatenates all the elements of an array into a single string. If no
delimiter is specified, the elements are separated by commas.
colors.sort();
// colors = ("blue", "green", "red", "yellow");
concat method is called with zero or more arguments item1, item2, ..., it returns
an array containing the array elements of the original array followed by the array
elements of each of the arguments in order.
colors.push ("pink");
// colors = ("red", "blue", "yellow", "green", "pink");
pop method removes the last element of the array.
colors.unshift ("violet");
// colors = ("violet", "red", "blue", "yellow", "green");
Functions
The function definition consists of a header and a body. The header has the keyword
function followed by a list of optional formal parameters. In the body of the
function there could be 0 or more return statements.
The function is called with a list of actual parameters. JavaScript does not check
that the type and number of the actual parameters matches with the formal
parameters. A function could accept a variable number of parameters. Each function
has an attribute called arguments that can only be refered to inside the function.
arguments is an array that holds the parameters passed to the function. We can
always find out the actual number of parameters passed to the function by checking
the length of the array arguments.
function addAll ()
{
var sum = 0;
for (var i = 0; i < arguments.length; i++)
sum = sum + arguments[i];
return sum;
}
...
var p = addAll (x, y, z);
Primitive types are passed by value and objects are passed by reference. Variables
inside a function have local scope if they are explicitly declared using the var
keyword, otherwise they have global scope. A variable declared explicitly inside a
function hides a global variable of the same name declared outside the function.
In JavaScript, functions are looked upon as objects. So one can create references
to them and pass them to other functions as an actual parameter.