Module 4-JavaScript
Module 4-JavaScript
APPLICATIONS
MODULE 4 - SYLABUS
• JavaScript:
• Client-Side Scripting
• What is JavaScript and What can it do?
• How to develop Javascript
• Simple Javascript
• Variables,
• Functions
• Conditions
• loops and repetition
JavaScript: Client-Side Scripting
Section 1 of 8
WHAT IS JAVASCRIPT
What is JavaScript
• Inline
• Embedded
• External
Inline JavaScript
Mash it in
<button type="button"
onclick='document.getElementById("demo").innerHTML = "Hello
JavaScript!"'>Click Me!</button>
</body>
</html>
Embedded JavaScript
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
</head>
<body>
<script>
//Script logic
</script>
</body>
</html
Embedded JavaScript
Better
<script>
function myFun(){
alert("Hello there!");
}
</script>
</body>
</html>
External JavaScript
Better
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
//Referencing external javascript file
<script src="script.js"></script>
</head>
<body>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-
scale=1.0" />
<link rel="stylesheet" src="style.css" />
<title>Document</title>
<script src="script.js"></script>
</head>
<body>
<button onclick="myFun()">Alert</button>
</body>
</html
// Script.js
function myFun(){
alert("Hello there!");
}
How to Reference External
JavaScript Files
• By using a full URL:
src = https://www.geeksforgeek.org/js/script.js
SYNTAX
JavaScript Syntax
JavaScript’s reputation for being quirky not only stems from its strange
way of implementing object-oriented principles but also from some odd
syntactic gotchas:
• Everything is type sensitive, including function, class, and variable
names.
• The scope of variables in blocks is not supported. This means
variables declared inside a loop may be accessible outside of the
loop, counter to what one would expect.
• There is a === operator, which tests not only for equality but type
equivalence.
• Null and undefined are two distinctly different states for a variable.
• Semicolons are not required, but are permitted (and encouraged).
• There is no integer type, only number, which means floating-point
rounding errors are prevalent even with values intended to be
integers.
Variables
var
• Automatically
• Using var
• Using let
• Using const
Variables
Assignment
Automatically declaredVariables
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript automatic Variables</h1>
<p>In this example, x, y, and z are undeclared.</p>
<p>They are automatically declared when first used.</p>
<p id="sum"></p>
<script>
x = 1;
y = 2;
z = x + y;
document.getElementById("sum").innerHTML =
"The value of z is: " + z;
</script>
</body>
</html>
JavaScript Let
The let keyword in JavaScript is used to make variables that are scoped to
the block they’re declared in.
Once you’ve used let to define a variable, you cannot declare it again
within the same block. It’s important to declare let variables before using
them.
The variables which are declared inside the { } block are known as block-
scoped variables. variables declared by the var keyword cannot be block-
scoped.
Let Example
{
let num = 5;
Output:
5
Uncaught ReferenceError: num is not defined
let Example
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript let Variables</h1>
<p id="let"></p>
<script>
let x = 1;
let y = 2;
let z = x + y;
document.getElementById("let").innerHTML =
"The value of z is: " + z;
</script>
</body>
</html>
Const Example
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Variables</h1>
<p id="demo"></p>
<script>
const x = 5;
const y = 6;
const z = x + y;
document.getElementById("demo").innerHTML =
"The value of z is: " + z;
</script>
</body>
</html>
Comparison Operators
True or not True
== Equals
(x==9) is true
(x=="9") is true
(x==="9") is false
=== Exactly equals, including type
(x===9) is true
(x!=="9") is true
!== Not equal in either value or
type (x!==9) is false
== equal to
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Comparison</h1>
<h2>The == Operator</h2>
<p id="test"></p>
<script>
let x = 6;
document.getElementById("test").innerHTML = (x == 9);
</script>
</body>
</html>
=== equal value and equal type
Comparison: Only returns true if both the value and the type of the
operands are exactly the same.
Examples:5 === '5' returns false because the types (number vs.
string) are different.
0 === false returns false because the types (number vs. boolean) are
different.
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Comparison</h1>
<h2>The === Operator</h2>
<p>Assign 5 to x, and display the value of the comparison (x === 5):</p>
<p id="test"></p>
<script>
let x = 7;
document.getElementById("test").innerHTML = (x === '7');
</script>
</body>
</html>
Logical Operators
The Boolean operators and, or, and not and their truth tables
are listed in Table 6.2. Syntactically they are represented with
&& (and), || (or), and ! (not).
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Comparison</h1>
<h2>The && Operator (Logical AND)</h2>
<p>The && operator returns true if both expressions are true,
otherwise it returns false.</p>
<p id="demo"></p>
<script>
let x = 6;
let y = 3;
document.getElementById("demo").innerHTML =
(x < 10 && y > 1) + "<br>" +
(x < 10 && y < 1);
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Comparison</h2>
<p>The NOT operator (!) returns true for false statements and false for
true statements.</p>
<p id="demo"></p>
<script>
let x = 3;
let y = 6;
document.getElementById("demo").innerHTML =
!(x > y);
</script>
</body>
</html>
Conditionals
If, else if, …, else