I need to implement a JavaScript function that can determine if a file exists on a web server by using the fetch() method.
Below is my current code snippet:
let result = checkFile("index.html");
console.log("typeof(result) = " + typeof(result));
async function checkFile(file) {
try {
let response = await fetch(file);
if (!response.ok) {
throw new Error("Network response was not OK");
} else {
return true;
}
}
catch {
return false;
}
}
After running the function, I noticed that it outputs the following message in the browser console:
"typeof(result) = object"
Why is the output not simply true or false?