Js Intro
Js Intro
Technologies
Workshop
Information Technology
JAVA SCRIPT
TOPICS:
What is JavaScript
Why JavaScript
JavaScript uses
Java Vs JavaScript
How and Where to JS code
Data Types
Pop-up Boxes
Conditional Statements
Loop Statements
WHAT IS JAVASCRIPT
JAVA JAVASCRIPT
<script> tag:
❖ The <script> tag is used to embed scripting statements into html file
❖ <script> tag can be placed in the <head> section or in the <body> section or
below the <body> section of html page
Syntax:
<body>
<script>type your scripting statement here
</script>
</body>
Example:
<!DOCTYPE html>
<html>
<body>
<script>
document.write("Welcome to JavaScript Class")
</script>
</body>
</html>
Code via an external file:
Syntax:
<body>
<script src=“filename.js”>
</script>
</body>
Example:
<!DOCTYPE html>
<html>
<body>
<script src=“example.js”></script>
</body>
</html>
Example.js file:
JavaScript has dynamic types. This means that the same variable can be used
to hold different data types
var X; //Here x is undefined
X=7; //Here x is Number
X=“CSI” //Here x is String
JavaScript is a dynamic type language, means you don't need to specify type of
the variable because it is dynamically used by JavaScript engine. You need to
use var here to specify the data type.
JavaScript provides different data types to hold different types of values. There are
two types of data types in JavaScript
1. Primitive data type
2. Non Primitive data type
Boolean:
represents boolean value either false or true.Boolean() function is
used to find out if given statement is true
EX:
(Boolean(9>7)
Undefined:
represents undefined value i.e variable with no value
EX-1:
let x;
if (x === "undefined") {
text = "x is undefined";
} else {
text = "x is defined";
}
EX-2:
let x;
if (typeof x === "undefined") {
text = "x is undefined";
} else {
text = "x is defined";}
Null:
The value Null represents the intentional absence of any object value.It is treated as falsy
for Boolean operations
EX:
var x=null
Regular Expressions:
represents regular expression
EX:
const regexp=/xyz/;
JavaScript Pop-up Boxes
❑ Alert Box
❑ Confirm Box
❑ Prompt Box
Alert Box:
An alert dialog box is mostly used to inform or alert the user by displaying some
messages in a small dialogue box.
EX:
<body>
<script>alert(“Alert box displayed”)
</script>
</body>
Confirm Box:
A confirmation box is used to let the user make a choice.
EX:
<body>
<script>confirm(“click ok or cancel to confirm”)
</script>
</body>
prompt Box:
JavaScript Prompt Box can be used when we want to get some user
input.
EX:
<body>
<script>prompt(“Are You interested in Web Technology Course”)
</script>
Operators
❑ Arithmetic Operators
❑ Assignment Operators
❑ Relational Operators
❑ Logical Operators
Arithmetic Operators:
Example:
<html>
<head>
<title>Operator Example</title>
</head>
<body>
<script >
var a = 5
++a
alert("The value of a = " + a )
-->
</script>
</body>
</html>
Assignment Operators:
Example:
<html>
<head>
<title>Operator Example</title>
</head>
<body>
<script>
var a = 10
var b = 2
a += b
alert("a += b is " + a )
</script>
</body>
</html>
Relational Operators:
Logical Operators:
Example:
<html>
<head>
<title>Operator Example</title>
</head>
<body>
<script>
userID = prompt("Enter User ID"," ")
password = prompt("Enter Password"," ")
if(userID == "user" && password == "secure")
{
alert("Valid login")
}
else
{
alert("Invalid login")
}
</script>
</body>
</html>
Conditional Statements:
▪ if...else
▪ switch.
if...else Statement
▪ Simple if
▪ if-else Statement
▪ Nested if
▪ Else-if ladder
If-else
Simple if
if (expression)
if (expression) statement or block
statement; else
statement or block
Nested if
else-if ladder
if (expression)
{ if (expression1)
if (expression) statement or block
statement or block else if (expression2)
else statement or block
statement or block else if (expression3)
} statement or block
else else
statement or block statement or block
Example:
<html>
<body>
<script>
var d = new Date()
var time = d.getHours()
if (time < 10)
{
document.write("<b>Good morning</b>")
}
else
{
document.write("<b>Good day</b>")
}
</script>
</body>
</html>
</html>
Switch
Statement
• A switch statement allows a program to evaluate an expression and
attempt to match the expression's value to a case label. If a match is
found, the program executes the associated statement.
• Syntax
switch (expression)
{
case label_1:
statements_1
break;
case label_2:
statements_2
break;
….
default:
statements_def
}
EXAMPLE:
<!DOCTYPE html>
<html>
<body>
<script>
let day;
switch (new Date().getDay())
{
case 0:
document.write("Weekend");
break;
case 6:
document.write("Weekend");
break;
default:
document.write("Not Weekend");
break;
}
</script>
</body>
</html>
Loop Statements
• For loop
• do while
• while loop
for Loop
Syntax of for loop
for (var=startvalue;var<=endvalue;var=var+increment)
{
code to be executed
}
<html>
<body>
<script>
for (i = 1; i <= 6; i++)
{
document.write(i*2)
document.write("<br/>")
}
</script>
</body>
</html>
do...while Statement
The do...while statement repeats until a specified condition evaluates to false. A
do...while statement looks as follows:
do
{
statement
} while (condition);
Example:
<html>
<body>
<script language=“javascript”> i=0
do { i++;
document.write(i);
} while (i<5);
While Loop
A while statement executes its statements as long as a specified
condition evaluates to true
while (condition)
{
statement
}
Example:
<html>
<body>
<script >
var i=0
while (i<=10)
{
document.write("The number is " + i)
document.write("<br />")
i=i+1
}
ASSIGNMENT