UNIT I Basics of JavaScript Programming
UNIT I Basics of JavaScript Programming
Class : TY
Course : Client Side Scripting
Language(Elective)
Unit I : Basics of JavaScript
Programming
Lecture 01: Basics of JavaScript
Programming
1
02
Client Side Scripting Language
2
Departmentwww.sandipuniversity.edu.in
of Computer Engineering www.sandipfoundation.org
Form and Event Handling 03
Unit Outcomes(UOs)
1a. Create object to solve the given problem.
1b. Develop JavaScript to implement the switch-case statement for the given problem
1c. Develop JavaScript to implement loop for solving the given iterative problem.
1d. Display properties of the given object using letters and setters.
1e. Develop program using basic features of JavaScript to solve the given problem.
Contents
1.1 Features of JavaScript.
1.2 Object Name, Property, method, Dot syntax, main event.
1.3 Values and Variables.
1.4 Operators and Expressions – Primary expressions, Object and Array initializers, function
definition expression, property access expressions, invocation expressions.
1.5 If Statement, if…else, if….elseif, nested if statement.
1.6 Switch…case statement.
1.7 Loop statement – for loop, for…in loop, while loop, continue statement.
1.8 Querying and setting properties and deleting properties, property getters and setters.
3
Department of Computer Engineering www.sandipfoundation.org
Teaching and examination Scheme 04
Unit Outcomes(UOs)
• Create object to solve the given problem.
• Develop JavaScript to implement the switch-case statement for the given problem.
• Develop JavaScript to implement loop for solving the given interactive problem.
• Display properties of the given object using getters and setters.
• Develop program using basic features of JavaScript to solve the given problem.
4
Department of Computer Engineering www.sandipfoundation.org
Basis of Comparison between Programming vs Scripting 04
It is the program that runs on server dealing with the generation of content of web page.
1) Querying the database
2) Operations over databases
3) Access/Write a file on server.
4) Interact with other servers.
5) Structure web applications.
6) Process user input. For example if user input is a text in search box, run a search algorithm on
data stored on server and send the results.
Examples :
The Programming languages for server-side programming are :
1) PHP
2) C++
3) Java and JSP
4) Python
5) Ruby on Rails
6
Department of Computer Engineering www.sandipfoundation.org
Client#side Programming 04
• It is the program that runs on the client machine (browser) and deals with the user
interface/display and any other processing that can happen on client machine like
reading/writing cookies.
1) Interact with temporary storage
2) Make interactive web pages
3) Interact with local storage
4) Sending request for data to server
5) Send request to server
6) work as an interface between server and user
BASIS FOR
SERVER-SIDE SCRIPTING CLIENT-SIDE SCRIPTING
COMPARISON
Works in the back end which could not Works at the front end and script
Basic
be visible at the client end. are visible among the users.
Does not need interaction with the
Processing Requires server interaction.
server.
PHP, ASP.net, Ruby on Rails,
Languages involved HTML, CSS, JavaScript, etc.
ColdFusion, Python, etcetera.
• For a Web page, HTML supplies document content and structure while CSS provides
presentation styling
• In addition, client-side scripts can control browser actions associated with a Web page.
• Client-side scripts are almost written in the Javascript language to control browser’s
actions.
• Client-side scripting can make Web pages more dynamic and more responsive
9
Department of Computer Engineering www.sandipfoundation.org
Tasks performed by client#side scripts 04
10
Department of Computer Engineering www.sandipfoundation.org
Pros & Cons of Client Side Scripting 04
Pros
• Allow for more interactivity by immediately responding to users’ actions.
• Execute quickly because they do not require a trip to the server.
• The web browser uses its own resources, and eases the burden on the server.
• It saves network bandwidth.
Cons
• Code is loaded in the browser so it will be visible to the client.
• Code is modifiable.
• Local files and databases cannot be accessed.
• User is able to disable client side scripting
11
Department of Computer Engineering www.sandipfoundation.org
Overview of JavaScript 04
12
Department of Computer Engineering www.sandipfoundation.org
Features of JavaScript 04
13
Department of Computer Engineering www.sandipfoundation.org
How to write JavaScript Document? 04
• JavaScript can be implemented using JavaScript statements that are placed within the <script>...
</script> HTML tags in a web page.
• You can place the <script> tags, containing your JavaScript, anywhere within your web page, but it is
normally recommended that you should keep it within the <head> tags.
• The <script> tag alerts the browser program to start interpreting all the text between these tags as a
script.
• A simple syntax of your JavaScript will appear as follows.
<script ...>
JavaScript code
</script>
• The script tag takes two important a5ributes −
• Language − This aHribute specifies what scripJng language you are using.
• Type − This aHribute is what is now recommended to indicate the scripJng language in use and its
value should be set to "text/javascript".
• So your JavaScript segment will look like −
<script language = "javascript" type = "text/javascript">
JavaScript code
</script>
14
Department of Computer Engineering www.sandipfoundation.org
Your First JavaScript Code 04
<html>
<body>
<script language = "javascript" type = "text/javascript">
<!--
document.write("Hello World!")
//-->
</script>
</body>
</html>
15
Department of Computer Engineering www.sandipfoundation.org
Enabling JavaScript in Browsers 04
• JavaScript in Firefox
• Here are the steps to turn on or turn off JavaScript in Firefox −
• Open a new tab → type about: config in the address bar.
• Then you will find the warning dialog. Select I’ll be careful, I promise!
• Then you will find the list of configure options in the browser.
• In the search bar, type javascript.enabled.
• There you will find the option to enable or disable javascript by right-clicking on the value of that
opJon → select toggle.
• JavaScript in Chrome
• Here are the steps to turn on or turn off JavaScript in Chrome −
• Click the Chrome menu at the top right hand corner of your browser.
• Select Settings.
• Click Show advanced settings at the end of the page.
• Under the Privacy section, click the Content settings button.
• In the "Javascript" section, select "Do not allow any site to run JavaScript" or "Allow all sites to
run JavaScript (recommended)“.
16
Department of Computer Engineering www.sandipfoundation.org
JavaScript Example 04
<html>
<body>
<script type="text/javascript">
alert("Hello Javatpoint");
</script>
</body>
</html>
17
Department of Computer Engineering www.sandipfoundation.org
JavaScript Example 04
message.js
function msg()
{
alert("Hello Javatpoint");
}
19
Department of Computer Engineering www.sandipfoundation.org
JavaScript Example 04
• Let's include the JavaScript file into html page. It calls the JavaScript function on button click.
• index.html
<head>
<script type="text/javascript" src="message.js">
</script>
</head>
<body>
<p>Welcome to JavaScript</p>
<form>
<input type="button" value="click" onclick="msg()"/>
</form>
</body>
</html>
20
Department of Computer Engineering www.sandipfoundation.org
Object Name, Property, Method, Dot Syntax, Main Event 04
Obect Properties:
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.
• For example, a form object has a title, a width, and a height—to mention a few properties
Method:
• A method is a process performed by an object when it receives a message.
• For example, a Submit button on a form is an object. Its Submit label and the dimensions of the
button are properties of the button object.
• If you click the Submit button, the form is submitted to the server-side application. In other
words, clicking the Submit button causes the button to process a method.
21
Department of Computer Engineering www.sandipfoundation.org
Object Name, Property, Method, Dot Syntax, Main Event 04
Dot Syntax:
• Used to access properties and methods of object.
• You access an object’s properties and methods by using the dot syntax along with the
object name and its property or method.
• So, for example, here’s how you would
• identify the background color of a document and the write method for a document:
document.bgColor
document.write()
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.
22
Department of Computer Engineering www.sandipfoundation.org
Values and Variables 04
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.
23
Department of Computer Engineering www.sandipfoundation.org
Values and Variables 04
• Variable
• A statement that tells the browser to create a variable requires two parts:
• The special word, called a keyword, tells the browser that you want it to create a variable.
• The word you need to use to declare a variable is var.
• The variable name can consist of any letter, digit, and an underscore, but it cannot begin with a
digit.
<script type="text/javascript">
<!--
var money;
var name;
You can also declare multiple variables with the same var keyword as follows:
<script type="text/javascript">
<!--
var money, name;
//-->
</script>
24
Department of Computer Engineering www.sandipfoundation.org
Values and Variables 04
• Storing a value in a variable is called variable initialization. You can do variable initialization at
the time of variable creation or at a later point in time when you need that variable.
• For instance, you might create a variable named money and assign the value 2000.50 to it later.
For another variable, you can assign a value at the time of initialization as follows.
<script type="text/javascript">
<!--
var name = "Ali";
var money;
money = 2000.50;
//-->
</script>
25
Department of Computer Engineering www.sandipfoundation.org
Values and Variables 04
<html>
<head>
<title>Variables in Javascript</title>
</head>
<body>
<script type="text/javascript">
//Variables in JavaScript
var a,b,c;
var string;
a=2;
b=3;
c=a+b;
string="The result is :";
document.write("Performing addition of a and b"+"</br>");
document.write(string);
document.write(c);
</script>
</center>
</body>
</html> 26
Department of Computer Engineering www.sandipfoundation.org
04
Keywords
• These are the reserved words having some special meaning associated with it.
27
Department of Computer Engineering www.sandipfoundation.org
Operators and Expressions 04
<html> document.write(result);
<body> document.write(linebreak);
<script type = "text/javascript">
var a = 10; document.write("(a >= b) => ");
var b = 20; result = (a >= b);
var linebreak = "<br />"; document.write(result);
document.write(linebreak);
document.write("(a == b) => ");
result = (a == b); document.write("(a <= b) => ");
document.write(result); result = (a <= b);
document.write(linebreak); document.write(result);
document.write(linebreak);
document.write("(a < b) => "); </script>
result = (a < b); </body>
document.write(result); </html>
document.write(linebreak);
Output
document.write("(a > b) => "); (a == b) => false
result = (a > b); (a < b) => true
document.write(result); (a > b) => false
document.write(linebreak); (a != b) => true
(a >= b) => false
document.write("(a != b) => "); a <= b) => true
result = (a != b); 31
Department of Computer Engineering www.sandipfoundation.org
Operators and Expressions – Logical Operators 04
<html>
<body> document.write("!(a && b) => ");
<script type = "text/javascript"> result = (!(a && b));
<!-- document.write(result);
var a = true; document.write(linebreak);
var b = false; //-->
var linebreak = "<br />"; </script>
<p>Set the variables to different values
document.write("(a && b) => "); and different operators and then try...</p>
result = (a && b); </body>
document.write(result); </html>
document.write(linebreak);
Output
document.write("(a || b) => "); (a && b) => false
result = (a || b); (a || b) => true
document.write(result); !(a && b) => true
document.write(linebreak);
32
Department of Computer Engineering www.sandipfoundation.org
Operators and Expressions – Bitwise Operators 04
<html> document.write(linebreak);
<body> document.write("(a << b) => ");
<script type = "text/javascript"> result = (a << b);
var a = 2; // Bit presentation 10 document.write(result);
var b = 3; // Bit presentation 11 document.write(linebreak);
var linebreak = "<br />"; document.write("(a >> b) => ");
document.write("(a & b) => "); result = (a >> b);
result = (a & b); document.write(result);
document.write(result); document.write(linebreak);
document.write(linebreak); </script>
document.write("(a | b) => "); </body>
result = (a | b); </html>
document.write(result);
document.write(linebreak); Output:
document.write("(a ^ b) => "); (a & b) => 2
result = (a ^ b); (a | b) => 3
document.write(result); (a ^ b) => 1
document.write(linebreak); (~b) => -4
document.write("(~b) => "); (a << b) => 16
result = (~b); (a >> b) => 0
document.write(result);
33
Department of Computer Engineering www.sandipfoundation.org
Operators and Expressions – Comparison Operators 04
<html> document.write(result);
<body> document.write(linebreak);
<script type = "text/javascript">
var a = 33; document.write("Value of a => (a /= b) => ");
var b = 10; result = (a /= b);
var linebreak = "<br />"; document.write(result);
document.write("Value of a => (a = b) => "); document.write(linebreak);
result = (a = b);
document.write(result); document.write("Value of a => (a %= b) => ");
document.write(linebreak); result = (a %= b);
document.write(result);
document.write("Value of a => (a += b) => "); document.write(linebreak);
result = (a += b); </script>
document.write(result); </body>
document.write(linebreak); </html>
• The conditional operator first evaluates an expression for a true or false value and then executes one of the two given
statements depending upon the result of the evaluation.
<html>
<body>
<script type = "text/javascript">
var a = 10;
var b = 20;
var linebreak = "<br />";
document.write ("((a > b) ? 100 : 200) => ");
result = (a > b) ? 100 : 200;
document.write(result);
document.write(linebreak);
document.write ("((a < b) ? 100 : 200) => ");
result = (a < b) ? 100 : 200;
document.write(result);
document.write(linebreak);
</script>
</body>
</html>
Output
((a > b) ? 100 : 200) => 200
((a < b) ? 100 : 200) => 100
35
Department of Computer Engineering www.sandipfoundation.org
If Statement, if…else, if…elseif, nested if statement 04
• If statement }
• It evaluates the content only if expression is true. The • If...else if statement
signature of JavaScript if statement is given below. • It evaluates the content only if expression is true from
if(expression) several expressions. The signature of JavaScript if else if
{ statement is given below.
if(expression1)
//content to be evaluated {
} //content to be evaluated if expression1 is true
}
• If...else Statement else if(expression2)
• It evaluates the content whether condition is true of false. {
//content to be evaluated if expression2 is true
The syntax of JavaScript if-else statement is given below. }
if(expression) else if(expression3)
{ {
//content to be evaluated if condition is //content to be evaluated if expression3 is true
true }
Else
} {
Else //content to be evaluated if no expression is true
{
//content to be evaluated if condition is }
false
36
Department of Computer Engineering www.sandipfoundation.org
If Statement 04
<html>
<body>
<script type = "text/javascript">
<!--
var age = 20;
if( age > 18 )
{
document.write("<b>Qualifies for driving</b>");
}
//-->
</script>
<p>Set the variable to different value and then try...</p>
</body>
</html>
37
Department of Computer Engineering www.sandipfoundation.org
if…else statement 04
<html>
<body>
<script type = "text/javascript">
<!--
var age = 15;
38
Department of Computer Engineering www.sandipfoundation.org
if…else if.. statement 04
<html>
<body>
<script type = "text/javascript">
<!--
var book = "maths";
if( book == "history" ) {
document.write("<b>History Book</b>");
} else if( book == "maths" ) {
document.write("<b>Maths Book</b>");
} else if( book == "economics" ) {
document.write("<b>Economics Book</b>");
} else {
document.write("<b>Unknown Book</b>");
}
//-->
</script>
<p>Set the variable to different value and then try...</p>
</body>
<html>
39
Department of Computer Engineering www.sandipfoundation.org
Switch…case statement 04
• The objective of a switch statement is to give an expression to evaluate and several different statements
to execute based on the value of the expression.
• The interpreter checks each case against the value of the expression until a match is found. If nothing
matches, a default condition will be used.
switch (expression) {
case condition 1: statement(s)
break;
default: statement(s)
}
40
Department of Computer Engineering www.sandipfoundation.org
Switch…case statement 04
<html>
<body>
<script type = "text/javascript">
var grade = 'A';
document.write("Entering switch block<br />");
switch (grade) {
case 'A': document.write("Good job<br />");
break;
case 'B': document.write("Pretty good<br />");
break;
case 'C': document.write("Passed<br />");
break;
case 'D': document.write("Not so good<br />");
break;
case 'F': document.write("Failed<br />");
break;
default: document.write("Unknown grade<br />")
}
document.write("Exiting switch block");
</script>
</body>
41
</html> Department of Computer Engineering www.sandipfoundation.org
Loop statement – for loop 04
• The JavaScript loops are used to iterate the piece • The syntax of for loop is given below.
of code using for, while, do while or for-in loops. for (initialization; condition; increment)
• It makes the code compact. It is mostly used in {
array. code to be executed
}
There are four types of loops in JavaScript.
1.for loop <html>
2.while loop <body>
3.do-while loop <script>
4.for-in loop for (i=1; i<=5; i++)
{
document.write(i + "<br/>")
1) JavaScript For loop }
• The JavaScript for loop iterates the elements for </script>
the fixed number of times. </body>
• It should be used if number of iteration is </html>
known.
Department of Computer Engineering www.sandipfoundation.org
Loop statement – while loop 04
43
Department of Computer Engineering www.sandipfoundation.org
Loop statement – do…while loop 04
44
Department of Computer Engineering www.sandipfoundation.org
Loop statement – Break statement 04
Syntax:
jump-statement;
break;
Ex:
for (i = 0; i < 10; i++)
{
if (i === 3)
{ break; }
text += "The number is " + i + "<br>";
}
45
Department of Computer Engineering www.sandipfoundation.org
Loop statement – Continue statement 04
• 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>";
}
46
Department of Computer Engineering www.sandipfoundation.org
19
Presented By
Prof. Vishal B. Ohol
Experience : 10 Years
ME(Software Engg)
47
Department of Computer Engineering www.sandipfoundation.org