JavaScript Learn Before React.js
JavaScript Learn Before React.js
Explanation:
Variables store values (let, const are preferred over var due to scope rules).
Different data types are used based on the kind of value.
Conditional statements control the flow of the program.
How to Understand:
2. Functions
Explanation:
How to Understand:
document.getElementById("myButton").addEventListener("click", () => {
document.getElementById("myText").textContent = "Button Clicked!";
});
Explanation:
How to Understand:
Look for event triggers and observe how they alter the DOM.
Use browser dev tools to inspect changes.
4. ES6+ Features
Explanation:
How to Understand:
Explanation:
How to Understand:
6. Error Handling
Example: Try-Catch
try {
let result = 10 / 0;
if (!isFinite(result)) throw new Error("Cannot divide by zero");
} catch (error) {
console.error(error.message); // Outputs: Cannot divide by zero
}
Explanation:
try runs code that might fail, and catch handles the error gracefully.
How to Understand:
const obj = { a: 1, b: 2 };
console.log(Object.keys(obj)); // ['a', 'b']
Explanation:
8. JSON
Explanation:
How to Understand:
9. Browser APIs
Example: LocalStorage
localStorage.setItem("user", "John");
console.log(localStorage.getItem("user")); // Outputs: John
Explanation:
How to Understand:
function findMax(arr) {
return Math.max(...arr);
}
console.log(findMax([1, 2, 3])); // Outputs: 3
Explanation:
How to Understand:
Break the problem into smaller steps and solve each step.
11. Tooling
How to Understand:
By practicing these examples and understanding their logic, you'll build a strong JavaScript
foundation to begin learning React.js.
Fetching data from an API using JavaScript can be done using the Fetch API, which is a
modern and versatile way to make HTTP requests. Here's a step-by-step guide to fetch data
and understand how it works:
Code Example:
fetch("https://jsonplaceholder.typicode.com/posts")
.then(response => {
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
return response.json(); // Parse JSON data
})
.then(data => {
console.log(data); // Log fetched data
})
.catch(error => {
console.error("Error fetching data:", error); // Handle errors
});
Explanation:
2. Using Async/Await
Code Example:
Explanation:
1. async/await: Simplifies working with asynchronous operations.
2. try...catch: Handles both fetch errors and parsing errors.
3. await: Waits for the fetch and response.json() to complete.
Code Example:
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
Explanation:
1. Break it Down:
o Identify the URL being fetched.
o Understand the request method (GET, POST, etc.).
o See how the response is handled (e.g., JSON parsing).
2. Run and Observe:
o Run the code in a browser console or an environment like CodeSandbox to see
the results.
3. Experiment:
o Change the URL or request data to observe different outcomes.
4. Debug:
o Use console.log to log intermediate steps like the raw response and parsed
data.
o Use browser dev tools (F12) to inspect network requests under the "Network"
tab.
Code Example:
displayPosts();
HTML to Use:
<div id="postContainer"></div>
Explanation:
By following these steps, you’ll be able to fetch data from APIs, handle errors, and even
interact with the DOM to display the data!