WT Unit Ii
WT Unit Ii
What is Script?
Simple easy and weakly typed program.
The term “scripting language” is also loosely to refer
dynamic high-level general-purpose languages, such as
JavaScript, VBScript, Perl, and Python, with the term
“script”.
Scripting languages are becoming more popular due to
the emergence of web-based applications.
Also the increase in computer performance over the past
few years has promoted a increase in the power and
sophistication of scripting languages.
Weakly or Loosely (Some of the things not
mandatory)
Variable declaration not compulsory
Programmer friendly
Introduction to Java Scripts:
JavaScript is a scripting language i.e., a
lightweight programming language that is
interpreted by the browser engine when the
web page is loaded.
JavaScript it the Scripting language of HTML and
the web
Original name is Live Script
Syntax is very close to C-language
JavaScript was created by Brendan Eich at
Netscape and was first introduced in December
1995 under the name of LiveScript.
It was rather quickly renamed JAVASCRIPT.
JavaScript's official name is ECMA
Script(European Computer Manufacturers
ATTRIBUTE
SYNTAX:
TAG PARAMETE
RS
OR
OBJEC
METHO
T
D
<head>
<script type=“text/javascript”
language=“javascript”>
Document.write(“HELOO<br>”);
Document.write(“HI<br>”);
</script>
</head>
2.Script in <body>…..</body>section:
<body>
<script type=“text/javascript”
language=“javascript”>
document.write(“HELOO<br>”);
document.write(“HI<br>”);
</script>
<body>
3.Script in <body>…..</body>&
<head>….</head>:
<head>
<script type=“text/javascript” >
document.write(“Hello Iam from HEAD”);
</script>
</head>
<body>
<script language=“javascript”>
document.write(“Hello Iam from BODY
section”);
document.write(“HI<br>”);
</script>
External JavaScript
The script code can be written in separate file and save
with .js extension.
The js file does not contain script tag
In real time industry generally we are using
external java script.
if(condition1)
{ code to be executed if condition1 is true
}
else if(condition2)
{
code to be executed if condition2 is true
}
else
{
code to be executed if condition1 and condition2 are false
}
<html>
<head>
<title>
elseif conditional statement
</title>
<script language = "javascript">
var stdmarks;
stdmarks = prompt("enter student marks" ,"stdmarks");
if(stdmarks>=90)
document.writeln("GRADE A");
elseif(stdmarks>=80)
document.writeln("GRADE B");
elseif(stdmarks>=70)
document.writeln("GRADE C");
elseif(stdmarks>=60)
document.writeln("GRADE D");
else
document.writeln("failed grade");
</script>
</head>
</html>
Switch Case Statement
JavaScript switch statement evaluates a switch
condition, base on switch condition value matched
case will run until break or end of case statement.
Syntax
switch(expression)
{
case n:
code block
break;
case n:
code block
break;
default:
code block
}
Loops in Java Script
In Java there are three kinds of loops
1. for : loops through a block of code a
specified number of times
2. while : loops through a block of code
while a condition is true. The condition is
tested at beginning of the loop
3. do .. while : loops through a block of
code while a specified condition is true.
The condition is tested at the end of the
loop.
1. for loop:
The for loop is commonly used when you
know the number of iterations in advance.
Syntax:
for (initialization; condition;
increment/decrement)
{
// code to be executed
}
Example:
for (let i = 0; i < 5; i++)
{
console.log(i);
}
2. while loop:
The while loop continues to execute a block of code
as long as the specified condition evaluates to true.
Syntax:
while (condition) {
// code to be executed
}
Example:
let i = 0;
while (i < 5) {
console.log(i);
i++;
}
3. do-while loop:
Similar to the while loop, but it ensures that
the block of code is executed at least once,
as the condition is checked after the block.
Syntax:
do {
// code to be executed
} while (condition);
Example:
let i = 0;
do {
console.log(i);
i++;
} while (i < 5);
<html>
<head>
<title>
do-while loop
</title>
<script language = "javascript">
var i=0;
do
{
document.writeln("the number is :" +i);
document.writeln("<br>");
i++;
}while(i<=5);
</script>
</head>
</html>
Break and Continue
The break command will break the loop
and continue executing the code that
follows after the loop (if any)
<script>
function ShowMessage()
{
alert("Hello World!");
}
ShowMessage();
</script>
</body>
</html>
It is possible to return some value from the function
using return keyword .
We can pass some arguments to the function .
syntax :
functiondefinition : function my_function(str1,str2) { }
function call : my_function(“hi”,”hello”);
Function literals :
Function literal is also used to create a function .
syntax : var variablename=function(arg1,arg2…)
{
statements ;
};
This type of functions is referred in two ways :
1.By using variable name .
2.By using Function name .
we can also create a function by using function constructor .
syntax :
var var_name=new Function(“arg1”,”arg2”,”body of function”);
Example:
<html>
<head>
<script type="text/javascript" src="myfun.js"></script>
</head>
<body>
<p>Welcome to JavaScript</p>
<form>
<input type="button" value="click" onclick="msg()"/>
</form>
</body>
</html>
myfun.js:
function msg(){
alert("Hello Javatpoint");
}
Syntax:
function functionname(arg1, arg2)
{
lines of code to be executed return val1;
}
<html> <head>
<script type="text/javascript">
var func = function(x,y){ return x*y };
function secondFunction(){
var result;
result = func(1,20);
document.write ( result );
}
</script>
</head>
<body>
<p>Click the following button to call the function</p>
<form>
<input type="button" onclick="secondFunction()" value="Call
Function">
</form>
</body> </html>
Recursion
Recursion is a programming concept where a
function calls itself in its own definition. This can
be a powerful and elegant way to solve certain
types of problems. In JavaScript, as in many
other programming languages, you can use
recursion
function to solve{problems that can be broken
factorial(n)
down
// Baseinto smaller,
case: if n is 0 similar subproblems.
or 1, return 1
if (n === 0 || n === 1) {
return 1;
} else {
// Recursive case: n! = n * (n-1)!
return n * factorial(n - 1);
}
}
// Example usage:
let result = factorial(5); // calculates 5! = 5 * 4 * 3 * 2 * 1
= 120
console.log(result); // prints 120
Global functions
Global functions are functions that are
defined in the global scope and can be
accessed from anywhere in the code. These
functions are not bound to any particular
object or class and can be invoked directly.
Here are a few examples of commonly used
global functions in JavaScript:
alert("Hello, World!"); // Displays an alert
box
confirm("Are you sure?"); // Displays a
confirmation box
console.log("This
prompt("Enter youris a log message");
name:"); // Displays a
prompt box