Intoduction to Javascript
Java = Java is general purpose , concurrent , class-based , object oriented ,computer programming language , i.e specifically designed to have as few implementation dependencies as possible. It is intended to let application developers " write once " run anywhere "(wora)" , means the code that runs on one platform does not need to be recompiled to run on another.
Java is a programming language expessly designed for use in the distributed environment of the internet . It was designed to have the "feel and look" of the c++ language, it is easier to use . Java can be use to create complete application that may run on a single computer or distributed among server and clients in a network . It can also be used to build a small application for use as a part of a web page
Features of java
- Compiled or interpreter = It has both compiled and interpreter language. The features program of java is first compiled and then it is must to be interpreted it.
- Platform independent = Java language is a platform independent means program of java is easily transferable because after compilation of java program bytes codes will be created then we have to just transfer the code of bytes code to another computer.
- Object Oriented = It is purely oops language that is all the code of the java language is written into the classes and objects . Java is most popular language because it can also support code reusability , maintainbility etc.
- Distributed = java is a distibuted language means that the program can be design to run on computer.
- Simple small and familier = It is a simple language because it contain many other language like C and C++ and java remove complexity because It does not use pointers , storage classes and go to statement and java does not support multiple inheritance.
Javascript
Javascript is an object oriented language that allows the creation of interactive webpages .The javascript allows enteries that are loaded into an html forms as required. This empowers a website to return site information according to a user request . Javascript offers several advantage to a web developer such as short development cycle , ease of learning , small size scripts and so on . The strengths of javascript can be easily and quickly used to extend the functionality of html pages , already on a website.
Javascript is the scripting language of the web. Javascript is an billion of web pages to add functionality, communicate with the server and so on.Advantage of javascript
- Interpreted language = It is an interpreted language , that requires no compilation steps .They provide an easy development process . The syntax is completely interpreted by the browser just as it interprets html tags.
- Platform independence = It is an programming language i.e completely independent of the hardware on it work. It is a language i.e understood by any javascript enabled browser. Thus javascript application work on any machine that has no appropriate javascript enabled browser installed . This machine can be anywhere on the network. Each browser is a specific platform , javascript interpretation will be with respect to the specific platform . A javascript program developed on a unix machine will work perfectly well on a windows machine .
- Embedded within Html = Javascript does not require any special or seperate editor for programs to be written , edited or compiled . It can be written in any texteditor like notepad , along with appropriate Html tags , and saved as yfc.html. Html files with embedded javascript commands can then be read and interpreted by any browser i.e javascript enabled.
- Minimal syntax - easy to learn = It can be learn by few commands and simple rules of syntax , complete application can be built using javascript.
- Quick Development = Javascript is quick development because javascript does not require time-consuming compilation , script can be developed in a short peroid of time . This is enhanced by the fact that many GUI interface features , such as alerts , prompts , confirm boxes and other Gui elements are handled by client side javascript , the browser and Html code.
- Easy Debugging and Testing = It is an interpreted language , scripts in javascripts are tested line by line, and the errors are also listed as they are encountered , i.e an appropriate error message alongs with the line number is listed for every errors i.e encountered . It is thus easy to locate errors , make changes , and test it again without the overhead and deley of compiling.
- Designed for programming user events = It can supports object/events based programming . It recognizes when a form button is pressed . This events can have suitable javascript code attached , that will execute when the button pressed events occurs.
Javascript syntax is embedded into an Html file . A browser reads Html file and interprets Html tags . Since all javascript need to be included as an integral part of an Html document When required , the browser needs to be informed that specific section of Html code is javascript . The browser will then use its built - in javascript engine to interprets this code. The browser is given this information using the Html tags <script>........... </script>. The <script> tag marks the beginning of the scripting code . The paired </script> marks the end of the scripting code.
Syntax:
<Scripting Language ="javascript">
// code
</script>
Data types and literal
Javascript supports four primitives types of values and supports complex types such as array and objects . Primitives types can be assigned a single literal value such as number, string, or boolean value. Literal are fixed value , that literally provide a value in a program.
The primitives data types that javascript supports are as follow:
- Number = It can consists of integer and floating point numbers and the special NAN (not a number) value. The integral literals can be represented in javascript in decimal , hexadecimal, and octal form.
- Boolean = It can consists of the logical value T or F.
Javascript auomatically converts the boolean values T and F into 1 and 0 when they are used in numerical expression.
- String = It can consists of string values that are enclosed in single or double quates .
- Null = It can consists of a single value , null, that identifies a null , empty or nonexistent reference.
In order to make working with datatypes convenient , variable are created . In javascript variable can be created that can hold any type of data.
In order to use a variable , It is good programming style to declare it. Declaring a variable tells javascript that a variable of a given name exits so that the javascript interpreter can understand references to that variable name throughout the rest of the script.
Although it is possible to declare variable by simply using them , declaring variable help to ensure that program are well organized and help keep track of the scope of the variables. variables can be declared using var command.
Syntax:
Var <variable name > = value;
Example :
Var first _ name;
Var last_name = "yfc"
Var phone = 8410080577;
Javascript Array
Array are javascript objects that are capable for storing a sequence of series of values. The array element index start with 0 . Hence the last array element index number is one less that the length of array. These values are stored in indexed location within the array . The length of the array is the number of element that an array contains. The individual elements of an array are accessed by using the name of the array followed by the index value of the array element enclosed in square brackets.An array can be declared using any one of the following technique.
arrayName = new array (Array length)
arrayName = new Array()
NOTE : Javascript automatically extends the length of any array when new array elements are initialized.
Example:
Cons_ detail = new Array()
Cons_ detail[50] = "mobile"
cons_ detail [100] = "laptop"
Dense Arrays
A dense array is an array that has been created with each of its element being assigned a specific value . Dense array are used exactly in the same manner as other arrays . They are declared and initialized at the listing the value of the array element in the array declaration creates dense array. for example a dense array can be created as
arrayName = new Array(value0, value1,.......... valuen )
In this array , since the element count start from 0 to n , the array length is n+1.
Example :
<HTML>
<HEAD><TITLE>Viewing the element of computer part </TITLE></HEAD>
<body>
<SCRIPT LANGUAGE = "JavaScript">
Computers = New Array(4);
Computers[0] = "cpu";
Computers[1] = " ram";
Computers[2] = "pendrive";
Computers[3] = "floppy disk";
document . write (Computers[0] + "<BR/>");
document . write (Computers[1] + "<BR/>");
document . write (Computers[2] + "<BR/>");
document . write (Computers[3] + "<BR/>");
join_crit = Computers . join();
Document . write(join_crit);
</script>
</body></HTML>
Arithmetic
Operators
Arithmetic
Operators are the most familiar operators because they are used every day to
solve common math calculations. The arithmetic operators that javascript
supports are:
Operator
|
Description
|
+
|
Addition
|
*
|
Multiplication
|
%
|
Modulus
|
_
|
Subtraction
or Unary negation
|
--
|
Return
the value then decrement
|
/
|
Division
|
++
|
Return
the value then increment
|
An
operators requiring a single operand is referred to as a unary operators and
one that requires two operands is a binary operator.
The above
standard arithmetic operators are binary operators . In addtion to these binary
operations, there are unary arithmatic operators . They are (++) and (--).
Both
these increment and decrement operators can be used intwo different ways . Before
the operands or after the operands . Example , ++x increment x by one and result , while x++ return x and then
increments the value of x by one . Similarly , --x decreases the value of x by
one before returning a result , while x-- return the value of x before
decreasing its value by one.
Example:
X
= 9;
Y = ++x;
Z =
X++ ;
Logical Operators
Logical
operators are used to perform boolean operators on boolean operands AND , OR ,
NOT . The Logical operators supported by javascript are :
Operators
|
Description
|
&&
|
Logical
AND
|
||
|
Logical
OR
|
!
|
Logical
NOT
|
Comparison Operators
Comparison
operators are used to compare two values . The comparison operators supported
by javascript are :
Operators
|
Description
|
==
|
Equal
|
!=
|
Not
equal
|
<
|
Less
than
|
>
|
Greater
than
|
===
|
Strictly
Equal
|
!==
|
Strictly
Not Equal
|
<=
|
Less
than or equal to
|
>=
|
Greater
than or equal to
|
String Operators
String
operators are those operators that are used to perform operation on string .
Currently Javascript supports only the String concatenation (+) operators .
This
operators is used to join two strings . For example , "pq" +
"ab" produces "pqab".
User Defined
Functions
A
user defined function first needs to be declared and coded . Once it is done
the function can be invoked by calling it using the name given to the
function when it was declared.
Function
can accept information in the form of arguments and can return results.
Example of javascript
<html>
<head><title>
</title>
<script
language = "javascript">
var
name = "";
Function
hello()
{
name = prompt(' Enter your name :', 'name');
alert('greetings' +
name +' , welcome to my page !');
}
function
goodbye ()
{
alerts('goodbye' +
name + ' sorry to see you go !');
}
</script>
</head>
<body
on load = "hello(); " onunload = "good bye();">
<img
src = " images . gif"/>
</body>
</html>
0 comments:
Post a Comment