Unit I Basics of Java Script Programming
Unit I Basics of Java Script Programming
PROGRAMMING
MR. S. P. KHOLAMBE
LECTURER IN CO DEPTT.,
MET BKC IOTP NASHIK
Features of Java Script
3
Features of Java Script
5
Limitations of JavaScript
Hello World....!
Put JavaScript Code
9
Between the body tag of html Between the head tag of html
<html> <html>
<body> <head>
<script type="text/javascript"> <script type="text/javascript">
document.write("Hello JavatScript"); function msg(){
</script> alert("Hello CSS");
</body> }
</html> </script>
</head>
<body>
<p>Welcome to Javascript</p>
<form>
<input type="button" value="click"
onclick="msg()"/>
</form>
</body>
</html>
External JavaScript file
message.js index.html
12
Single Line Comment Multi Line Comment
<html> <html>
<body> <body>
<script> <script>
var a=1,b=20,c; /* It is multi line comment.
var c=a+b;//It adds values of a and b It will not be displayed */
variable document.write("Example of
document.write(c);//prints sum of 10 javascript multiline comment");
and 20 </script>
</script> </body>
</body> </html>
</html>
Output:
Output: It will display on Web Browser. Example of
21 javascript multiline comment
Object Name, Property, Method, Dot Syntax, Main Event
Object Name:
1. JavaScript is a OOP language.
14
Object Name, Property, Method, Dot Syntax, Main Event
Property:
1. Property is actually a value associated with each object.
Methods:
1. It is a function associated with each object.
Dot Syntax:
1. Every object is associated with property & methods.
2. For accessing the properties & methods of an object we use dot
operator.
Main Event:
1. Event is something happen when JavaScript execute.
2. Execution of appropriate code on occurrence of event is called event
handling.
3. Some function are defined for event handling. These function are called
event handler.
16
Values
17
Variable
Local Variable:
A JavaScript local variable is declared inside block or function.
It is accessible within the function or block only.
Global Variable:
A JavaScript global variable is accessible from any function. A
variable i.e. declared outside the function or declared with
window object is known as global variable.
19
Local Variable
<html>
<body>
<script>
function abc()
{
var x=10;//local variable
document.write("Local Variable is: " ,x)
}
abc()
</script>
</body>
</html>
A list of all the reserved words in JavaScript are given in the following table.
They cannot be used as JavaScript variables, functions, methods, loop labels, or
any object names.
22
Operators & Expression
1. Arithmetic Operators
2. Comparison Operators
3. Logical (or Relational ) Operators
4. Bitwise Operators
5. Assignment Operators
6. Conditional (or ternary) Operators
23
Arithmetic Operators
24
<html> document.write("a / b = ");
<body> document.write(a / b);
<script type="text/javascript"> document.write(linebreak);
26
<html> document.write("(a != b) : ", (a != b));
<body> document.write(linebreak);
28
<html>
<body> Output:
<script type="text/javascript"> Logical Operators:
var a = true; (a && b) : false
var b = false; (a || b) : true
var linebreak = "<br />"; !(a && b) : true
document.write("Logical Operators");
document.write(linebreak);
</script>
</body>
</html>
Bitwise Operators
Sr. No. Operator & Description
1 & (Bitwise AND)
It performs a Boolean AND operation on each bit of its integer arguments.
Ex: (A & B) is 2.
2 | (Bitwise OR)
It performs a Boolean OR operation on each bit of its integer arguments.
Ex: (A | B) is 3.
3 ^ (Bitwise XOR)
It performs a Boolean exclusive OR operation on each bit of its integer arguments.
Ex: (A ^ B) is 1.
4 ~ (Bitwise Not)
It is a unary operator and operates by reversing all the bits in the operand.
Ex: (~B) is -4.
5 << (Left Shift)
Ex: (A << 1) is 4.
6 >> (Right Shift)
Ex: (A >> 1) is 1.
7 >>> (Right shift with Zero)
30
Ex: (A >>> 1) is 1.
<html>
<body> document.write("(~b) : ", (~b));
document.write(linebreak);
<script type="text/javascript">
var a = 2; // Bit presentation 0010 document.write("(a << b) : ", (a << b));
var b = 3; // Bit presentation 0011 document.write(linebreak);
var linebreak = "<br />";
document.write("(a >> b) : ", (a >> b));
document.write("Bitwise Operator :"); document.write(linebreak);
document.write(linebreak); </script>
</body>
document.write("(a & b) : ",(a & b)); </html>
document.write(linebreak);
Output:
document.write("(a | b) : ", (a | b)); Bitwise Operator :
document.write(linebreak); (a & b) : 2
(a | b) : 3
document.write("(a ^ b) : ", (a ^ b)); (a ^ b) : 1
document.write(linebreak); (~b) : -4
(a << b) : 16
(a >> b) : 0
Assignment Operators
32
<html> document.write("Value of a => (a *=
<body> b) : ", (a *= b));
document.write(linebreak);
<script type="text/javascript">
var a = 33; document.write("Value of a =>
var b = 10; (a /= b) : ", (a /= b));
var linebreak = "<br />"; document.write(linebreak);
document.write("Value of a =>
document.write("Assignment Operators:");
(a %= b) : ", (a %= b));
document.write(linebreak);
document.write(linebreak);
</script>
document.write("Value of a => (a = b) : ", (a = b)); </body>
document.write(linebreak); </html>
document.write("Conditional Operator:");
document.write(linebreak);
document.write("typeof Operator:")
document.write(linebreak);
Output:
Qualifies for driving
If….else Statement
The 'if...else' statement is the next form of control statement that allows
JavaScript to execute statements in a more controlled way.
Syntax:
if (expression)
{
Statement(s) to be executed if expression is true
} else {
Statement(s) to be executed if expression is false
}
If the resulting value is true, the given statement(s) in the ‘if’ block, are
executed. If the expression is false, then the given statement(s) in the else
block are executed.
<html>
<body>
<script type = "text/javascript">
<!--
var age = 15;
if( age > 18 ) {
document.write("<b>Qualifies for driving</b>");
} else {
document.write("<b>Does not qualify for driving</b>");
}
</script> Output:
</body> Does not qualify for driving
</html>
if...else if... statement
The 'for' loop is the most compact form of looping. It includes the
following three important parts −
1. The loop initialization where we initialize our counter to a starting
value. The initialization statement is executed before the loop begins.
2. The test statement which will test if a given condition is true or not. If
the condition is true, then the code given inside the loop will be
executed, otherwise the control will come out of the loop.
3. The iteration statement where you can increase or decrease your
counter.
Syntax:
for (initialization; test condition; iteration statement)
{ Statement(s) to be executed if test condition is true }
<html>
<body>
<script type = "text/javascript">
<!--
var count;
Output:
document.write("Starting Loop" + "<br />");
for(count = 0; count < 10; count++) Starting Loop
{ Current Count : 0
Current Count : 1
document.write("Current Count : " + count ); Current Count : 2
document.write("<br />"); Current Count : 3
} Current Count : 4
document.write("Loop stopped!"); Current Count : 5
Current Count : 6
</script> Current Count : 7
</body> Current Count : 8
</html> Current Count : 9
Loop stopped!
For….in Loop
The prompt dialog box is very useful when you want to pop-up a
text box to get user input. Thus, it enables you to interact with
the user. The user needs to fill in the field and then click OK.
This dialog box is displayed using a method called prompt() which
takes two parameters:
(i) a label which you want to display in the text box and
(ii) a default string to display in the text box.
This dialog box has two buttons: OK and Cancel. If the user clicks
the OK button, the window method prompt() will return the
entered value from the text box.
<html>
<head>
<script type = "text/javascript">
<!--
function getValue() {
var retVal = prompt("Enter your name : ", "your name here");
document.write("You have entered : " + retVal);
}
</script>
</head>
<body>
<p>Click the following button to see the result: </p>
<form>
<input type = "button" value = "Click Me" onclick = "getValue();" />
</form>
</body>
</html>