0% found this document useful (0 votes)
7 views

Cyber Security Practical_ Experiment 10

The document outlines an experiment aimed at teaching basic concepts of JavaScript, including variables, data types, operators, control structures, functions, and objects. It provides examples of JavaScript code demonstrating these concepts, such as variable declaration, arithmetic operations, comparison operators, and control flow using if-else statements and loops. The document serves as an introductory guide for learners to familiarize themselves with fundamental JavaScript programming techniques.

Uploaded by

paulemma2020
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Cyber Security Practical_ Experiment 10

The document outlines an experiment aimed at teaching basic concepts of JavaScript, including variables, data types, operators, control structures, functions, and objects. It provides examples of JavaScript code demonstrating these concepts, such as variable declaration, arithmetic operations, comparison operators, and control flow using if-else statements and loops. The document serves as an introductory guide for learners to familiarize themselves with fundamental JavaScript programming techniques.

Uploaded by

paulemma2020
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

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>

JavaScript is the programming language of <h2>My First JavaScript</h2>


the Web.
<button type="button"
onclick="document.getElementById('demo').innerHTML = Date()">
Click me to display Date and Time.</button>

<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>

<p>Display "Good day!" if the hour is less than 18:00:</p>

<p id="demo">Good Evening!</p>

<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.

Different Kinds of Loops


JavaScript supports different kinds of loops:
•for - loops through a block of code a number of times
•for/in - loops through the properties of an object
•for/of - loops through the values of an iterable object
•while - loops through a block of code while a specified condition is true
•do/while - also loops through a block of code while a specified condition is true
<!DOCTYPE html>
<html>
<body>

<h2>JavaScript For Loop</h2>

<p id="demo"></p>

<script>
let text = "";

for (let i = 0; i < 5; i++) {


text += "The number is " + i + "<br>";
}

document.getElementById("demo").innerHTML = text;
</script>

</body>
</html>

You might also like