0% found this document useful (0 votes)
6 views11 pages

Yash FSD Part 2

The document outlines various JavaScript experiments focusing on functions, error handling, and DOM manipulation. It includes activities such as defining functions, understanding function scope, and using try-catch for error handling. Additionally, it covers the importance of debugging and adding event listeners in JavaScript.

Uploaded by

Yash Rao
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views11 pages

Yash FSD Part 2

The document outlines various JavaScript experiments focusing on functions, error handling, and DOM manipulation. It includes activities such as defining functions, understanding function scope, and using try-catch for error handling. Additionally, it covers the importance of debugging and adding event listeners in JavaScript.

Uploaded by

Yash Rao
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Experiment No-4 DATE:8-10-2024

AIM OF THE EXPERIMENT: Introduction to JavaScript Function

Activity 1: Defining and Invoking Functions

Task: Define a simple function named greet() that takes a name as a parameter
and logs a greeting message to the console.

SOURCE CODE:

function greet(name) {
console.log(`Hello, ${name}! Welcome!`);
}

// Invoking the function


greet('yash');

output:

Activity 2: Parameters and Return Values

Task: Create a function called add that takes two numbers as parameters and
returns their sum

SOURCE CODE:
function add(num1, num2) {

return num1 + num2;

// Example of invoking the function

13
let sum = add(5, 10);

console.log(`The sum is: ${sum}`);

OUTPUT:

Activity 3: Function Scope

Task: Explain the concept of scope in JavaScript. Create a function that


demonstrates local and global scope.

SOURCE CODE:
let globalVar = "hello sir mukul sir"; // Global variable

function demoScope() {

let localVar = "sir thode badya number dena"; // Local variable

console.log(globalVar); // Accessing global variable

console.log(localVar); // Accessing local variable

demoScope();

OUTPUT:

14
Activity 4: Higher-Order Functions

Task: Write a function named filter that takes an array and a callback function.
It should return a new array containing elements that satisfy the condition
defined in the callback.

SOURCE CODE:
function filter(array, callback) {

let result = [];

for (let i = 0; i < array.length; i++) {

if (callback(array[i])) {

result.push(array[i]);

return result;

// Example callback function

function isEven(num) {

return num % 2 === 0;

// Using the filter function

let numbers = [1, 2, 3, 4, 5, 6];

let evenNumbers = filter(numbers, isEven);

console.log(evenNumbers);

OUTPUT:

15
Activity 5: Function Expressions and Anonymous Functions

Task: Explain function expressions and demonstrate how to use them. Create a
function expression that logs a message to the console.

SOURCE CODE:
let logMessage = function() {

console.log("sir,attendance dhek lena");

};

// Invoking the function expression

logMessage();

OUTPUT

16
Experiment No-5.1 DATE:08-10-24

Activity 1: Identifying Different Types of Errors

Task: Discuss the common types of errors in JavaScript, such as:


○ Syntax Errors
○ Reference Errors
○ Type Errors
○ Range Errors

source code

// Syntax Error

// Missing a closing parenthesis or curly brace.

console.log("Hello im yash"); // Fixed: Added closing parenthesis and semicolon

// Reference Error

// Trying to use a variable that has not been defined.

let definedVariable = "I am defined";

console.log(definedVariable); // Fixed: Defined the variable before using it

// Type Error

// Trying to call a non-function as a function or using incorrect data types.

let num = 5;

// Fixed: Ensure num is not called as a function

console.log(num); // Displays the value of num instead of calling it as a function

// Range Error

// Creating an array with an invalid length.

try {

let arr = new Array(5); // Fixed: Changed array length to a valid positive integer

17
console.log("Array created successfully", arr);

} catch (error) {

console.error("Error:", error.message);

Activity 2: understanding error mesages

TASK: Explain how to read and understand error messages in the console. Highlight key
components such as:
○ Type of error
○ Location (file name and line number)
○ Error description

source code

// Syntax Error

try {

console.log("Hello World"); // Fixed: Added closing parenthesis and semicolon

} catch (error) {

console.error("Syntax Error:", error.message);

// Reference Error

try {

18
console.log(undefinedVariable); // Trying to use a variable that has not been defined

} catch (error) {

console.error("Reference Error:", error.message);

// Type Error

try {

let num = 5;

num(); // Attempting to call a non-function as a function

} catch (error) {

console.error("Type Error:", error.message);

// Range Error

try {

let arr = new Array(-1); // Creating an array with an invalid length

} catch (error) {

console.error("Range Error:", error.message);

Activity 3: using try-catch for error handling

19
TASK; Introduce the concept of try-catch blocks for handling errors gracefully in
JavaScript

source code

try {

// Code that may cause an error

let result = num.toUpperCase(); // This will cause a TypeError

} catch (error) {

console.error("An error occurred:", error.message); // Handle the error gracefully

Activity 4:IMPLEMENT custom error


TASK:How to throw custom errors using the throw statement

source code

function checkAge(age) {
if (age < 18) {
throw new Error("User must be at least 18 years old.");
}
return "Access granted.";
}

try {

20
console.log(checkAge(16)); // Throws a custom error
} catch (error) {
console.error("Custom error:", error.message);
}

Activity 5 debugging with consolve

TASK:Explain the importance of debugging and how to use console.log() effectively

to identify issues in the code.

source code
function add(a, b) {

console.log("Function called with arguments:", a, b); // Logs values of a and b

return a + b;

console.log(add(5, 10)); // You’ll see "Function called with arguments: 5, 10" in the console

21
Experiment No-6.1 DATE:22-10-24
AIM OF THE EXPERIMENT: Add JavaScript for DOM Manipulation

Figure 6.1.1 HTML Structure

Figure 6.1.2 JavaScript for DOM Manipulation

Figure 6.1.3 After click Chanded

Figure 6.1.4 changed

22
Experiment No-6.2 DATE:22-10-24
AIM OF THE EXPERIMENT: Adding Event Listeners in JavaScript

Figure 6.2.1 DOM In line HTML

Figure 6.2.2 After Click Changed

Figure 6.2.3 Changed

23

You might also like