Web Lecture Week4
Web Lecture Week4
HTML Document?
4
<head>
5
<title>
6
Inline JavaScript
7
</title>
8
</head>
9
10
<body>
11
<h2>
12
Adding JavaScript in HTML Document
13
</h2>
14
15
<button onclick="alert('Button Clicked!')">
16
Click Here
17
</button>
18
</body>
19
20
</html>
Output
4
<head>
5
<title>
6
Add JavaScript Code inside Head Section
7
</title>
8
9
<script>
10
function myFun() {
11
document.getElementById("demo")
12
.innerHTML = "Content changed!";
13
}
14
</script>
15
</head>
16
17
<body>
18
<h2>
19
Add JavaScript Code
20
inside Head Section
21
</h2>
22
23
<h3 id="demo" style="color:green;">
24
GeeksforGeeks
25
</h3>
26
27
<button type="button" onclick="myFun()">
28
Click Here
29
</button>
30
</body>
31
32
</html>
Output
2. JavaScript
Code Inside <body> Tag
JavaScript can also be placed inside the <body> section of an HTML
page. Typically, scripts placed at the end of the <body> load after
the content, which can be useful if your script depends on the DOM
being fully loaded.
Example: This example shows showing the addition of a script file
inside the body section.
HTML
1
<!DOCTYPE html>
2
<html>
3
4
<head>
5
<title>
6
Add JavaScript Code inside Body Section
7
</title>
8
</head>
9
10
<body>
11
<h2>
12
Add JavaScript Code
13
inside Body Section
14
</h2>
15
16
<h3 id="demo" style="color:green;">
17
GeeksforGeeks
18
</h3>
19
20
<button type="button" onclick="myFun()">
21
Click Here
22
</button>
23
24
<script>
25
function myFun() {
26
document.getElementById("demo")
27
.innerHTML = "Content changed!";
28
}
29
</script>
30
</body>
31
32
</html>
Output
External
JavaScript (Using External File)
For larger projects or when reusing scripts across multiple HTML
files, you can place your JavaScript code in an external .js file. This
file is then linked to your HTML document using the src attribute
within a <script> tag.
Example: This example shows showing the linking of an external
script file inside the head section.
HTMLJavaScript
1
<!DOCTYPE html>
2
<html>
3
4
<head>
5
<title>
6
External JavaScript
7
</title>
8
<script src="script.js"></script>
9
</head>
10
11
<body>
12
<h2>
13
External JavaScript
14
</h2>
15
16
<h3 id="demo" style="color:green;">
17
GeeksforGeeks
18
</h3>
19
20
<button type="button" onclick="myFun()">
21
Click Here
22
</button>
23
</body>
24
25
</html>
Output:
JavaScript Syntax
Last Updated : 12 Aug, 2024
4
let str1 = "Geek"
5
let str2 = 'Geeks'
6
7
console.log(num1)
8
console.log(num2)
9
console.log(str1)
10
console.log(str2)
Output
50
50.05
Geek
Geeks
JavaScript Variables
A JavaScript variable is the simple name of the storage location
where data is stored. There are two types of variables in JavaScript
which are listed below:
Local variables: Declare a variable inside of a block or
function.
Global variables: Declare a variable outside function or
with a window object.
Example: This example shows the use of JavaScript variables.
JavaScript
1
// Declare a variable and initialize it
2
// Global variable declaration
3
let Name = "Apple";
4
5
// Function definition
6
function MyFunction() {
7
8
// Local variable declaration
9
let num = 45;
10
11
// Display the value of Global variable
12
console.log(Name);
13
14
// Display the value of local variable
15
console.log(num);
16
}
17
18
// Function call
19
MyFunction();
Output:
Apple
45
JavaScript Operators
JavaScript operators are symbols that are used to compute the value
or in other words, we can perform operations on operands.
Arithmetic operators ( +, -, *, / ) are used to compute the value, and
Assignment operators ( =, +=, %= ) are used to assign the values
to variables.
Example: This example shows the use of javascript operators.
JavaScript
1
// Variable Declarations
2
let x, y, sum;
3
4
// Assign value to the variables
5
x = 3;
6
y = 23;
7
8
// Use arithmetic operator to
9
// add two numbers
10
sum = x + y;
11
12
console.log(sum);
Output
26
JavaScript Expressions
Javascript Expression is the combination of values, operators, and
variables. It is used to compute the values.
Example: This example shows a JavaScript expression.
JavaScript
1
// Variable Declarations
2
let x, num, sum;
3
4
// Assign value to the variables
5
x = 20;
6
y = 30
7
8
// Expression to divide a number
9
num = x / 2;
10
11
// Expression to add two numbers
12
sum = x + y;
13
14
console.log(num + "\n" + sum);
Output
10
50
JavaScript Keywords
The keywords are the reserved words that have special meanings in
JavaScript.
// let is the keyword used to
// define the variable
let a, b;
4
// Assign value to the variables
5
x = 20;
6
y = 30
7
8
/* Expression to add two numbers */
9
sum = x + y;
10
11
console.log(sum);
Output
50
4
// Declare a variable
5
let num = 45;
6
7
// Display the result
8
console.log(num);
9
}
10
11
// Function call
12
func();
Output
45
JavaScript Identifiers
JavaScript Identifiers are names used to name variables and
keywords and functions.
A identifier must begin with:
A letter(A-Z or a-z)
A dollar sign($)
A underscore(_)
Note: Numbers are not allowed as a first character in JavaScript
Identifiers.
JavaScript Case Sensitive
JavaScript Identifiers are case-sensitive.
Example: Both the variables firstName and firstname are different
from each other.
JavaScript
1
let firstName = "Geek";
2
let firstname = 100;
3
4
console.log(firstName);
5
console.log(firstname);
Output
Geek
100