Web Programming Lab Manual 06 JavaScript (Advance) (1)
Web Programming Lab Manual 06 JavaScript (Advance) (1)
2 Problem analysis
The objective of this lab manual is to introduce students to advanced concepts in JavaScript that go beyond the
foundational knowledge of variables, functions, and events. These advanced concepts include ECMAScript 6
(ES6) features, asynchronous programming, closures, prototypes, JavaScript modules, and DOM manipulation.
The challenge is to provide a structured learning path that enables students to understand and practically apply
these advanced topics in real-world web development scenarios.
The manual also requires students to handle asynchronous tasks, interact with APIs, and manipulate the
Document Object Model (DOM) dynamically. These are essential skills for modern JavaScript development
but can be difficult for students transitioning from basic programming concepts.
1 <!DOCTYPE html>
2 <html>
3
4 <body>
5
6 <script>
7 const sum = (a, b) => a + b;
8 console.log(sum(5, 10)); // Output: 15
9 </script>
10
11 </body>
12
13 </html>
1 <!DOCTYPE html>
2 <html>
3
4 <body>
5
6 <script>
7 const name = ’John’;
8 console.log(‘Hello, ${name}!‘); // Output: Hello, John!
9 </script>
10
11 </body>
12
1 <!DOCTYPE html>
2 <html>
3
4 <body>
5
6 <script>
7 function greet(name = ’Guest’) {
8 return ‘Hello, ${name}‘;
9 }
10 console.log(greet()); // Output: Hello, Guest
11 console.log(greet(’Alice’)); // Output: Hello, Alice
12 </script>
13
14 </body>
15
16 </html>
1 <!DOCTYPE html>
2 <html>
3
4 <body>
5
6 <script>
7 const [x, y] = [1, 2];
8 console.log(x, y); // Output: 1 2
9
10 const { name, age } = { name: ’John’, age: 30 };
11 console.log(name, age); // Output: John 30
12 </script>
13
14 </body>
15
16 </html>
1 <!DOCTYPE html>
2 <html>
3
4 <body>
5
6 <script>
7 const promise = new Promise((resolve, reject) => {
8 let success = true; // Modify to test resolve/reject
9 if (success) {
10 resolve(’Operation successful’);
11 } else {
12 reject(’Operation failed’);
3.2.2 Async/Await
1 <!DOCTYPE html>
2 <html>
3
4 <body>
5
6 <script>
7 async function fetchData() {
8 try {
9 const response = await fetch(’https://jsonplaceholder.typicode.
com/posts/1’);
10 const data = await response.json();
11 console.log(data);
12 } catch (error) {
13 console.error(’Error:’, error);
14 }
15 }
16 fetchData();
17 </script>
18
19 </body>
20
21 </html>
1 <!DOCTYPE html>
2 <html>
3
4 <body>
5
6 <script>
7 setTimeout(() => console.log(’Hello after 1 second’), 1000);
8 console.log(’This runs first’);
9 </script>
10
11 </body>
12
13 </html>
3.3 Closures
1 <!DOCTYPE html>
2 <html>
3
4 <body>
5
6 <script>
7 function Person(name) {
8 this.name = name;
9 }
10
11 Person.prototype.greet = function () {
12 return ‘Hello, ${this.name}‘;
13 };
14
15 const john = new Person(’John’);
16 console.log(john.greet()); // Output: Hello, John
17 </script>
18
19 </body>
20
21 </html>
1 <!DOCTYPE html>
2 <html>
3
4 <body>
5
6 <script type="module">
1 <!DOCTYPE html>
2 <html>
3
4 <body>
5
6 <script>
7 fetch(’https://jsonplaceholder.typicode.com/posts/1’)
8 .then(response => response.json())
9 .then(data => console.log(data))
10 .catch(error => console.error(’Error:’, error));
11 </script>
1 <!DOCTYPE html>
2 <html>
3
4 <body>
5
6 <script>
7 fetch(’https://jsonplaceholder.typicode.com/posts/1’)
8 .then(response => response.json())
9 .then(data => {
10 console.log(’First fetch:’, data);
11 return fetch(’https://jsonplaceholder.typicode.com/users/1’);
12 })
13 .then(userResponse => userResponse.json())
14 .then(user => console.log(’Second fetch:’, user))
15 .catch(error => console.error(’Error:’, error));
16 </script>
17
18 </body>
19
20 </html>
5 Input/Output
Output of the program is given below.
7 Lab Task (Please implement yourself and show the output to the
instructor)
Write the following JavaScript inside a <script> tag:
7.1 Task 1:
1. Create an asynchronous function using async/await that fetches data from an API.
2. Manipulate the DOM by dynamically creating new HTML elements based on the fetched data.
7.2 Task 2:
1. Implement a JavaScript program to count and display the items of a dropdown list, in an alert window.
2. Implement a JavaScript program to change the attribute value of image tag.
9 Policy
Copying from internet, classmate, seniors, or from any other source is strongly prohibited. 100% marks will be
deducted if any such copying is detected.