Basics of Javascript Programming: K. K. Wagh Polytechnic, Nashik-3
Basics of Javascript Programming: K. K. Wagh Polytechnic, Nashik-3
Chapter 1
Basics of JavaScript Programming
Overview of JavaScript:
• It is dynamic Computer Programming language
• Lightweight and most commonly used for web pages.
• It interact with user and make a web page dynamic.
• Using HTML only static pages can be created.
• No user interaction with HTML
• JavaScript was developed by Netscape in 1995.
• Its initial name was LiveScript.
• Later on Sun Microsystems joined Netscape and the name
is changed to JavaScript.
Features of JavaScript:
<html>
<head></head>
<body>
<script type="text/javascript">
document.write("HELLO WORLD");
</script>
<p>this is web page body</p>
</body>
</html>
Explanation of Program:
Object Name:
• Object is entity. In JavaScript document, window, forms, fields,
buttons are some properly used objects.
• Each object is identified by ID or name.
• Array of objects or Array of collections can also be created.
Property:
• It is the value associated with each object.
• Each object has its own set of properties.
Method:
• It is a function or process associated with each object.
• Ex: for document object write is a method.
Dot Syntax: Used to access properties and methods of object.
Main Event: Event causes JavaScript to execute the code. In JavaScript
when user submits form, clicks button, writes something to text box the
corresponding event gets triggered and execution of appropriate code is
done through Event Handling.
Values:
Values:
It uses six types of values:
• Number: It is numeric value can be integer or float.
• String: It is a collection of characters. Enclosed within single or
double quote.
• Boolean: It contains true and false values. Values can be compared
with variables and can be used in assignment statement.
• Null: This value can be assigned by reserved word ‘null’. It means no
value. If we try to access null value error will occur.
• Object: It is the entity that represents some value. Ex: Form is an
object on which some components can be placed and used.
• Function: It is intended for execution of some task. There are
predefined and user defined functions. The ‘function’ keyword is used
to define function.
Keywords:
• These are the reserved words having some special meaning associated
with it.
abstract else instanceof super
boolean enum int switch
break export interface synchronized
byte extends let this
case false long throw
catch final native throws
char finally new transient
class float null true
const for package try
continue function private typeof
debugger goto protected var
default if public void
delete implements return volatile
do import short while
double in static with
Operators and Expressions:
switch (variable-name)
{
case value1:
Execute stmt1;
break;
case value2:
Execute stmt2;
break;
case valueN:
Execute stmtN;
break;
default:
Execute last;
}
Decision Making and Looping
1) While Loop
do
{
// Loop body;
}
while (condition is true);
e.g.
int a = 0;
do
{
document.write(a);
a++;
}while(a<50);
Decision Making and Looping
3) for Loop
for(initialization; condition; incr/decrement)
{
// Loop body;
}
e.g.
int a;
for(a=0;a<10;a++)
{
document.write(a);
}
OR
int a;
for(a=0;a<10;a++)
document.write(a);
Decision Making and Looping
4) for in Loop
• The for/in statement loops through the properties of an object.
• The block of code inside the loop will be executed once for each
property.
• Syntax:
for (var in object)
{
code block to be executed
}
• Parameter Values
Parameter Description
var Required. A variable that iterates over properties
of an object
object Required. The specified object that will be iterated
break Statement
• The break statement can also be used to jump out of a loop.
• The break statement breaks the loop and continues executing
the code after the loop (if any):
Syntax:
jump-statement;
break;
Ex:
for (i = 0; i < 10; i++)
{
if (i === 3)
{ break; }
text += "The number is " + i + "<br>";
}
break Statement
Output:
The number is 0
The number is 1
The number is 2
continue Statement
• The continue statement breaks one iteration (in the loop), if a
specified condition occurs, and continues with the next
iteration in the loop.
• Syntax:
jump-statement;
break;
Ex:
for (i = 0; i < 10; i++)
{
if (i ==3)
{ continue; }
text += "The number is " + i + "<br>";
}
continue Statement
Output:
The number is 0
The number is 1
The number is 2
The number is 4
The number is 5
The number is 6
The number is 7
The number is 8
The number is 9