Javasxript Lecuture Note I-1
Javasxript Lecuture Note I-1
COURSE CONTENTS:
1. Introduction to JavaScript
2. Variables and Data Types
3. Operators
4. Control Statements (if, switch)
5. Loops (for, while, do...while)
6. Functions
7. Arrays and Objects
8. Comments
1. Introduction to JavaScript
Furthermore, JavaScript is the programming language of the web, it can update and change both
HTML and CSS. It can calculate, manipulate and validate data
Features of JavaScript:
"finds" an HTML element (with id="demo"), and changes the element content (innerHTML) to
"Hello JavaScript":
Example:
<!DOCTYPE html>
<html>
<body>
</body>
</html>
OUTPUT
Click
Click Me!
Me
Hello JavaScript!
Practical 1
<!DOCTYPE html>
<html>
<body>
<h2>What Can JavaScript Do?</h2>
<p id="demo">JavaScript can change the style of an HTML element.</p>
<button type="button"
onclick="document.getElementById('demo').style.fontSize='35px'">Click Me!</button>
</body>
</html>
Practical 2
<!DOCTYPE html>
<html>
<body>
<p>In this case JavaScript changes the value of the src (source) attribute of an image.</p>
</body>
</html>
2. Statements
A JavaScript statement is an instruction for the browser to perform an action. Statements can be
variable declarations, function calls, loops, conditionals, etc. A computer program is a list of
"instructions" to be "executed" by a computer. In a programming language, these programming
instructions are called statements. A JavaScript program is a list of programming statements.
This statement tells the browser to write "Hello Dolly." inside an HTML element with id="demo":
Example:
<html>
<body>
<h2>JavaScript Statements</h2>
OUTPUT
JavaScript Statements
Hello Dolly.
Practical I
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Statements</h2>
<p id="demo1"></p>
<script>
let a, b, c;
a = 5;
b = 6;
c = a + b;
document.getElementById("demo1").innerHTML = c;
</script>
</body>
</html>
KEYWORDS
JavaScript statements often start with a keyword to identify the JavaScript action to be performed.
Here is a list of some of the keywords you will learn about in this tutorial:
Keyword Description
Variables store data that can be used later in a program. JavaScript uses var, let, and const for
variable declaration.
Data Types:
Example:
Example:
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Variables</h1>
<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>
OUTOUT
JavaScript Variables
Practical I
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Variables</h1>
<p id="demo"></p>
<script>
const price1 = 5;
const price2 = 6;
document.getElementById("demo").innerHTML =
</script>
</body>
</html>
Practical II
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Variables</h1>
<p id="demo"></p>
<script>
const pi = 3.14;
document.getElementById("demo").innerHTML =
</script>
</body>
</html>
3. Operators
JavaScript operators are used to perform different types of mathematical and logical computations.
Examples:
Types of Operators:
• Arithmetic: +, -, *, /, %, **
• Assignment: =, +=, -=, etc.
• Comparison: ==, ===, !=, !==, >, <, >=, <=
• Logical: &&, ||, !
• Unary: typeof, ++, --
• Ternary: condition ? expr1 : expr2
Example:
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Operators</h1>
<h2>The Assignment (=) Operator</h2>
<p id="demo"></p>
<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>
OUTPUT
JavaScript Operators
Practical I
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Arithmetic</h1>
<h2>The + Operator</h2>
<p id="demo"></p>
<script>
let x = 5;
let y = 2;
let z = x + y;
document.getElementById("demo").innerHTML = z;
</script>
</body>
</html>
Practical II
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Arithmetic</h1>
<h2>The * Operator</h2>
<p id="demo"></p>
<script>
let x = 5;
let y = 2;
let z = x * y;
document.getElementById("demo").innerHTML = z;
</script>
</body>
</html>
5. Control Statements
Very often when you write code, you want to perform different actions for different decisions.
If Statement:
Use the if statement to specify a block of JavaScript code to be executed if a condition is true.
Syntax
{
if (condition)
// block of code to be executed if the condition is true
}
Example
<!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>
OUTPUT
JavaScript if
Good day!
Use the else statement to specify a block of code to be executed if the condition is false.
if (condition) {
} else {
Example
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript if .. else</h2>
<p id="demo"></p>
<script>
let greeting;
} else {
document.getElementById("demo").innerHTML = greeting;
</script>
</body>
</html>
OUTPUT
JavaScript if .. else
A time-based greeting:
Good day
Use the else if statement to specify a new condition if the first condition is false.
Syntax:
if (condition1) {
// block of code to be executed if condition1 is true
} else if (condition2) {
// block of code to be executed if the condition1 is false and condition2 is true
} else {
// block of code to be executed if the condition1 is false and condition2 is false
}
Example
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript if .. else</h2>
<p>A time-based greeting:</p>
<p id="demo"></p>
<script>
const time = new Date().getHours();
let greeting;
if (time < 10) {
greeting = "Good morning";
} else if (time < 20) {
greeting = "Good day";
} else {
greeting = "Good evening";
}
document.getElementById("demo").innerHTML = greeting;
</script>
</body>
</html>
OUTPUT
JavaScript if .. else
A time-based greeting:
Good day
Practical I
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Math.random()</h2>
<p id="demo"></p>
<script>
let text;
} else {
document.getElementById("demo").innerHTML = text;
</script>
</body>
</html>
Switch Statement:
Use the switch statement to select one of many code blocks to be executed.
Syntax
switch(expression) {
case x:
// code block
break;
case y:
// code block
break;
default:
// code block
}
Example
This example uses the weekday number to calculate the weekday name:
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript switch</h2>
<p id="demo"></p>
<script>
let day;
switch (new Date().getDay()) {
case 0:
day = "Sunday";
break;
case 1:
day = "Monday";
break;
case 2:
day = "Tuesday";
break;
case 3:
day = "Wednesday";
break;
case 4:
day = "Thursday";
break;
case 5:
day = "Friday";
break;
case 6:
day = "Saturday";
}
document.getElementById("demo").innerHTML = "Today is " + day;
</script>
</body>
</html>
OUTPUT
JavaScript switch
Today is Thursday
The default Keyword
The default keyword specifies the code to run if there is no case match:
Example
If today is neither Saturday (6) nor Sunday (0), write a default message:
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript switch</h2>
<p id="demo"></p>
<script>
let text;
switch (new Date().getDay()) {
case 6:
text = "Today is Saturday";
break;
case 0:
text = "Today is Sunday";
break;
default:
text = "Looking forward to the Weekend";
}
document.getElementById("demo").innerHTML = text;
</script>
</body>
</html>
OUTPUT
JavaScript switch
Looking forward to the Weekend
Switching Details
If no matching cases are found, the program continues to the default label.
If no default label is found, the program continues to the statement(s) after the switch.
Strict Comparison
A strict comparison can only be true if the operands are of the same type.
Example
let x = "0";
switch (x) {
case 0:
text = "Off";
break;
case 1:
text = "On";
break;
default:
text = "No value found";
}
OUTPUT
JavaScript switch
No value found
6. Functions
Example
// Function to compute the product of p1 and p2
function myFunction(p1, p2) {
return p1 * p2;
}
OUTPUT
JavaScript Functions
12