SL Unit-1
SL Unit-1
Design These make the coding process These provide full usage of the
simple and fast. languages.
Complexity These are very easy to use and These are pretty complex in
easy to write. terms of writing and usage.
Type of Coding Scripting languages help Programming languages
write a small piece of an help write the full code
Parameter Scripting language Programming languages
entire code. concerning a program.
Developing Time These take less time because These take more time
they involve lesser code. because a programmer must
write the entire code.
Length of Codes These involve very few and These require numerous
short coding lines. lines of coding for a single
function.
Web Scripting
• The process of creating and embedding scripts in a web page is
known as web-scripting.
• A script or a computer-script is a list of commands that are
embedded in a web-page normally and are interpreted and executed
by a certain program or scripting engine.
• Scripts may be written for a variety of purposes such as for
automating processes on a local-computer or to generate web pages.
• The programming languages in which scripts are written are called
scripting language, there are many scripting languages available
today.
• Common scripting languages
are VBScript, JavaScript, ASP, PHP, PERL, JSP etc.
Types of Script
• Scripts are broadly of following two type :
Client-Side Scripts , Server-Side Scripts
Client-Side Scripts :
1. Client-side scripting is responsible for interaction within a web
page.
2. The client-side scripts are firstly downloaded at the client-end and
then interpreted and executed by the browser (default browser of the
system).
3. The client-side scripting is browser-dependent. i.e., the client-side
browser must be scripting enables in order to run scripts.
4. Client-side scripting is used when the client-side interaction is used.
Some example uses of client-side scripting may be :
1. To get the data from user’s screen or browser.
2. For playing online games.
3. Customizing the display of page in browser without reloading or
reopening the page.
• Here are some popular client-side scripting languages VBScript,
JavaScript, Hypertext Processor(PHP).
Server-Side Scripts :
1. Server-side scripting is responsible for the completion or
carrying out a task at the server-end and then sending the result
to the client-end.
2. In server-side script, it doesn’t matter which browser is being
used at client-end, because the server does all the work.
3. Server-side scripting is mainly used when the information is
sent to a server and to be processed at the server-end.
4.Some sample uses of server-scripting can be :
• Password Protection.
• Browser Customization (sending information as per the
requirements of client-end browser)
• Form Processing
• Building/Creating and displaying pages created from a database.
• Dynamically editing or adding content to a web-page.
5. Here are some popular server-side scripting languages PHP, Perl, ASP
(Active Server Pages), JSP ( Java Server Pages).
JAVA SCRIPT
• JavaScript is the Programming Language for the
Websites.
• The first scripting language was introduced by
Brendan Eich in 1996
• JavaScript is an object-based scripting language
which is lightweight and cross-platform.
• JavaScript can update and change both HTML and
CSS.
• JavaScript can calculate, manipulate and validate
data.
Features of JavaScript
P2:
<html>
<body>
<script type="text/Javascript">
alert("Hello JavaScript");
</script>
</body>
</html>
Output:
JavaScript that displays alert dialog box.
Output screen
2) code between the head tag
<html>
<head>
<script type="text/javascript">
function msg()
{
alert("Hello Javascript");
}
</script>
</head>
<body>
<p>Welcome to JavaScript</p>
<form>
<input type="button" value="click" onclick="msg()"/>
</form>
</body>
</html>
Output screen
External JavaScript file
function msg(){
alert("Hello Javascript");
}
Output screen
Advantages of External JavaScript
Synyax
/* your code here */
<html>
<body>
<script>
/* It is multi line comment.
It will not be displayed */
document.write("example of javascript multiline comment");
</script>
</body>
</html>
JavaScript Display Possibilities
JavaScript can "display" data in different ways:
• Writing into an HTML element, using innerHTML.
• Writing into the HTML output using document.write().
• Writing into an alert box, using window.alert().
• Writing into the browser console, using console.log().
Using innerHTML
• To access an HTML element, JavaScript can use
the document.getElementById(id) method.
• The id attribute defines the HTML element. The innerHTML property
defines the HTML content:
• The innerHTML property can be used to write the dynamic html on the
html document.
• It is used mostly in the web pages to generate the dynamic html such as
registration form, comment form, links etc.
innerHTML
<html>
<body>
<h1>My First Web Page</h1>
<p>My First Paragraph</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = 5 + 6;
</script>
</body>
</html>
Output:
My First Web Page
My First Paragraph.
11
Properties of document object
Methods of document object
document.write()
<!DOCTYPE html>
<html>
<body>
<script>
document.write(5 + 6);
</script>
</body>
</html>
Javascript -
document.getElementById() method
<script type="text/javascript">
function getcube()
{
var number=document.getElementById("number").value;
alert(number*number*number);
}
</script>
<form>
Enter No:<input type="text" id="number" name="number"/><br/>
<input type="button" value="cube" onclick="getcube()"/>
</form>
Using window.alert()
• You can use an alert box to display data:
<html>
<body>
<script>
window.alert(5 + 6);
</script>
</body>
</html>
JavaScript Debugging
//gloabal variable
function a(){
document.writeln(“data from a function”data);
}
function b(){
document.writeln(“data from b function “+data);
}
a();//calling JavaScript function
b();
</script>
JavaScript Variable
• The var keyword is used in all JavaScript code from 1995 to 2015.
• The let and const keywords were added to JavaScript in 2015.
• If you want your code to run in older browsers, you must use var.
</body>
</html>
JavaScript Identifiers
• All JavaScript variables must be identified with unique names.
• These unique names are called identifiers.
• Identifiers can be short names (like x and y) or more descriptive
names (age, sum, totalVolume).
if(a == 2)
{
var b = 3;
document.write(a + ' ' + b);
}
document.write(a + ' ' + b);
}
greet();
</script>
</body>
</html>
NOTE
The block scope does not work with the var keyword. You can either use let or
const keywords for that.
<html><body>
<script>
{
var a="hey";
let b="welcome to";
const c="class";
console.log(a);
console.log(b);
console.log(c);
}
console.log(a);
//console.log(b);
//console.log(c);
</script>
</body>
</html>
JavaScript Data Types
There are five types of primitive data types in JavaScript. They are
as follows
Data Type Description
BigInt
• In JavaScript, the “number” type cannot safely represent integer
values larger than (253-1) (that’s 9007199254740991), or less than -
(253-1) for negatives.
String
In JavaScript, there are 3 types of quotes.
1. Double quotes: "Hello".
2. Single quotes: 'Hello'.
3. Backticks: `Hello`.
• Double and single quotes are “simple” quotes. There’s practically no
difference between them in JavaScript.
• Backticks are “extended functionality” quotes. They allow us to
embed variables and expressions into a string by wrapping them
in ${…}
let name = "John";
// embed a variable
alert( `Hello, ${name}!` ); // Hello, John!
// embed an expression
alert( `the result is ${1 + 2}` ); // the result is 3
• The expression inside ${…} is evaluated and the result becomes a
part of the string.
<html>
<body>
<script>
let i=10;
var str=`hello,${i}`;
document.write(str);
</script>
</body>
</html>
<html>
<body>
<script>
let name="john";
var str=`hello,${name}`;
document.write(str);
</script>
</body>
</html>
String Length
To find the length of a string, use the built-in length property:
Boolean (logical type)
• The boolean type has only two values: true and false.
• This type is commonly used to store yes/no values: true means “yes,
correct”, and false means “no, incorrect”.
The “null” value
• The special null value does not belong to any of the types.
• It forms a separate type of its own which contains only
the null value:
let age = null;
• It’s just a special value which represents “nothing”, “empty” or
“value unknown”.
• The code above states that age is unknown.
The “undefined” value
• It makes a type of its own, just like null.
•The meaning of undefined is “value is not assigned”.
•If a variable is declared, but not assigned, then its value is undefined:
Example:
let age;
alert(age); // shows "undefined"
Technically, it is possible to explicitly assign undefined to a variable
JavaScript Date Objects
• A new Date() creates a date object with the current date and time:
<html>
<body>
<h1>JavaScript Dates</h1>
<h2>Using new Date()</h2>
<p>Create a new date object with the current date and time:</p>
<p id="demo"></p>
<script>
const d = new Date();
document.getElementById("demo").innerHTML = d;
</script>
</body>
</html>
output
JavaScript Array
default:
code to be executed if above values are not matched;
}
<html>
<body>
<script>
var grade='A';
var result;
switch(grade)
{
case 'A':
result="A Grade";
break;
case 'B':
result="B Grade";
break;
case 'C':
result="C Grade";
break;
default:
result="No Grade";
}
document.write(result);
</script>
</body>
</html>
Loops in JavaScript
A while loop is a control flow statement that allows code to be executed repeatedly based on a given Boolean condition.
The while loop can be thought of as a repeating if statement.
Syntax :
while (boolean condition)
{
loop statements...
}
• While loop starts with checking the condition. If it is evaluated to be
true, then the loop body statements are executed otherwise first
statement following the loop is executed. For this reason, it is also
called the Entry control loop
• Once the condition is evaluated to be true, the statements in the loop
body are executed. Normally the statements contain an update value
for the variable being processed for the next iteration.
• When the condition becomes false, the loop terminates which marks
the end of its life cycle.
<html>
<body>
<script>
var i=11;
while (i<=15)
{
document.write(i + "<br/>");
i++;
}
</script>
</body>
</html>
for loop:
1. The do-while loop starts with the execution of the statement(s). There is no checking of any
condition for the first time.
2. After the execution of the statements, and update of the variable value, the condition is checked for a
true or false value. If it is evaluated to be true, the next iteration of the loop starts.
3. When the condition becomes false, the loop terminates which marks the end of its life cycle.
4. It is important to note that the do-while loop will execute its statements at least once before any
condition is checked, and therefore is an example of the exit control loop.
<html>
<body>
<script>
var i=21;
do {
document.write(i + "<br/>");
i++;
}while (i<=25);
</script>
</body>
</html>
JavaScript Functions
<h2>JavaScript Functions</h2>
<p id="demo"></p>
<script>
var x = myFunction(4, 3);
document.getElementById("demo").innerHTML = x;
function myFunction(a, b)
{
return a * b;
}
</script>
</body>
</html>
Object
• In JavaScript, an object is a collection of properties, where each
property is defined as a key-value pair.
• The following example defines an empty object using the object
literal syntax:
let emptyobject={}
• The following example defines the person object with two
properties: firstName and lastName.
let person = {
firstName: ’john’,
lastName:’Deo’
};
To access a object’s property, you can use
•The dot notation (.)
•The array-like notation ([]).
console.log(person.firstName);
console.log(person.lastName);
(or)
console.log(person[‘firstName’]);
console.log(person[‘LastName’]);
<html>
<body>
<script>
let person = {
firstName: "john",
lastName:"Deo",
age:22
};
document.write(person.firstName+"<br/>");
document.write(person.lastName+"<br>");
document.write(person.age);
</script>
</body>
</html>
<html>
<body>
<script>
let person = {
firstName: "john",
lastName:"Deo",
age:22
};
document.write(person['firstName']+"<br/>");
document.write(person['lastName']+"<br>");
document.write(person['age']);
</script>
</body>
</html>
<html>
<body>
<script>
let person = {
firstName: "john",
lastName:"Deo",
age:22
};
document.write(person.firstName+"<br/>");
delete person.firstName;
document.write(person.firstName+"<br>");
document.write(person['age']);
</script>
</body>
</html>
JavaScript RegExp(Regular Expression)