2nd Unit
2nd Unit
1. Element Selection: Uses CSS-style selectors via $(selector) to select DOM
elements easily.
2. Cross-Browser Compatibility: Abstracts away browser differences in how the DOM
is accessed and manipulated.
5. Animations and Effects: Offers built-in functions like .hide(), .fadeIn(), and
.slideToggle().
Example:
Vanilla JavaScript:
javascript
CopyEdit
document.getElementById("myButton").addEventListener("click",
function() {
document.getElementById("myText").style.color = "red";
});
jQuery Equivalent:
javascript
CopyEdit
$("#myButton").click(function() {
$("#myText").css("color", "red");
});
4) What is AJAX, and how is it used for server communication? Provide a practical
example
AJAX (Asynchronous JavaScript and XML) is a technique that allows web pages to
communicate with a server without reloading the entire page. It enables the page to
request and receive data in the background, making web applications faster and more
dynamic.
● Asynchronous: Does not interrupt the user's interaction with the page.
● Lightweight: Only necessary data is exchanged, not the entire HTML page.
● Flexible: Can exchange data in formats like JSON, XML, HTML, or plain text.
4. JavaScript handles the response and updates the DOM accordingly.
HTML:
html
CopyEdit
<button id="getData">Get Data</button>
<div id="result"></div>
JavaScript (jQuery):
javascript
CopyEdit
$("#getData").click(function() {
$.ajax({
url: "https://api.example.com/data", // Server endpoint
method: "GET",
success: function(response) {
$("#result").html("Received: " + response);
},
error: function() {
$("#result").html("An error occurred.");
}
});
});
● When the button is clicked, an AJAX GET request is sent to the server.
● If there's an error (e.g., server not responding), an error message is shown instead.
5) How do you handle errors in JavaScript? Provide examples of `try`, `catch`, and
`finally` blocks.
In JavaScript, errors are handled using try...catch...finally blocks. These allow you
to test code for errors (try), handle any exceptions (catch), and run cleanup code
(finally) regardless of whether an error occurred.
Syntax Overview:
javascript
CopyEdit
try {
// Code that may throw an error
} catch (error) {
// Code to handle the error
} finally {
// Code that will run regardless of the result
}
Practical Example:
javascript
CopyEdit
function divide(a, b) {
try {
if (b === 0) {
throw new Error("Division by zero is not allowed");
}
let result = a / b;
console.log("Result:", result);
} catch (err) {
console.error("Error:", err.message);
} finally {
console.log("Division attempt completed.");
}
}
● catch: Catches and handles the error, preventing the program from crashing.
● finally: Runs no matter what — useful for closing resources, clearing states, or
logging.
// Example usage:
let number = 5;
console.log("Factorial of", number, "is", factorial(number)); //
Output: 120
// Example usage:
let number = 5;
console.log("Factorial of", number, "is", factorial(number)); //
Output: 120
Both methods are valid; recursion is more elegant but may be less efficient for very
large numbers due to call stack limits.
In simpler terms:
● They enable function factories (functions that return functions with preset
behavior).
🔧 Example:
javascript
CopyEdit
function outerFunction() {
let count = 0;
counter(); // Count: 1
counter(); // Count: 2
counter(); // Count: 3
Explanation:
7) What are jQuery events? Write a jQuery code snippet to handle a button click event.
jQuery provides a simple and consistent way to handle events in web development. Events
are
actions that occur in the browser, like user interactions (clicking a button, moving the mouse,
typing text), or other occurrences (page load, focus change). With jQuery, you can easily
manage
these events and define actions that should occur when they are triggered.
html
CopyEdit
<button id="myButton">Click Me</button>
<p id="message"></p>
jQuery:
javascript
CopyEdit
$(document).ready(function() {
$("#myButton").click(function() {
$("#message").text("Button was clicked!");
});
});
Explanation: