msbte campus css unit 1
msbte campus css unit 1
3. It automatically inserts the semicolon at the end of the statement, hence there
is no need to write semicolon at the end of the statement in JavaScript.
4.Dynamic typing:
It supports dynamic typing, that means the data type is bound to the value and
not to the variable. For example one can assign integer value to a variable say
‘a’ and later on can assign some string value to the same variable in JavaScript.
For running your JavaScript, just open the Web browser like Internet Explorer,
Mozilla Firefox and Chrome and type the name of the file on the address bar
along with its complete pathname.
For example - My JavaScript program has a name FirstPg.html and it is present
at the location
d:/JavaScript_Ex folder. Hence the complete path is typed at the address bar
Script Explanation
In above script
1) We have embedded the javaScript within
<script type="text/javascript">
</script>
2) A comment statement using / *and */. Note that this type of comment will be
recognized only within the <script >tag. Because, JavaScript supports this kind
of comment statement and not the XHTML document
3) Then we have written document. write statement, using which we can display
the desired message on the web browser. To print the desired message on the
web browser we write the message in double quotes inside the document.write
method.
For example
document. write("Welcome to First Page of JavaScript");
Comments in JavaScript
JavaScript supports following comments
1. The / i.e. a single line comment can be //used in JavaScript.
2. The/* and* / can be used as a multi-line comment.
3. The XHTML <!__> and <__>is also allowed inJavaScript
Object Name
JavaScript is a object oriented programming language.
That means programs are written using objects.
Object is nothing but some entity. In JavaScript document, window, forms,
fields, buttons are some
popularly used objects.
Each object is identified by either an ID or by a name.
Sometimes an array or collection of objects can also be created and particular
object can be accessed from that array.
Property:
Property is actually a value associated with each object.
For example - for the object window the height and width are the properties that
are associated with it.
Each object has its own set of properties.
Method:
Method is a unction or a process associated with each object,
For example - for the document object the method is write. To this write method
we pass the string which we want to get displayed on the browser window.
Dot Syntax
Every object is associated with some property and method.
For accessing the properties and methods of an object we use dot operator.
Prepared By:Mr Vikas Ligade. Page 4
Chapter:1 Basics Of JavaScript Programming.
For example
document.write("Hello "V/accessing method write of
//document object
Main Event:
Event is something that causes JavaScript to executethe code.
For example - if you press a key for F1 and function for F1 gets executed.
In JavaScript when user submits the form, clicks some button, writes something
to text box then
corresponding events get triggered.
Execution of appropriate code on Occurrence to event is called event handling.
Normally some functions are defined for event handling. These functions are
called event handler.
Number
Number is a numeric value that can be integer or float.
It can be used calculation.
String
String is a collection of characters. It is enclosed within single quote or double
quote. A string may contain numbers but these numbers can not be used for
calculations
Boolean
The Boolean values are true and false. These values can be compered with the
variables or can be used in assignment statement.
Null
The Null value can be assigned by using the reserved word null. The null means
no value. If we try to access the null value then a runtime error will occur.
Object
Object is for an entity that represents some value. For example window object
is a value using which the window is created. Form is an object upon which
some components such as Button, Checkbox, radio buttons can be placed and
used.
Function
Function is intended for execution of some task. There can be predefined
function and user defined
Function
for example –
alert() is a predefined function, using which a popup window having some
message can be displayed. Similarly, one can write his/her own function called
user defined function in
JavaScript. The keyword function is used while defining the user defined
function.
1.3.2 Variables
In java script we can declare the variable using the reserved word var. The value
of this variable can be anything; it can be numeric or it can be string or it can be
a Boolean value.
[JavaScript VarDemo.html]
<!DOCTYPE html>
<html>
<head>
<title> variables in JavaScript </title>
</head>
<body
<script type="text/javascript">
<script>
var x = 10;
var y = 20;
var z=x+y;
document.write(z);
</script>
</body
</html>
Note that using ‘var’ we can define the variable which is of type numbers(2,3 or
5) as well as the string
The result = “”.
Rules for Using Variables
Following are some conventions used in JavaScript for handling the identifiers-
1. Identifiers must begin with either letter or underscore or dollar sign. It is then
followed by any number of letters, underscores, dollars or digits.
2. There is no limit on the length of identifiers.
3. The letters in the identifiers are case-sensitive. That means the identifier
INDEX, Index, index, inDex are considered to be distinct.
4 Programmer defined variable names must not have upper case letters.
1.3.3 Keywords
Keywords are basically the reserved words are the special words associated
with some meaning. Various keywords that are used in JavaScript are enlisted
as below –
Conditional Operator
The conditional operator is ? The syntax of conditional operator is
Condition?expression1:expression 2
Where expression denotes the true condition and expression2 denotes talse
condition.
For example
a>b?true:false
This means that if a is greater than b then the expression will return the value
true otherwise it will return false.
Expressions
JavaScript makes use of mathematical statements using operators and operands.
These statements are called expressions.
1.5 If Statement
As a selection statement we use various types of if statements. These statements
are -
1. if-
The syntax of if is:
if(condition)
statement if the condition is true
2. if...else –
The syntax of if...else is:
if(condition)
statement if the condition is true
else
statement if the condition is false
3.if...else if –
The syntax of if...else if is:
if(condition)
statement if the condition 1s true
else if(condition)
statement it another condition 1s true
else if(condition)
</html>
Script Explanation
The above document is to find the largest number from the given three numbers.
we nave used nested if...else statements. The script is pretty simple, in which
first of all we have compared a with b, if a is greater than b then we have
compared a with c. Similarly in the else part b is compared with c. Thus allthe
three numbers get compared with each other and the largest number is found out
among the three.
Since already in our program we have set the a, b and c values as 10, 20 and 30
respectively the output should be cis largest number and here comes the output
on web browser
We can use various operators such as arithmetic operator, logical operator,
relational operators and so on in the if statement. Following JavaScript makes
use of such operators.
JavaScript[ifElse Demo.html]
<!DOCTYPE html>
<html>
<head>
<title>If else Demo</title>
</head>
<body>
<script type="text/javascript'>
var marks;
marks=80;
if(marks<40)
document.write("You are failed");
else if(marks>=40 && marks<50)
document.write("You are passed'");
else if(marks> =50 && marks<60)
document.write("You have got Second class");
d=new Date (;
ch=d.getMonth();
switch(ch)
case 0: adocument.write(January )
break
case 1: document.Write(February");
break;
case 2: document. Wnte("March'");
break;
case 3: document.write("April"
break;
case 4: document.write('May );
break;
case 5: document.write('June');
break;
case 6: document.write(July)
break;
case 7: document.write("August )
break;
case 8: document.write('September");
break;
case 9: document.write('October);
break;
case 10: document.write('November);
break
case 11: document.write(December);
break;
Prepared By:Mr Vikas Ligade. Page 13
Chapter:1 Basics Of JavaScript Programming.
</script>
</body>
</html>
Script Explanation: In this script,
(1) We have used the object Date which returns the value of the current date.
(2) Then using getMonth() method the month value can be obtained. The month
value starts from 0 t011 representing January to December. This value is
basically obtained from system date function. The month is taken in variable ch
according to the value in ch the corresponding case will get executed.
1.7 Loop Statement
1.7.1 while Loop
while statements help us in implementing the iterative logic of the program. The
syntax of while is a
follows-
Some initial condition;
while(terminating condition)
{
//some statements;
stepping condition;
}
The while is implemented by following JavaScript.
<html>
<head>
<script type="text/javascript">
document.write("<b>Using while loops </b><br />");
var i = 0, j = 1, k;
document.write("<b>Fibonacci series less than 40</b><br />");
while(i<40)
{
document.write(i + "<br />");
k = i+j;
i = j;
j = k;
}
</script>
</head>
<body>
</body>
</html>
JavaScript[DoWhile.html]
<html>
<head>
<script type="text/javascript">
document.write("<b>Using do...while loops </b><br />");
var i = 2;
document.write("Even numbers less than 20<br />");
do
{
document.write(i + "<br />");
i = i + 2;
}while(i<=20)
</script>
</head>
<body>
</body>
</html>
if (x == 5) {
continue; // skip rest of the loop body
}
document.write( x + "<br />");
}
document.write("Exiting the loop!<br /> ");
</script>
</body>
</html>