SlideShare a Scribd company logo
UNIT – III
JAVA SCRIPT
Arulkumar V P
Introduction: Scripting
• Scripting: Scripting is an easy way of automating the process
which is needed to be done step-by-step by a user.
• Scripting languages: do not require the compilation step and
are rather interpreted. For example, normally, a C program needs
to be compiled before running whereas normally, a scripting
language like JavaScript or PHP need not be compiled.
• Applications of Scripting Languages :
1.To automate certain tasks in a program
2.Extracting information from a data set
3.Less code intensive as compared to traditional programming
languages
Introduction: Types of Scripting
• Client scripting can be defined as a code that is present in
a client’s HTML page.
• It is usually attached to the browser in a language that is
compatible with the browser.
• The browser then downloads that code temporarily and
processes it without the server. If additional information is
required, a request is raised and sent to the server.
• Client-side programming languages are :
1) Javascript
2) VBScript
3) HTML
4) CSS
5) AJAX
6) jQuery etc.
Introduction: Types of Scripting
• Server-side scripting is a method of designing websites
so that the process or user request is run on the
originating server.
• Server-side scripts provide an interface to the user and
limit access to proprietary data and help keep control of
the script source code.
• which produce a response customized for each user's
(client's) request to the website.
• Server-side programming languages are :
ASP .NET, PHP, C++, Java and JSP, Python, Ruby on Rails, R
Introduction: Types of Scripting
CLIENT SIDE SCRIPTING SERVER SIDE SCRIPTING
Source code is visible to user. Source code is not visible to user
because its output of server side is
a HTML page.
It usually depends on browser
and it’s version.
In this any server side technology
can be use and it does not depend
on client.
It runs on user’s computer. It runs on web server.
There are many advantages link
with this like faster. response
times, a more interactive
application.
The primary advantage is its ability
to highly customize, response
requirements, access rights based
on user.
Introduction: Types of Scripting
CLIENT SIDE SCRIPTING SERVER SIDE SCRIPTING
It does not provide security for
data.
It provides more security for data.
It is a technique use in web
development in which scripts
runs on clients browser.
It is a technique that uses scripts
on web server to produce a
response that is customized for
each clients request.
HTML, CSS, javascript and
VBScript
ASP, PHP, Python, Java and Ruby
Introduction: JavaScript
• JavaScript is the most popular scripting language on the
internet, and works in all major browsers, such as Internet
Explorer, Firefox, Chrome, Opera, and Safari.
• What is JavaScript?
• JavaScript is a scripting language
• JavaScript is usually embedded directly into HTML pages
• JavaScript was designed to add interactivity to HTML pages
• JavaScript is an interpreted language (means that scripts
execute without preliminary compilation)
Introduction: JavaScript
• Everyone can use JavaScript without purchasing a license.
• JavaScript is Netscape's cross-platform, object-oriented
scripting language.
• Core JavaScript contains a core set of objects, such as
Array, Date, and Math, and
• A core set of language elements such as operators,
control structures, and statements. Core JavaScript can
be extended for a variety of purposes by supplementing it
with additional objects.
Introduction: JavaScript
What can a JavaScript do? (Applications)
• JavaScript gives HTML designers a programming tool
HTML authors are normally not programmers, but JavaScript
is a scripting language with a very simple syntax! Almost
anyone can put small "snippets" of code into their HTML
pages
• JavaScript can put dynamic text into an HTML page - A
JavaScript statement like this:
document.write("<h1>" + name + "</h1>") can write a
variable text into an HTML page
• JavaScript can react to events - A JavaScript can be set to
execute when something happens, like when a page has
finished loading or when a user clicks on an HTML element.
Introduction: JavaScript
What can a JavaScript do?
• JavaScript can be used to validate data - A JavaScript
can be used to validate form data before it is submitted
to a server. This saves the server from extra processing
• JavaScript can be used to create cookies - A JavaScript
can be used to store and retrieve information on the
visitor's computer
• JavaScript can read and write HTML elements - A
JavaScript can read and change the content of an HTML
element
JavaScript: Placement in HTML File
In HTML, JavaScript code is inserted between
<script> . . . </script> tag
The most preferred ways to include JavaScript in an
HTML file are
1. Script in <head>...</head> section.
2. Script in <body>...</body> section.
3. Script in <body>...</body> and <head>...</head>
sections.
4. Script in an external file and then include in
<head>...</head> section.
JavaScript: <script> tag
JavaScript programs can be inserted into any part of an
HTML document with the help of the <script> tag, which is
automatically executed when the browser processes the tag.
The <script> tag has a few attributes that are
The type attribute: <script type=…>
he language attribute: <script language=…>
<script type="text/javascript">
. . . .
</script>
Script files are attached to HTML with the src attribute:
<script src="/path/to/script.js"> . . . </script>
JavaScript: <script> tag
A single <script> tag can't have both the src attribute
and code inside.
<script src="file.js"></script> <script>
alert("HELLO");
</script>
<script src="file.js">
alert("HELLO"); // the content is ignored, because src
is set </script>
x

JavaScript: <script> in <head>
• If you want to have a script run on some event, such as when a
user clicks somewhere
• Scripts to be executed when they are called, or when an event
is triggered, go in the head section.
• If you place a script in the head section, you will ensure that
the script is loaded before anyone uses it.
<html>
<head>
<script type = "text/javascript">
alert("Hello World")
</script>
</head>
<body>
<h2> Alert Completed </h2>
</body>
</html>
JavaScript: <script> in <body>
If we want to display/generate content in html on page
load, we can place <script> inside the <body>:
<html>
<head>
</head>
<body>
<script type = "text/javascript">
document.write("Hello World")
</script>
<p>This is web page body </p>
</body>
</html>
JavaScript: <script> in <head> & <body>
You can place an unlimited number of scripts in your
document, so you can have scripts in both the body and the head
section, the <script> in <head> will be loaded first.
<html>
<head>
<script type="text/javascript">
....
</script>
</head>
<body>
<script type="text/javascript">
....
</script>
</body>
</html>
JavaScript: <script> in External file (.js)
• JavaScript files have the file extension .js
• External JavaScript file (.js) can be referenced using
<script src="/PathToScriptFile.js"></script>
 src attribute is used to specify the full path of .js file.
• You can place an external script reference in <head> or
<body> as you like.
• External scripts cannot contain <script> tags.
• Placing scripts in external files has some advantages:
• It separates HTML and code
• It makes HTML and JavaScript easier to read and maintain
• Cached JavaScript files can speed up page loads
JavaScript: Code Structure
• Statements are syntax constructs and commands that perform
actions.
• We can have as many statements in our code as we want.
Statements can be separated with a semicolon.
• The semicolon is optional (according to the JavaScript
standard), and the browser is supposed to interpret the end of
the line as the end of the statement.
• A semicolon may be omitted in most cases when a line break
exists.
var v= 3 +
4 + 4;
If the line ends with a plus "+", then it is an "incomplete
expression", so the semicolon is not required. And in this case
that works as intended.
JavaScript: Code Structure
Note: Using semicolons makes it possible to write multiple
statements on one line.
Ex:
var name = "Jeeva"
var mark = 89
(or)
var name = "Jeeva"; var mark = 89
JavaScript is Case Sensitive
Unlike HTML, JavaScript is case sensitive - therefore
watch your capitalization closely when you write JavaScript
statements, create or call variables, objects and functions.
Ex: num, Num are different variables.
JavaScript: Code Structure
JavaScript Code:
JavaScript code (or just JavaScript) is a sequence of JavaScript
statements.
Each statement is executed by the browser in the sequence they are
written.
This example will write a heading and two paragraphs to a web
page:
Example:
<script type="text/javascript">
document.write("<h1>This is a heading</h1>");
document.write("<p>This is a paragraph.</p>");
document.write("<p>This is another paragraph.</p>");
</script>
JavaScript: Code Structure
JavaScript Blocks
• JavaScript statements can be grouped together in blocks.
• Blocks start with a left curly bracket { ,
• and ends with a right curly bracket } .
• The purpose of a block is to make the sequence of statements
execute together.
Example
<script type="text/javascript">
{
document.write("<h1>This is a heading</h1>");
document.write("<p>This is a paragraph.</p>");
document.write("<p>This is another paragraph.</p>");
}
</script>
JavaScript: Comments
Single line comments:
Comments can be added to explain the JavaScript, or to make the
code more readable.
Single line comments start with //.
JavaScript Multi-Line Comments
Multi line comments start with /* and end with */.
The following example uses a multi line comment to explain the
code:
Using Comments at the End of a Line
In the following example the comment is placed at the end of a
code line.
document.write("Hello"); // Write "Hello“
JavaScript: Variables
Variables as symbolic names for values in your application. You
give variables names by which used to hold values or expressions.
We refer to them and which must conform to certain rules.
A variable can have a short name, like x, or a more descriptive name,
like carname.
• A JavaScript identifier, or name, must start with a letter or
underscore ("_"),
• Subsequent characters can also be digits (0-9).
• Because JavaScript is case sensitive, letters include the characters
"A" through "Z" (uppercase) and the characters "a" through "z"
(lowercase).
• Identifier name must not contain any white-space.
• legal names: Number_hits, temp99, and _name
• illegal names: 9temp, Number hits,
JavaScript: Variables
Declaring (Creating) JavaScript Variables
Creating variables in JavaScript is most often referred to as
"declaring" variables.
We can declare a variable in two ways:
• By simply assigning its a value. For example, x = 42
• With the keyword var. For example, var x = 42
var x;
var carname;
After the declaration shown above, the variables are empty (they
have no values yet).
However, you can also assign values to the variables when you
declare them:
var x=5;
var carname="Volvo";
JavaScript: Variables
Assigning Values to Undeclared JavaScript Variables:
If you assign values to variables that have not yet been declared,
the variables will automatically be declared.
These statements:
x=5;
carname="Volvo";
Redeclaring JavaScript Variables:
If you redeclare a JavaScript variable, it will not lose its original
value.
var x=5;
var x;
After the execution of the statements above, the variable x will still
have the value of 5. The value of x is not reset (or cleared) when
you redeclare it.
JavaScript: Variables Scope
When we set a variable identifier by assignment outside of a
function, it is called a global variable,
because it is available everywhere in the current document. When
we declare a variable within a
function, it is called a local variable, because it is available only
within the function.
let keyword used for variable declaration in JavaScript like var but
the difference between them is that var is function scoped(global)
and let is block scoped. Example:
var a = 10;
var a = 20; //a is replaced
let a = 10;
let a = 20; //SyntaxError: //Identifier
'a' has already been declared
JavaScript: Data Types
JavaScript: Operators
JavaScript operators are symbols that are used to perform
operations on operands, types of operators in JavaScript
• Arithmetic Operators
• Comparison (Relational) Operators
• Assignment Operators
• Bitwise Operators
• Logical Operators
• Special Operators
JavaScript: Arithmetic Operators
Operator Description Example
+ Addition 10+20 = 30
- Subtraction 20-10 = 10
* Multiplication 10*20 = 200
/ Division 20/10 = 2
%
Modulus
(Remainder)
20%10 = 0
++ Increment var a=10; a++; Now a = 11
-- Decrement var a=10; a--; Now a = 9
JavaScript: Comparison Operators
Operator Description Example
== Is equal to 10==20 => false
===
Identical
(equal and of same type)
10===20 => false
10 === "10" => false
!= Not equal to 10!=20 => true
!== Not Identical 20!==20 => false
> Greater than 20>10 => true
>= Greater than or equal to 20>=10 => true
< Less than 20<10 => false
<= Less than or equal to 20<=10 => false
JavaScript: Assignment Operators
Operator Description Example
= Assign 10+10 = 20
+= Add and assign var a=10; a+=20; Now a = 30
-= Subtract and assign var a=20; a-=10; Now a = 10
*= Multiply and assign var a=10; a*=20; Now a = 200
/= Divide and assign var a=10; a/=2; Now a = 5
%= Modulus and assign var a=10; a%=2; Now a = 0
JavaScript: Logical Operators
Operator Description Example
&& Logical AND (10==20 && 20==33)
=> false
|| Logical OR (10==20 || 20==33)
=> false
! Logical Not !(10==20) => true
Logical operators are used to determine the logic between
variables or values. An expression containing logical
operator returns either 0 or 1 depending upon whether
expression results true or false.
JavaScript: Bitwise Operators
The bitwise operators perform bitwise operations on
operands.
Operator Description Example
& Bitwise AND (10==20 & 20==33) = false
| Bitwise OR (10==20 | 20==33) = false
^ Bitwise XOR (10==20 ^ 20==33) = false
~ Bitwise NOT (~10) = -10
<< Bitwise Left Shift (10<<2) = 40
>> Bitwise Right Shift (10>>2) = 2
>>>
Bitwise Right Shift
with Zero
(10>>>2) = 2
JavaScript: Bitwise Operators
The bitwise operators perform bitwise operations on
operands. Example:
12 = 00001100 (In Binary)
25 = 00011001 (In Binary)
Bit Operation of 12 and 25
00001100 ------>12
& 00011001 ------>25
00001000 ==> 8 (In decimal)

More Related Content

What's hot (20)

PPTX
Improving web site performance and scalability while saving
mdc11
 
PPT
Web Fundamentals
arunv
 
PPT
Ui perf
Franz Allan See
 
PDF
Client-side Website Optimization
Radu Pintilie
 
PPT
Web performance Talk
Prasoon Agrawal
 
PPTX
Optimizing Client-Side Performance
andrew4web
 
PPTX
Website/Web Applications / Static vs Dynamic Website / Web Browser /
Sachin Yadav
 
PDF
Skalowalna architektura na przykładzie soccerway.com
Spodek 2.0
 
PPTX
Difference between reseller hosting and dedicated web servers
HTS Hosting
 
PPTX
Caching in Drupal 8
valuebound
 
PDF
Reliable dedicated server hosting provider
Go4hosting Web Hosting Provider
 
PPTX
Web Hosting Solutions: Shared Hosting and VPS Hosting
HTS Hosting
 
PPTX
Differences between Reseller Hosting, Dedicated Hosting & Shared Hosting
HTS Hosting
 
PPTX
Apache Multiview Vulnerability
Ronan Dunne, CEH, SSCP
 
PDF
Simple server side cache for Express.js with Node.js
Gokusen Newz
 
PPT
Web servers
webhostingguy
 
PPTX
Web hosting
audace82
 
PDF
web hosting
Thush madu
 
PPTX
What is Server? (Web Server vs Application Server)
Amit Nirala
 
PPTX
Posting Images using Android
Ali Muzaffar
 
Improving web site performance and scalability while saving
mdc11
 
Web Fundamentals
arunv
 
Client-side Website Optimization
Radu Pintilie
 
Web performance Talk
Prasoon Agrawal
 
Optimizing Client-Side Performance
andrew4web
 
Website/Web Applications / Static vs Dynamic Website / Web Browser /
Sachin Yadav
 
Skalowalna architektura na przykładzie soccerway.com
Spodek 2.0
 
Difference between reseller hosting and dedicated web servers
HTS Hosting
 
Caching in Drupal 8
valuebound
 
Reliable dedicated server hosting provider
Go4hosting Web Hosting Provider
 
Web Hosting Solutions: Shared Hosting and VPS Hosting
HTS Hosting
 
Differences between Reseller Hosting, Dedicated Hosting & Shared Hosting
HTS Hosting
 
Apache Multiview Vulnerability
Ronan Dunne, CEH, SSCP
 
Simple server side cache for Express.js with Node.js
Gokusen Newz
 
Web servers
webhostingguy
 
Web hosting
audace82
 
web hosting
Thush madu
 
What is Server? (Web Server vs Application Server)
Amit Nirala
 
Posting Images using Android
Ali Muzaffar
 

Similar to JS BASICS JAVA SCRIPT SCRIPTING (20)

DOC
Basics java scripts
ch samaram
 
DOC
Java script by Act Academy
actanimation
 
PPT
JAVA SCRIPT
Go4Guru
 
DOC
2javascript web programming with JAVA script
umardanjumamaiwada
 
PDF
CS8651- Unit 2 - JS.internet programming paper anna university -2017 regulation
amrashbhanuabdul
 
PPTX
Unit 4 Java script.pptx
Gangesh8
 
PDF
WEB PROGRAMMING UNIT II BY BHAVSINGH MALOTH
Bhavsingh Maloth
 
PDF
Web programming UNIT II by Bhavsingh Maloth
Bhavsingh Maloth
 
PPT
Introduction to JavaScript
Andres Baravalle
 
PPTX
Java script Basic
Jaya Kumari
 
PPTX
JavaScript_III.pptx
rashmiisrani1
 
PPTX
HNDIT1022 Week 08, 09 10 Theory web .pptx
IsuriUmayangana
 
PDF
Unit 4(it workshop)
Dr.Lokesh Gagnani
 
PPTX
Client side scripting using Javascript
Bansari Shah
 
PPT
Java script
sanjay joshi
 
PPT
Java script
umesh patil
 
PPTX
Lecture-15.pptx
vishal choudhary
 
PPTX
CHAPTER 3 JS (1).pptx
achutachut
 
PPT
basics of javascript and fundamentals ppt
MaanKansagra
 
PPT
Javascript overview and introduction to js
mohammedarshadhussai4
 
Basics java scripts
ch samaram
 
Java script by Act Academy
actanimation
 
JAVA SCRIPT
Go4Guru
 
2javascript web programming with JAVA script
umardanjumamaiwada
 
CS8651- Unit 2 - JS.internet programming paper anna university -2017 regulation
amrashbhanuabdul
 
Unit 4 Java script.pptx
Gangesh8
 
WEB PROGRAMMING UNIT II BY BHAVSINGH MALOTH
Bhavsingh Maloth
 
Web programming UNIT II by Bhavsingh Maloth
Bhavsingh Maloth
 
Introduction to JavaScript
Andres Baravalle
 
Java script Basic
Jaya Kumari
 
JavaScript_III.pptx
rashmiisrani1
 
HNDIT1022 Week 08, 09 10 Theory web .pptx
IsuriUmayangana
 
Unit 4(it workshop)
Dr.Lokesh Gagnani
 
Client side scripting using Javascript
Bansari Shah
 
Java script
sanjay joshi
 
Java script
umesh patil
 
Lecture-15.pptx
vishal choudhary
 
CHAPTER 3 JS (1).pptx
achutachut
 
basics of javascript and fundamentals ppt
MaanKansagra
 
Javascript overview and introduction to js
mohammedarshadhussai4
 
Ad

Recently uploaded (20)

PDF
NFPA 10 - Estandar para extintores de incendios portatiles (ed.22 ENG).pdf
Oscar Orozco
 
PDF
13th International Conference of Security, Privacy and Trust Management (SPTM...
ijcisjournal
 
PDF
輪読会資料_Miipher and Miipher2 .
NABLAS株式会社
 
PPTX
Kel.3_A_Review_on_Internet_of_Things_for_Defense_v3.pptx
Endang Saefullah
 
PDF
A Brief Introduction About Robert Paul Hardee
Robert Paul Hardee
 
DOCX
Engineering Geology Field Report to Malekhu .docx
justprashant567
 
PDF
Bayesian Learning - Naive Bayes Algorithm
Sharmila Chidaravalli
 
PDF
Designing for Tomorrow – Architecture’s Role in the Sustainability Movement
BIM Services
 
PPTX
Alan Turing - life and importance for all of us now
Pedro Concejero
 
PPTX
Introduction to File Transfer Protocol with commands in FTP
BeulahS2
 
PPTX
Unit_I Functional Units, Instruction Sets.pptx
logaprakash9
 
PPSX
OOPS Concepts in Python and Exception Handling
Dr. A. B. Shinde
 
PPTX
Functions in Python Programming Language
BeulahS2
 
PPTX
Comparison of Flexible and Rigid Pavements in Bangladesh
Arifur Rahman
 
PDF
FSE-Journal-First-Automated code editing with search-generate-modify.pdf
cl144
 
PDF
MODULE-5 notes [BCG402-CG&V] PART-B.pdf
Alvas Institute of Engineering and technology, Moodabidri
 
PPTX
Engineering Quiz ShowEngineering Quiz Show
CalvinLabial
 
PDF
Tesia Dobrydnia - An Avid Hiker And Backpacker
Tesia Dobrydnia
 
PDF
Plant Control_EST_85520-01_en_AllChanges_20220127.pdf
DarshanaChathuranga4
 
PPTX
Diabetes diabetes diabetes diabetes jsnsmxndm
130SaniyaAbduNasir
 
NFPA 10 - Estandar para extintores de incendios portatiles (ed.22 ENG).pdf
Oscar Orozco
 
13th International Conference of Security, Privacy and Trust Management (SPTM...
ijcisjournal
 
輪読会資料_Miipher and Miipher2 .
NABLAS株式会社
 
Kel.3_A_Review_on_Internet_of_Things_for_Defense_v3.pptx
Endang Saefullah
 
A Brief Introduction About Robert Paul Hardee
Robert Paul Hardee
 
Engineering Geology Field Report to Malekhu .docx
justprashant567
 
Bayesian Learning - Naive Bayes Algorithm
Sharmila Chidaravalli
 
Designing for Tomorrow – Architecture’s Role in the Sustainability Movement
BIM Services
 
Alan Turing - life and importance for all of us now
Pedro Concejero
 
Introduction to File Transfer Protocol with commands in FTP
BeulahS2
 
Unit_I Functional Units, Instruction Sets.pptx
logaprakash9
 
OOPS Concepts in Python and Exception Handling
Dr. A. B. Shinde
 
Functions in Python Programming Language
BeulahS2
 
Comparison of Flexible and Rigid Pavements in Bangladesh
Arifur Rahman
 
FSE-Journal-First-Automated code editing with search-generate-modify.pdf
cl144
 
MODULE-5 notes [BCG402-CG&V] PART-B.pdf
Alvas Institute of Engineering and technology, Moodabidri
 
Engineering Quiz ShowEngineering Quiz Show
CalvinLabial
 
Tesia Dobrydnia - An Avid Hiker And Backpacker
Tesia Dobrydnia
 
Plant Control_EST_85520-01_en_AllChanges_20220127.pdf
DarshanaChathuranga4
 
Diabetes diabetes diabetes diabetes jsnsmxndm
130SaniyaAbduNasir
 
Ad

JS BASICS JAVA SCRIPT SCRIPTING

  • 1. UNIT – III JAVA SCRIPT Arulkumar V P
  • 2. Introduction: Scripting • Scripting: Scripting is an easy way of automating the process which is needed to be done step-by-step by a user. • Scripting languages: do not require the compilation step and are rather interpreted. For example, normally, a C program needs to be compiled before running whereas normally, a scripting language like JavaScript or PHP need not be compiled. • Applications of Scripting Languages : 1.To automate certain tasks in a program 2.Extracting information from a data set 3.Less code intensive as compared to traditional programming languages
  • 3. Introduction: Types of Scripting • Client scripting can be defined as a code that is present in a client’s HTML page. • It is usually attached to the browser in a language that is compatible with the browser. • The browser then downloads that code temporarily and processes it without the server. If additional information is required, a request is raised and sent to the server. • Client-side programming languages are : 1) Javascript 2) VBScript 3) HTML 4) CSS 5) AJAX 6) jQuery etc.
  • 4. Introduction: Types of Scripting • Server-side scripting is a method of designing websites so that the process or user request is run on the originating server. • Server-side scripts provide an interface to the user and limit access to proprietary data and help keep control of the script source code. • which produce a response customized for each user's (client's) request to the website. • Server-side programming languages are : ASP .NET, PHP, C++, Java and JSP, Python, Ruby on Rails, R
  • 5. Introduction: Types of Scripting CLIENT SIDE SCRIPTING SERVER SIDE SCRIPTING Source code is visible to user. Source code is not visible to user because its output of server side is a HTML page. It usually depends on browser and it’s version. In this any server side technology can be use and it does not depend on client. It runs on user’s computer. It runs on web server. There are many advantages link with this like faster. response times, a more interactive application. The primary advantage is its ability to highly customize, response requirements, access rights based on user.
  • 6. Introduction: Types of Scripting CLIENT SIDE SCRIPTING SERVER SIDE SCRIPTING It does not provide security for data. It provides more security for data. It is a technique use in web development in which scripts runs on clients browser. It is a technique that uses scripts on web server to produce a response that is customized for each clients request. HTML, CSS, javascript and VBScript ASP, PHP, Python, Java and Ruby
  • 7. Introduction: JavaScript • JavaScript is the most popular scripting language on the internet, and works in all major browsers, such as Internet Explorer, Firefox, Chrome, Opera, and Safari. • What is JavaScript? • JavaScript is a scripting language • JavaScript is usually embedded directly into HTML pages • JavaScript was designed to add interactivity to HTML pages • JavaScript is an interpreted language (means that scripts execute without preliminary compilation)
  • 8. Introduction: JavaScript • Everyone can use JavaScript without purchasing a license. • JavaScript is Netscape's cross-platform, object-oriented scripting language. • Core JavaScript contains a core set of objects, such as Array, Date, and Math, and • A core set of language elements such as operators, control structures, and statements. Core JavaScript can be extended for a variety of purposes by supplementing it with additional objects.
  • 9. Introduction: JavaScript What can a JavaScript do? (Applications) • JavaScript gives HTML designers a programming tool HTML authors are normally not programmers, but JavaScript is a scripting language with a very simple syntax! Almost anyone can put small "snippets" of code into their HTML pages • JavaScript can put dynamic text into an HTML page - A JavaScript statement like this: document.write("<h1>" + name + "</h1>") can write a variable text into an HTML page • JavaScript can react to events - A JavaScript can be set to execute when something happens, like when a page has finished loading or when a user clicks on an HTML element.
  • 10. Introduction: JavaScript What can a JavaScript do? • JavaScript can be used to validate data - A JavaScript can be used to validate form data before it is submitted to a server. This saves the server from extra processing • JavaScript can be used to create cookies - A JavaScript can be used to store and retrieve information on the visitor's computer • JavaScript can read and write HTML elements - A JavaScript can read and change the content of an HTML element
  • 11. JavaScript: Placement in HTML File In HTML, JavaScript code is inserted between <script> . . . </script> tag The most preferred ways to include JavaScript in an HTML file are 1. Script in <head>...</head> section. 2. Script in <body>...</body> section. 3. Script in <body>...</body> and <head>...</head> sections. 4. Script in an external file and then include in <head>...</head> section.
  • 12. JavaScript: <script> tag JavaScript programs can be inserted into any part of an HTML document with the help of the <script> tag, which is automatically executed when the browser processes the tag. The <script> tag has a few attributes that are The type attribute: <script type=…> he language attribute: <script language=…> <script type="text/javascript"> . . . . </script> Script files are attached to HTML with the src attribute: <script src="/path/to/script.js"> . . . </script>
  • 13. JavaScript: <script> tag A single <script> tag can't have both the src attribute and code inside. <script src="file.js"></script> <script> alert("HELLO"); </script> <script src="file.js"> alert("HELLO"); // the content is ignored, because src is set </script> x 
  • 14. JavaScript: <script> in <head> • If you want to have a script run on some event, such as when a user clicks somewhere • Scripts to be executed when they are called, or when an event is triggered, go in the head section. • If you place a script in the head section, you will ensure that the script is loaded before anyone uses it. <html> <head> <script type = "text/javascript"> alert("Hello World") </script> </head> <body> <h2> Alert Completed </h2> </body> </html>
  • 15. JavaScript: <script> in <body> If we want to display/generate content in html on page load, we can place <script> inside the <body>: <html> <head> </head> <body> <script type = "text/javascript"> document.write("Hello World") </script> <p>This is web page body </p> </body> </html>
  • 16. JavaScript: <script> in <head> & <body> You can place an unlimited number of scripts in your document, so you can have scripts in both the body and the head section, the <script> in <head> will be loaded first. <html> <head> <script type="text/javascript"> .... </script> </head> <body> <script type="text/javascript"> .... </script> </body> </html>
  • 17. JavaScript: <script> in External file (.js) • JavaScript files have the file extension .js • External JavaScript file (.js) can be referenced using <script src="/PathToScriptFile.js"></script>  src attribute is used to specify the full path of .js file. • You can place an external script reference in <head> or <body> as you like. • External scripts cannot contain <script> tags. • Placing scripts in external files has some advantages: • It separates HTML and code • It makes HTML and JavaScript easier to read and maintain • Cached JavaScript files can speed up page loads
  • 18. JavaScript: Code Structure • Statements are syntax constructs and commands that perform actions. • We can have as many statements in our code as we want. Statements can be separated with a semicolon. • The semicolon is optional (according to the JavaScript standard), and the browser is supposed to interpret the end of the line as the end of the statement. • A semicolon may be omitted in most cases when a line break exists. var v= 3 + 4 + 4; If the line ends with a plus "+", then it is an "incomplete expression", so the semicolon is not required. And in this case that works as intended.
  • 19. JavaScript: Code Structure Note: Using semicolons makes it possible to write multiple statements on one line. Ex: var name = "Jeeva" var mark = 89 (or) var name = "Jeeva"; var mark = 89 JavaScript is Case Sensitive Unlike HTML, JavaScript is case sensitive - therefore watch your capitalization closely when you write JavaScript statements, create or call variables, objects and functions. Ex: num, Num are different variables.
  • 20. JavaScript: Code Structure JavaScript Code: JavaScript code (or just JavaScript) is a sequence of JavaScript statements. Each statement is executed by the browser in the sequence they are written. This example will write a heading and two paragraphs to a web page: Example: <script type="text/javascript"> document.write("<h1>This is a heading</h1>"); document.write("<p>This is a paragraph.</p>"); document.write("<p>This is another paragraph.</p>"); </script>
  • 21. JavaScript: Code Structure JavaScript Blocks • JavaScript statements can be grouped together in blocks. • Blocks start with a left curly bracket { , • and ends with a right curly bracket } . • The purpose of a block is to make the sequence of statements execute together. Example <script type="text/javascript"> { document.write("<h1>This is a heading</h1>"); document.write("<p>This is a paragraph.</p>"); document.write("<p>This is another paragraph.</p>"); } </script>
  • 22. JavaScript: Comments Single line comments: Comments can be added to explain the JavaScript, or to make the code more readable. Single line comments start with //. JavaScript Multi-Line Comments Multi line comments start with /* and end with */. The following example uses a multi line comment to explain the code: Using Comments at the End of a Line In the following example the comment is placed at the end of a code line. document.write("Hello"); // Write "Hello“
  • 23. JavaScript: Variables Variables as symbolic names for values in your application. You give variables names by which used to hold values or expressions. We refer to them and which must conform to certain rules. A variable can have a short name, like x, or a more descriptive name, like carname. • A JavaScript identifier, or name, must start with a letter or underscore ("_"), • Subsequent characters can also be digits (0-9). • Because JavaScript is case sensitive, letters include the characters "A" through "Z" (uppercase) and the characters "a" through "z" (lowercase). • Identifier name must not contain any white-space. • legal names: Number_hits, temp99, and _name • illegal names: 9temp, Number hits,
  • 24. JavaScript: Variables Declaring (Creating) JavaScript Variables Creating variables in JavaScript is most often referred to as "declaring" variables. We can declare a variable in two ways: • By simply assigning its a value. For example, x = 42 • With the keyword var. For example, var x = 42 var x; var carname; After the declaration shown above, the variables are empty (they have no values yet). However, you can also assign values to the variables when you declare them: var x=5; var carname="Volvo";
  • 25. JavaScript: Variables Assigning Values to Undeclared JavaScript Variables: If you assign values to variables that have not yet been declared, the variables will automatically be declared. These statements: x=5; carname="Volvo"; Redeclaring JavaScript Variables: If you redeclare a JavaScript variable, it will not lose its original value. var x=5; var x; After the execution of the statements above, the variable x will still have the value of 5. The value of x is not reset (or cleared) when you redeclare it.
  • 26. JavaScript: Variables Scope When we set a variable identifier by assignment outside of a function, it is called a global variable, because it is available everywhere in the current document. When we declare a variable within a function, it is called a local variable, because it is available only within the function. let keyword used for variable declaration in JavaScript like var but the difference between them is that var is function scoped(global) and let is block scoped. Example: var a = 10; var a = 20; //a is replaced let a = 10; let a = 20; //SyntaxError: //Identifier 'a' has already been declared
  • 28. JavaScript: Operators JavaScript operators are symbols that are used to perform operations on operands, types of operators in JavaScript • Arithmetic Operators • Comparison (Relational) Operators • Assignment Operators • Bitwise Operators • Logical Operators • Special Operators
  • 29. JavaScript: Arithmetic Operators Operator Description Example + Addition 10+20 = 30 - Subtraction 20-10 = 10 * Multiplication 10*20 = 200 / Division 20/10 = 2 % Modulus (Remainder) 20%10 = 0 ++ Increment var a=10; a++; Now a = 11 -- Decrement var a=10; a--; Now a = 9
  • 30. JavaScript: Comparison Operators Operator Description Example == Is equal to 10==20 => false === Identical (equal and of same type) 10===20 => false 10 === "10" => false != Not equal to 10!=20 => true !== Not Identical 20!==20 => false > Greater than 20>10 => true >= Greater than or equal to 20>=10 => true < Less than 20<10 => false <= Less than or equal to 20<=10 => false
  • 31. JavaScript: Assignment Operators Operator Description Example = Assign 10+10 = 20 += Add and assign var a=10; a+=20; Now a = 30 -= Subtract and assign var a=20; a-=10; Now a = 10 *= Multiply and assign var a=10; a*=20; Now a = 200 /= Divide and assign var a=10; a/=2; Now a = 5 %= Modulus and assign var a=10; a%=2; Now a = 0
  • 32. JavaScript: Logical Operators Operator Description Example && Logical AND (10==20 && 20==33) => false || Logical OR (10==20 || 20==33) => false ! Logical Not !(10==20) => true Logical operators are used to determine the logic between variables or values. An expression containing logical operator returns either 0 or 1 depending upon whether expression results true or false.
  • 33. JavaScript: Bitwise Operators The bitwise operators perform bitwise operations on operands. Operator Description Example & Bitwise AND (10==20 & 20==33) = false | Bitwise OR (10==20 | 20==33) = false ^ Bitwise XOR (10==20 ^ 20==33) = false ~ Bitwise NOT (~10) = -10 << Bitwise Left Shift (10<<2) = 40 >> Bitwise Right Shift (10>>2) = 2 >>> Bitwise Right Shift with Zero (10>>>2) = 2
  • 34. JavaScript: Bitwise Operators The bitwise operators perform bitwise operations on operands. Example: 12 = 00001100 (In Binary) 25 = 00011001 (In Binary) Bit Operation of 12 and 25 00001100 ------>12 & 00011001 ------>25 00001000 ==> 8 (In decimal)