0% found this document useful (0 votes)
94 views39 pages

XML Is A Mark-Up Language Much Like HTML.: XML Is Designed To Store and Transport Data

XML stands for Extensible Markup Language and is used to store and transport data. It is designed to carry data, not display it. XML tags are not predefined like HTML, and must be defined by the user. XML is platform and language independent. It is commonly used to transfer data between applications and for storing data.

Uploaded by

Ganesh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
94 views39 pages

XML Is A Mark-Up Language Much Like HTML.: XML Is Designed To Store and Transport Data

XML stands for Extensible Markup Language and is used to store and transport data. It is designed to carry data, not display it. XML tags are not predefined like HTML, and must be defined by the user. XML is platform and language independent. It is commonly used to transfer data between applications and for storing data.

Uploaded by

Ganesh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 39

XML

XML Stands for Extensible Markup Language


XML is a mark-up language much like HTML.
XML is designed to store and transport data.
XML was designed to carry data, not to display data.
XML tags are not predefined. You must define your own tags.
XML is platform independent and language Independent (java, python, .net).
Before you start to learn XML, you should know basics of HTML & JavaScript.

Ex: Facebook, Gmail, WhatsApp etc.

*HTML used to display data; XML is used to Store & transfer data.

HTML SYNTAX: (Pre-defined tags) – [Non case sensitive]


<!DOCTYPE html> //optional
<html>
<head>
<title></title>
</head>

<body>
<p></p>
<h1></h1>
</body>
</html>

XML SYNTAX: (User-defined tags) - [case-senstive]


<?xml version=”1.0” encoding=”UTF-8” ?> //optional
<root>
<child1>
<subchild></subchild>
</child1>

<child2>
<sub></sub>
<sub></sub>
</child2>
</root>

*Here , starting tag and ending tag should be same and case sensitive.
*Similar to HTML , we can use attributes in XML @ starting tag.
Ex:college.xml

<college>
<student>
<name>Ganesh Peketi</name>
<rollno>14191A0155</rollno>
</student>

<faculty>
<name>Siva Kumar</name>
<branch>CSE</branch>
</faculty>
<HOD college=”GOVT”/>
<principal> Dr.Murali</principal>
</college>

Note: Rules to declare Elements:-


o Case-sensitive
o Must start with a letter (or) underscore
o Cannot contain/start with XML
o Can contain letters, numbers, hyphens, underscore etc
o Cannot contain spaces

Ex: <person>, <firstname>, <lastname>, <first_name>, <firstName>, <FirstName>


<FIRSTNAME>, <firstname>

XML USAGE IN REAL TIME:

- Login with facebook, google account.i.e., data transfer b/w applications


- JAVA <–>PHP, .NET, PYTHON, C (Language Independent )
Lec-3 : XML – Building Blocks:
 XML DOCUMENTS ARE WELLFORMED

 Must be maintain a unique root element.


 Each opening tag has closing tag.
 Elements are case-sensitive.
 Either attribute values can be stored in ‘single’ or “double quotes”.
 Entity References: to store symbols as it is:
< -----> &lt;
> -----> &gt;
& ----->&amp;
‘ ----->&apos;
‘’ ----->&quot;
*Check weather rules are followed or not:
Two Ways:
1)DTD
2)SCHEMA
XML BUILDING BLOCKS:
The main building blocks of an XML documents are:
Elements: These are the main building blocks of XML
Attributes: These will provide extra information about elements. Placed inside the opening
tag.
Entities: Some characters have a special meaning in XML, like less than sign (<) that defines
start of an XML tag. [&lt; , &gt; , &amp; ..]
PCDATA: It means parse-able character data. It will be parsed by a parser, PCDATA cannot
contain any &, <, or > characters.
CDATA: It means character data. Parser will not parse it. It can contain special characters like
<, >, &.

--PCDATA, CDATA are used to store data in Element.


Types of script:Scripts are classified into the following two types.
 client side script
 server side script

Client side script: These scripts are getting executed within the web Browser (client).
Ex: JavaScript, VB script

Server side script: A script which executes in server machine with support of the web
server software’s like IIS(Internet information services), Tomcat, JBOSS, etc.
Ex: php, jsp, asp.net, nodeJS, reactJS
JavaScript Introduction

 JavaScript is an object-based scripting language and its light weight.


> POPs
> OOPs
> OBPs
 JavaScript was invented by “Brendan Eich” in 1995, and became an ECMA
(European Computer Manufacturers Association) standard in 1997.
ECMAScript is the official name of the language.
 Netscape first introduced a JavaScript interpreter in Navigator 2.
 JavaScript is implementation of ECMAScript; ECMAScript is the specification of
JavaScript.
 JavaScript is client-side (browser-side) programming. That means it executes
on the browser.
 It can also be used in server-side by using NodeJS or ReactJS
 JavaScript Translator is responsible to translate the JavaScript code which is
embedded in browser.
 JavaScript is “interpreter-based” language, means the code will be converted
into machine language line-by-line. JavaScript interpreter is already embedded
in Browsers.
 JavaScript is a case sensitive language.
 Most of the JavaScript operators, keyword and control statements are similar
to C language.

Why we Use JavaScript?

Using HTML we can only design a web page but it’s not supported to perform
logical operation such as calculations, decision making and repetitive tasks,
dynamically displaying output, reading inputs from the user, and updating
content on webpage at client side. Hence to perform these entire tasks at client
side we need to use JavaScript.

Where it is used?
There are so many web applications running on the web that are using JavaScript
like Google, Facebook,twitter, amazon, YouTube etc.
It is used to create interactive websites. It is mainly used for:
1. Client-side verifications and validation
2. Dynamic drop-down menus
3. Displaying date and time
4. Build small but complete client side programs
5. Build forms that respond to user input without accessing a server.
6. Displaying popup windows and dialog boxes (like alert dialog box, confirm
dialog box and prompt dialog box)
7. Manipulate HTML "layers" including hiding, moving, and allowing the user
to drag them around a browser window.
etc...

Limitations of JavaScript
JavaScript have some limitations which are given below;

1. Client-side JavaScript does not allow reading and writing of files.


2. It cannot be used for networking applications.
3. It doesn't have any multithreading or multiprocessing capabilities.
4. it doesn't support db connections.

JavaScript Versions
Version Officeal Name Release Date
1 ECMAScript 1 June-1997

2 ECMAScript 2 June-1998

3 ECMAScript 3 Dec-1998

4 ECMAScript 4 2004 (not released)

5 ECMAScript 5 Dec-2009
5.1 ECMAScript 5.1 June-2011

6 ECMAScript June-2015

7 ECMAScript June-2016

8 ECMAScript June-2017

how many ways to imp js?


JS we can develop /imp in 3 ways, but in 4place.
those are:
> inline scripting
inline script nothing but writing code within the tag, by using event/dynamic attributes
for this we need tag & event attribute
onclick, onsubmit, onfocus, oninput etc..
Syn: <tag attributes event="js code">

>internal scripting
> scripting in head sec
head is first executed part of html, hence javascript is also executes first.
<head>
<script type="text/javascript>
JS code
</script>
</head>
> scripting in body sec
body level script is executed after head section
<body>
<script type="text/javascript>
JS code
</script>
</body>

> external scripting


type js code in sep file and then save that file with "filename.js"
re-use
<script src="filename1.js"> </script>
<script src="filename2.js"> </script>

Comments in JavaScript
Comment is nothing but it is a statement which is not display on browser
window. It is useful to understand which code is written for what purpose.
Comments are useful in every programming language to deliver message. It is
used to add information about the code, warnings or suggestions so that the end
user or other developer can easily interpret the code.
Types of Comments:
There are two types of comments are in JavaScript
1. Single-line Comment ex: //comment
2. Multi-line Comment e: /* comments */

Single-line Comment
It is represented by double forward slashes //. It can be used before any
statement.
Example:
<script>
// It is single line comment
document.write("Hello Javascript");
</script>

Multi-line Comment
It can be used to add single as well as multi line comments.
It is represented by forward slash / with asterisk * then asterisk with forward
slash.
Example:
<script>
/* It is multi line comment.
It will not be displayed */
document.write("Javascript multiline comment");
</script>

document write() method: The write() method writes HTML expressions or


javascript code to a document.
Syn:document.write(exp1,exp2,exp3....)

1) document ---> object ---> webpage


2) write() ---> is method ---> webpage level

Example:
<html>
<head>
<script type='text/javascript'>
document.write("<h1>hello world!</h1><p> have a nice day ! </p>");
</script>
</head>
</html>

Can we use HTML tags in write() method?


Yes, we can use tags in write().

writeln() method: The writeln() method is similar to the write method, with the
addition of writing a newline character after each statement.
Syn:document.writeln(exp1,exp2,exp3 ...)

Example:
<!DOCTYPE html>
<html>
<body>
<pre>
<script type='text/javascript'>
document.writeln("Welcome to JS");
document.writeln("Welcome to JS");
</script>
</pre>
</head>
</html>

Note: You have to place writeln() in pre tag to see difference between write()
and writeln().
Writeln() actually produces the output in new line (\n) but browser will not
detect the \n as linebreak, hence to show it correctly and keep format as it is
we will use pre tag.
Example:
<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'>
document.write("<h1 style='color:blue; font-size:30px; font-family:tahoma'>
Welcome To JS</h1>");
document.write("<font color='green' size='16px' face='Arial'> Welcome To
JS</font>");
</script>
</head>
<body>
</body>
</html>

Note:
 the above type of code is known as DHTML
 In JavaScript a string should be in single or double quotes.
 Double quotes inside using single quotes are valid, single quotes inside
using double quotes valid.

Example
<!DOCTYPE html>
<html>
<body>
<script>
document.write("JavaScript is client side script");
document.write("<br>");
document.write("JavScript is 'ECMA' Implementation<br>");
document.write('JavScript released by NetScape<br>');
document.write('NetScape release "Mocha"<br>');
//document.write('NetScape release 'Mocha'<br>'); Error
//document.write("NetScape release "Mocha"<br>"); Error
</script>
</body>
</html>

JavaScript string with escape sequences: An escape character is consisting of


backslash "/" symbol with an alphabet. The following are frequently using escape
characters.
1. \n : inserts a new line
2. \t : inserts a tab
3. \r : carriage return
4. \b : backspace
5. \f : form feed
6. \' : single quote
7. \”: double quote
8. \\ : Backslash

Example:

Difference between window.document.write&document.write:


There is no difference between these two statements,window is highest level
object. It contains child objects & their methods
child object/sub object
|
window.document.write();
| | |
browser page method

document.write();
| |
page method

browser is default object, master object, super object.


write() is a method related to document object

ex:
<head>
<script type='text/javascript'>
window.document.write("livescript is javascript");
document.write("<br>");
document.write('livescript is javascript');
</script>
</head>
JavaScript semicolon(;):
In javascript every statements ends with semicolon(;). It is an optional notation.

ex:
<head>
<script type='text/javascript'>
document.write=("livescript is javascript");
</script>
</head>

ex:
<head>
<script type='text/javascript'>
document.write("javascript");
document.write('livescript');
document.write('livescript is javascript');
</script>
</head>

Note:
1) In the above script semicolon(;) is mandatory.
2) It is a good programming practice to use the semicolon.

JavaScript place in HTML file:


There is a flexibility given to include javascript code any where in a html
document but the follow ways are most prefered in the live environment.
 script in <head>-----</head> section
 script in <body>-----</body> section
 script in <body>-----</body>&<head>-----</head> section
 script in & external file & then include in <head>-----</head> section.

ex:
<head>
<script type='text/javascript'>
document.write("welcome to head section");
</script>
</head>
<body>
<script language="javascript">
document.write("welcome to the body section");
</script>
</body>

External javascript:
Javascript can also be placed in a external files,these files contains javascript
code. This code we can apply on different webpages. External javascript files
extensions is .js

Note:
1) External script cann't contain the <script></script> tags.
2) To use an external script,point to the .js file in the "src" attribute of the
<script> tag.

how to run external js:


Creating javascript file:
document.write("<h1 style='color:blue'> welcome to external javascript....!
</h1>");
document.write("<br/>");
document.write("thank u----");

save with example.js extension @any location.

creating html files:


<html>
<head>
<script type="text/javascript" src="example.js"></script>
</head>
<body>
</body>
</html>
save with .html or .htm extensions.
Javascript code: It is a sequence of javascript statements, each statement is
executed by the browser in the sequence they are returned.
<head>
<script type="text/javascript">
document.write("<p>This is paragraph</p>") //js code
</script>
</head>
Javascript blocks: JavaScript sentences can be group together in blocks. Blocks
starts with a left curly bracket { and end with a right curly bracket }. The purpose
of your block is to make the sequence of statement excute together.
<head>
<script type="text/javascript">
{
document.write(This is a block);
}
</script>
</head>

JavaScript popup boxes: JavaScript has 3 kinds of popup boxes.


1. Alert box
2. Confirm box
3. Prompt box

Alert box: An alert box is often used if you want to make sure information comes
through the user. When an alert box pops up, the user will have to click "ok" to
proceed.
Syn: alert("message");
ex:
<body>
<script type='text/javascript'>
alert("invalid entry");
</script>
</body>

How to display multiple line on the alert:


We cann't the use <br> tag here because alert is a method of the windows
object, that cann't be interpret html tag. Insted we use the new line escape
character.
<head>
<script type="text/javascript">
alert("javascript \n is\n a\n client-side \n programming \n language");
</script>
</head>

ex: Alert with functions


<head>
<script type='text/javascript'>
functionmyAlert(){
alert("javascript \n is \n a \n client-side \n programming \n
language");
alert("1 \n \t 2 \n \t \t3");
}
</script>
</head>
<body>
<p> click the button to display alert messages ....</p>
<button onclick="myAlert()"> click me</button>
</body>

confirm box:
It is often used, If you want the user to verify and accept something. When a
confirm box pops up, the user will have to click either "ok" or "cancel" to
proceed. If the user clicks "ok" the box returns true. If the user clicks "cancel" the
box returns "false".
Syntax: confirm("message");
ex:
<head>
<script type='text/javascript'>
confirm("click ok or cancel");
</script>
</head>

ex:
<head>
<script type='text/javascript'>
var x=confirm("click ok or cancel");
alert("user selected option is:"+x);
</script>
</head>

ex:
<head>
<script type='text/javascript'>
var x=confirm("click ok or cancle");
alert("user selected option is:"+x);
if(x==true) {
alert("user clicked on OK button");
}
else{
alert("user clicked on cancel button");
}
</script>
</head>

ex: confirm with function


<head>
<script type='text/javascript'>
functionmyConfirm(){
var x=confirm("click ok or cancel");
alert("user selected option is:"+x);
if(x==true) {
alert("user clicked on ok button");
}
}
</script>
</head>
<body>
<p> click the button to display the user selected result..</p>
<button onclick="myConfirm()">confirm</button>
</body>

Prompt Box: It is used to, if you want the user to input a value before entering a
page. When a prompt box pops up the user will have to click either "ok" or
"cancel" to proceed after entering an input value. If the user clicks "ok" the box
returns the value/empty. If the user clicks "cancel" the box returns "null".
Syntax: prompt("sometext", defaultvalue);
ex:
<head>
<script type='text/javascript'>
prompt("Enter Any Number:");
</script>
</head>
ex:
<head>
<script type='text/javascript'>
varMyVal=prompt("Enter Any Number:");
alert("User Entered value is:"+MyVal);
</script>
</head>

Note: these 3methods are provided by window object.

External JavaScript with popup boxes:


step1: Create a required js file
functionMyAlert(){
alert("welcome to externaljs");
}
functionMyConfirm(){
confirm("click ok or cancel");
}
functionMyPrompt(){
prompt("Enter Any Value");
}

Save with .js extension @ any location....!!

step2: preparing required html file.


<html>
<head>
<script type="text/javascript" src="myscript.js">
</script>
</head>
<body>
<p>Click the button to display alert message..</p>
<button onclick="MyAlert()">Alert</button>
<p>click the button to display confirm message...</p>
<button onclick="MyConfirm()">confirm</button>
<p>click the button to display prompt value..</p>
<button onclick="MyPrompt()">prompt</button>
</body>
</html>

Inline JavaScript

When java script was written within the html element using attributes related to
events of the element then it is called as inline java script.

Example of Inline JavaScript


<html>
<form>
<input type="button" value="Click" onclick="alert('Button Clicked')"/>
</form>
</html>

Internal JavaScript

When java script was written within the section using element then it is called as
internal java script.

Example of Internal JavaScript


<html>
<head>
<script>
functionmsg()
{
alert("Welcome in JavaScript");
}
</script>
</head>
<form>
<input type="button" value="Click" onclick="msg()"/>
</form>
</html>
External JavaScript

Writing java script in a separate file with extension .js is called as external java
script. For adding the reference of an external java script file to your html page,
use tag with src attribute as follows

Example
<script type="text/javascript" src="filename.js"/>
 Create a file with name functions.js and write the following java script
functions in it.
a.js
functionmsg()
{
alert("Welcome to JavaScript");
}
 Create a html page and use the file functions.js as follows

index.html
<html>
<head>
<script type="text/javascript" src="a.js"></script>
</head>
<body>
<form>
<input type="button" value="click" onclick="msg()"/>
</form>
</body>
</html>
Variable Declaration
variable is a reference name of a memory block.
variable are created or stored in RAM.
variable are used to store/to hold a value for reuse purpose.
Java script did not provide any data types for declaring variables and a variable in
java script can store any type of value. Hence java script is loosely typed
language.

We can use a variable directly without declaring it.

how to declare a variable?


we can define vars in JS two ways, those are:
> by using "var" keyword
> by using "let" keyword

Rules to declaring a variable


 Name should start with a letter (a to z or A to Z), underscore( _ ), or dollar( $
) sign.
 After first letter we can use digits (0 to 9).
 variables are case sensitive. for example a and A are different variables.
 space are not allowed, means name should be single word.
 special chars (symbols) are not allowed in name, except _ and $.
 keywords we can't use as a name.

Variable declaration
Example
var x = 10; // Valid
var _value="porter"; // Valid
var 123=20; // Invalid
var #a=220; // Invalid
var user name="siva"; // Invalid
var case=10.56;

Example of Variable declaration in JavaScript

<script>
var a=10;
var b=20;
var c=a+b;
document.write(c);
</script>
Output
30

Types of Variable in JavaScript

 Local Variable
 Global Variable

Local Variable

A variable which is declared inside block or function is called local variable. It is


accessible within the function or block only.

Ex: JS8.html
For example:

<script>
function abc()
{
var x=10; //local variable
}
</script>
or
Example
<script>
If(10<13)
{
var y=20;//javascript local variable
}
</script>

Global Variable
var is declared with in script tag but outside function & block those are global
variables.
these global variable is accessible from anywhere in program.
declared with window object is known as global variable.

Ex: JS9.html

For example:
<script>
var value=10;//global variable
function a()
{
alert(value);
}
function b()
{
alert(value);
}
</script>

Declaring global variable through window object


The best way to declare global variable in javascript is through the window
object.
Syntax
window.var=value;
Now it can be declared inside any function and can be accessed from any
function.

For example:
function m()
{
window.value=200; //declaring global variable by window object
}
function n()
{
alert(window.value); //accessing global variable from other function
}

In JS we can declare the variables the following two ways.


1. Implicit declaration
2. Explicit declaration

Implicit declaration: In every scripting it is the default declaration.


ex: y=100;

Explicit declaration: All programming languages default declaration


ex: int a=5;

Scripts are able to support implicit declaration but languages are only explicit
declaration.
Note: Explicit declaration is always recommended as a good programming
practice.

Javascript datatypes:
In javascript data types are classified into the following two types.
1. primitive datatypes
2. non-primitive datatypes

primitive data types: javascript has a five primitive data types.


1. string Ex: "siva" 'apples123' "Dno: 1-2-3/1a"
2. number Ex: 10 -25 100.56 -3.7
3. boolean Ex: true false
4. undefined datatype is not reco
5. object null

Non-primitive datatypes: These are popularly known as reference or composite


data types.
Note:
The main difference b/w PDT’s and NPDT’s: In PDT value stored in variable,
whereas NPDT’s will store reference addresses only.

Primitive data types:


Strings: In javascript a String should be within single or double quotes.
var name="nit";
var name='nit';
Number: Javascript has only one type of numbers,they can be return with or
without decimals
var x1=34.00; with decimals
var x2=34 without decimals

Boolean: It is used to represent a booleanvalue,These are as follows.


var x = true //equivalent to true, yes or on
var y = false //equivalent to false, no or off

undefined: It is a value of variable with no value.


var x; //now x is undefined

Null: variables can be emptied by setting the value to null.


ex: var x=null; //now x is null

typeof
typeof is predefine function, and it's used to identify datatype of a variable.
Syn: typeof var-name

Ex: JS10.html

Dynamic data types: Javascript has dynamic types. This means that the same
variable can be used as different types.
ex:
var x; //now x is undefined
var x=5; // now x is a number
var x="ram"; // now x is a String

Ex: JS11.html
<!-- example on variable declaration -->
<!DOCTYPE html>
<html lang="en">
<head>
<title>JS Ex13</title>
</head>
<body>
<h1> Demo on difference between var and let </h1>
<script>
var a=10; //define
document.write(a +"<br>");
var a=20.56; //re-defination
document.write(a +"<br>");
var a="apple"; //re-defination
document.write(a +"<br>");

let n=101; //define


document.write(n +"<br>");
//let n=202; re-defination ==> Error: Identifier 'n' has already been
declared
n=202; //changing value
document.write(n +"<br>");
</script>
</body>
</html>

Non-primitive data types: When a variable is declared with the keyword new,
the variable is an object.
new is used for dynamic memory allocations (for creating objects).
these datatypes are also called as reference datatype.
Ex:
var st=new String();
var x=new Number();
let y=new Boolean();
here LHS are reference variables, and RHS are objects.
reference variables are storing address of dynamic memory (object)
JavaScript operators
operator is a symbol (special char) and it is used to perform certain
operation(task).
every operator is a symbol, but every symbol is not operator.
every operator requires some values, those are called as operands.
Ex:

Javascript supports the following list of operators:


 arithmetical operators
 relational operators
 logical operators
 assignment operators
 incre/decre operators
 ternary operator
 concat operator etc...
Arithmetical operators: using these operators we can perform the basic math
calculations.
operator are:
operator Description example
+ addition j+12
- subtraction j-22
* multiplication j*7
/ division j/3.14
% modulus j%6

relational operators: these operator are used to provide comparison between


two operands. these are boolean operators (true/false).
operator are:
operator Description example
== is equal to j==42
!= is not equal to j!=17
> is greater than j>0
< is less than j<100
>= is greater than or equal to j>=23
<= is less than or equal to j<=13

Logical operators: these operators are used to perform multiple comparisons


@time. these are boolean operators (true/false).
operator are:
operator Description example
&& And j==1 && k==2
|| OR j<100 || j>0
! Not !(j==k)

assignment operators: these operator are used to store/assign value.


operator are:
operator Description example
= store a=10
shorthand:
+= addition & assign a+=10
-= subtract & assign a-=5
*= product & assign a*=20
/= division & assign a/=7
%= modulus & assign a%=6

unary operators: these operator are used to increment or to decrement a value.


operators are ++ and --
++ (increment) ==> it adding 1 to an existing value Ex: a++
-- (decrement) ==> it subtracting 1 from an existing value Ex: a--

ternary operator: this operator are used to decision making operation. operator
is ?:, this operator also called as conditional operator.
(condition) ? statement1 : statement2

concat operator: this operator are used to concat multiple strings then formed
into a single string. one operand should be string to perform concatenation.
resultant value comes in string format.
operator is +.
Ex: "rama"+"rao ==> "ramarao"
"mangos"+123 ==> "mangos123"
ParseInt():

--pareseInt() is one of predefined function of JS.

--It changing string format integer into integer format

Ex: “100”  100

“ram”  NaN (Not a Number). NaN is a Keyword.

Ex:JS16,17

Control Statement
Control statements are used to control (change) execution flow of program
based input data.
Types:
> Conditional statements (Decision making statements’)
> loops (iterations)
> Un-conditional statements (jump)

Conditional Statements:

If Statement
if statement is used to perform decision-making operations. Means if condition is
true it executes some statements, if condition is false it executes some other
statements.

There are three forms of if statement.


 if
 If else
 if else if (ladder if)

If statement
if is most basic statement of Decision making statements. It tells to program to
execute a certain part of code only if particular condition or test case is true.
Example
<script>
var a=10;
if(a>5)
{
document.write("value of a is greater than 5");
}
</script>

if-else statement
In general it can be used to execute one block of statement among two blocks.

Example of if..else statement


<script>
var a=40;
if(a%2==0)
{
document.write("a is even number");
}
else{
document.write("a is odd number");
}
</script>

Result:
a is even number

JavaScript If...else if statement (ladder_if):


It evaluates the content only if expression is true from several expressions.

Syntax
if(expression1)
{
//content to be evaluated if expression1 is true
}
else if(expression2)
{
//content to be evaluated if expression2 is true
}
else
{
//content to be evaluated if no expression is true
}
Example of if..else if statement
<script>
var a=40;
if(a==20)
{
document.write("a is equal to 20");
}
else if(a==5)
{
document.write("a is equal to 5");
}
else if(a==30)
{
document.write("a is equal to 30");
}
else
{
document.write("a is not equal to 20, 5 or 30");
}
</script>

Note:
> if support 6types relational operators (condition)
> comp styles:
> var to var a>b
> var to val n<0
> val to val 1>2
> val to var 1<a
> if supports all 4
> if supports all datatypes

switch statement
> Switch is selection statement, but it is not decision making.
> Switch is not allows to use relational operators, but its performs equality
operation.
> Its supports all types comparison style and all datatypes.
> Its better performance.

Syn:
switch(var)
{
case value: statements...
break;
case value: statements...
break;
case ...
default: statements...
}

Looping Statement
Set of instructions given to the compiler to execute set of statements until
condition becomes false is called loops. The basic purpose of loop is code
repetition.
The way of the repetition will be forms a circle that's why repetition statements
are called loops. Some loops are available In JavaScript which are given below.
 while loop (top testing/entry level)
 for loop
 do-while (bottom testing/exit level)

while loop
When we are working with “while loop” always pre-checking process will be
occurred. Pre-checking process means before evolution of statement block
condition part will be executed. “While loop” will repeat in clock wise direction.
Example of while loop
<script>
var i=10;
while (i<=13)
{
document.write(i + "<br/>");
i++;
}
</script>

do-while loop
In implementation when we need to repeat the statement block at least 1 then
go for do-while. In do-while loop post checking of the statement block condition
part will be executed.

Example of do-while loop


<script>
var i=11;
do{
document.write(i + "<br/>");
i++;
}while (i<=15);
</script>
for Loop
For loop is a simplest loop first we initialized the value then check condition and
then increment and decrements occurred.

Steps of for loop

Example of for loop


<script>
for (i=1; i<=5; i++)
{
document.write(i + "<br/>")
}
</script>

<noscript> tag: It is used to provide an alternate contains for users when script is
disabled or not supporting, It is a paired tag. It is always declared within the body
section.
syntax: <noscript>------</noscript>
ex:
<head>
<script type='text/javascript'>
alert("welcome to js");
</script>
</head>
<body>
<noscript>
<p style='color:red'>oops your browser not supporting javascript
update/change the script settings and try..</p>
</noscript>
</body>

You might also like