JavaScript:
JavaScript is a scripting language
that allows creation of interactive web pages. JavaScript allows user entries
which are loaded into a HTML form to be processed as required. This feature
empowers a website to return site information according to a user request.
Advantages
1. An
interpreted language
2. Embedded
with HTML
3. Quick
development and easy to learn
4. Procedural
capability:
This
language supports facility for checking, looping and branching.
5. Easy
debugging and testing
6. Platform
Independent
Structure of JavaScript section:
<head>
<script
language=”Text/Javascript” >
Code
</script>
</head>
Data types in JavaScript:
1.
Number:
This
data type consists of integer and floating point numbers and a special value
NaN (Not a Number).
2.
Boolean:
It
consists of the logical value either true or false.
JavaScript
automatically converts the Boolean value (true/false) into one and zero when
they are used in numerical expression.
3.
String:
Is
consists of string values that are enclosed in single or double quotes.
For
ex:
’Manak Nagar , Rajnagar’
“Radhey”
4.
Null:
It
consists of single value NULL which identifies an empty or non existence reference.
Some Important Points:-
1. JavaScript
is also “non case-sensitive” language.
2. Declaration
of variables in JavaScript should be in case-sensitive manner.
3. Variable
names can begin with an uppercase letter, lower case letter, an underscore
character (_) or a dollar sign character ($). The remaining character can
consists lower case, upper case, special symbols or digit (0-9).
4. JavaScript
does not allow the data type of the variable to be declared when a variable is
created.
The same variable may be used to hold different types of data at
different times with a different code snippet.
Literal:
Literals
are fixed values which never change in a program.
Integer
literal can be represented in JavaScript in the form of
decimal/hexadecimal/octal.
Creating variable in JavaScript:
Syntax:-
var “variable_name” = value;
Ex:
var num=1000;
var num=”string”;
The
equal sign is used to assign a value to a variable which is known as assignment
operator.
Note:
<script> tag is used to
implement a script.
Language=”javascript”
Type=”text/javascript” language
& type attributes are used with script tag to identify which type of
scripting language is being used.
JavaScript Practice:
Program 1
<html>
<head>
<script type=”text/javascript”>
document.write
(“Hello world”);
</script>
</head>
<body>
</body>
</html>
If
a user wants to show the text in the next line so it should use
document.writeln instead of document.write.
Program 2
Write HTML Tags with javascript –
<html>
<head>
<script language=”javascript”>
document.write
(“<h1>Hello world</h1>”);
</script>
</head>
<body>
</body>
</html>
Program 3
Javascript in body section –
<html>
<head>
</head>
<body>
<script type=”text/javascript”>
document.write
(“Hello world”);
</script>
</body>
</html>
Program 4
An external Javascript –
<html>
<head>
<script src=”javascript
path (ex: x.js)”>
</script>
</head>
<body>
<script
src=”javascript path (ex: y.js)”>
</script>
</body>
</html>
Program 5
WAHP which use as Javascript and
gives your name, contact no. And full address in different line.
<html>
<head>
</head>
<body>
<script language=”javascript”>
document.write
(“Name= Lucky”);
document.writeln
(“Contact No. = 7737873339”);
document.writeln
(“Address : Manak Nagar, 100 ft road, Rajnagar”);
</script>
</body>
</html>
Program 6
WAHP which use as Javascript and
declare a variable assigning a value to it and display it
<html>
<head>
</head>
<body>
<script language=”javascript”>
Var a=10;
Document.writeln(“My
age is : ”+a);
</script>
</body>
</html>
Program 7
WAHP which use as Javascript and
declare a variable assigning a value to it and display it
<html>
<head>
</head>
<body>
<script
language=”javascript”>
Var name=”Govinda”
Document.writeln(name);
name=”Lucky”;
Document.writeln(name);
Document.writeln(“My
name is : ”+name+”I am in III year”);
</script>
</body>
</html>
Selection/Conditional Statements in
JavaScript :
Program 8 ( If Statement )
WAHP which use as Javascript and use
if statement
<html>
<head>
</head>
<body>
<script
language=”javascript”>
var
day=”saturday”;
if(day=”saturday”)
document.write(“It
is saturday”);
</script>
</body>
</html>
Program 9 ( If-else Statement )
WAHP which use as Javascript and
use if-else statement
<html>
<head>
</head>
<body>
<script
language=”javascript”>
var a=”10”;
var b=”20”;
if(a>b)
document.write(a+“
is greater than ”+b);
else
document.write(b+“
is greater than ”+a);
</script>
</body>
</html>
Program 10 ( Switch-case Statement
)
WAHP which use as Javascript and use
switch-case statement
<html>
<head>
</head>
<body>
<script
language=”javascript”>
var day=”5”;
switch(day)
{
case 1:
document.write(“Monday”);
break;
case 2:
document.write(“Tuesday”);
break;
case 3:
document.write(“Wednesday”);
break;
case 4:
document.write(“Thursday”);
break;
case 5:
document.write(“Friday”);
break;
case 6:
document.write(“Saturday”);
break;
case 7:
document.write(“Sunday”);
break;
default :
document.write(“Not
a valid day”);
}
</script>
</body>
</html>
Program 11 ( Immediate If)
WAHP which use as Javascript and use
Immediate if (Conditional operator/Ternary operator)
<html>
<head>
</head>
<body>
<script
language=”javascript”>
var day=”saturday”;
(day==”saturday”)?document.write(“Weekend”):document.write(“Not
Weekend”);
</script>
</body>
</html>
Loop in JavaScript :
Program 12 ( For Loop)
WAHP which use as Javascript and use
for loop
<html>
<head>
</head>
<body>
<script
language=”javascript”>
var n;
for(n=1;n<=10;n++)
{
Document.writeln(“value is : ”+n);
}
</script>
</body>
</html>
Program 13 (While Loop)
WAHP which use as Javascript and
use While loop
<html>
<head>
</head>
<body>
<script
language=”javascript”>
var n;
n=1;
while(n<=10)
{
Document.writeln(“value is : ”+n);
n++;
}
</script>
</body>
</html>
Program 14 (Do-While Loop)
WAHP which use as Javascript and
use Do-While loop
<html>
<head>
</head>
<body>
<script
language=”javascript”>
var n;
n=1;
do
{
Document.writeln(“value is : ”+n);
n++;
}while(n<=10);
</script>
</body>
</html>
Program 15
WAHP which use as Javascript and
prints a table
<html>
<head>
</head>
<body>
<script
language=”javascript”>
var n=2;
var i,t;
for(i=1;i<=10;i++)
{
t=n*i;
Document.writeln(n + “ X ”+ i +” = “ + t);
}
</script>
</body>
</html>
thanks
ReplyDelete