Cyber Security Practical_ Experiment 10
Cyber Security Practical_ Experiment 10
Aim: To study the concepts of java script with the help of few basic programs.
Learning Objective:
At the end of the session, you will be familiar with basic concepts and programs in JavaScript like:
• Understanding the fundamentals of JavaScript
• Understanding variables and data types in JavaScript
• Using arithmetic, comparison and logical operators in JavaScript
• Understanding control structures like if-else statements and loops in JavaScript
• Using functions and objects in JavaScript
<!DOCTYPE html>
JavaScript is the world's most popular <html>
programming language. <body>
<p id="demo"></p>
</body>
</html>
JavaScript Variables
<!DOCTYPE html>
Variables are Containers for Storing Data
<html>
JavaScript Variables can be declared in 4 ways:
•Automatically <body>
•Using var <h1>JavaScript Variables</h1>
•Using let
•Using const <p>In this example, x, y, and z are undeclared.</p>
<p>They are automatically declared when first used.</p>
<p id="demo"></p>
<script>
x = 5;
y = 6;
z = x + y;
document.getElementById("demo").innerHTML =
"The value of z is: " + z;
</script>
</body>
</html>
JavaScript Operators <!DOCTYPE html>
<html>
Javascript operators are used to perform different <body>
types of mathematical and logical computations.
Examples: <h1>JavaScript Operators</h1>
The Assignment Operator = assigns values <h2>The Assignment (=) Operator</h2>
The Addition Operator + adds values
The Multiplication Operator * multiplies values <p id="demo"></p>
The Comparison Operator > compares values
<script>
// Assign the value 5 to x
let x = 5;
// Assign the value 2 to y
let y = 2;
// Assign the value x + y to z
let z = x + y;
// Display z
document.getElementById("demo").innerHTML = "The sum
of x + y is: " + z;
</script>
</body>
</html>
JavaScript Comparison Operators
Operator Description <!DOCTYPE html>
== equal to <html>
=== equal value and equal type <body>
!= not equal
!== not equal value or not equal type <h1>JavaScript String Operators</h1>
> greater than
< less than <p>All conditional operators can be used on both numbers
>= greater than or equal to and strings.</p>
<= less than or equal to
? ternary operator <p id="demo"></p>
<script>
let text1 = "A";
let text2 = "B";
let result = text1 < text2;
document.getElementById("demo").innerHTML = "Is A less
than B? " + result;
</script>
</body>
</html>
JavaScript if, else, and else if
Conditional statements are used to perform different actions based on different conditions
In JavaScript we have the following conditional statements:
•Use if to specify a block of code to be executed, if a specified condition is true
•Use else to specify a block of code to be executed, if the same condition is false
•Use else if to specify a new condition to test, if the first condition is false
•Use switch to specify many alternative blocks of code to be executed
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript if</h2>
<script>
if (new Date().getHours() < 18) {
document.getElementById("demo").innerHTML = "Good
day!";
}
</script>
</body>
</html>
JavaScript For Loop
Loops are handy, if you want to run the same code over and over again, each time with a
different value.
<p id="demo"></p>
<script>
let text = "";
document.getElementById("demo").innerHTML = text;
</script>
</body>
</html>