function showHeroes(jsonObj) {
const heroes = jsonObj["members"];
for (const hero of heroes) {
// …
}
}JavaScript debugging and error handling / notebook
JavaScript debugging and error handling
Runnable notebook: every JavaScript, HTML and CSS example on this page executes in a sandboxed local frame. Nothing leaves the browser.
const requestURL =
"https://mdn.github.io/learning-area/javascript/oojs/json/superheroes.json";
const response = fetch(requestURL);
populateHeader(response);
showHeroes(response);const requestURL =
"https://mdn.github.io/learning-area/javascript/oojs/json/superheroes.json";
const response = fetch(requestURL);
console.log(`Response value: ${response}`);
populateHeader(response);
showHeroes(response);console.log(`Response value: ${response}`);console.error(`Response value: ${response}`);fetch(requestURL).then((response) => {
populateHeader(response);
showHeroes(response);
});<!doctype html>
<html lang="en-US">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width" />
<title>Handling JS errors</title>
</head>
<body>
<script>
// Code goes below this line
</script>
</body>
</html>function inchesToMeters(num) {
const mVal = (num * 2.54) / 100;
const m2dp = mVal.toFixed(2);
return m2dp;
} const height = 70; console.log(inchesToMeters(height)); function inchesToMeters(num) {
if (typeof num !== "number" || Number.isNaN(num)) {
console.log("A number was not provided. Please correct the input.");
return undefined;
}
const mVal = (num * 2.54) / 100;
const m2dp = mVal.toFixed(2);
return m2dp;
} throw new Error("A number was not provided. Please correct the input.");try {
// Run some code
} catch (error) {
// Handle any errors
} try {
console.log(inchesToMeters(height));
} catch (error) {
console.error(error);
console.log("Insert code to handle the error");
}if ("geolocation" in navigator) {
navigator.geolocation.getCurrentPosition((position) => {
// show the location on a map, perhaps using the Google Maps API
});
} else {
// Give the user a choice of static maps instead
}