Jaba Part 2
Jaba Part 2
advanced concepts.
---
---
Table of Contents
6. Error Handling
9. Fetch API
---
Functions
// Function Declaration
function greet(name) {
return `Hello, ${name}!`;
}
// Function Call
console.log(greet("Alice")); // Output: Hello, Alice!
Scope
Scope determines where variables are accessible.
function example() {
let localVar = "I am local";
console.log(globalVar); // Accessible
console.log(localVar); // Accessible
}
example();
console.log(localVar); // Error: localVar is not defined
---
Arrays
// For loop
for (let i = 0; i < numbers.length; i++) {
console.log(numbers[i]);
}
// ForEach method
numbers.forEach(num => console.log(num));
---
The Document Object Model (DOM) allows JavaScript to interact with the HTML
structure.
Selecting Elements
Changing Content
Adding Elements
---
button.addEventListener("click", () => {
alert("Button clicked!");
});
---
Parsing JSON
Stringifying JSON
---
6. Error Handling
try {
let result = riskyFunction();
console.log(result);
} catch (error) {
console.error("An error occurred:", error.message);
}
---
if (success) {
resolve("Operation successful");
} else {
reject("Operation failed");
}
});
promise
.then(response => console.log(response))
.catch(error => console.error(error));
Async/Await
fetchData();
---
Arrow Functions
Template Literals
Destructuring
---
9. Fetch API
fetch("https://api.example.com/data")
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error("Error:", error));
---
Would you like this content saved as a PDF? Let me know, and I’ll generate it for
you!