i passed this code but i dont understand this. It says (If responseFromServer is true) shouldnt the code be if(responseFromServer === true) { ? if not please explain im a lil confused Your code so far
const makeServerRequest = new Promise((resolve, reject) => {
// responseFromServer represents a response from a server
let responseFromServer;
if(responseFromServer) {
resolve("We got the data") // Change this line
} else {
reject("Data not received")// Change this line
}
});
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36
Challenge: ES6 - Complete a Promise with resolve and reject
The if does not test if something is true or false, it checks if they are truthy or falsy. In JS, the falsy values are undefined, null, 0, -0, 0n, NaN, false, and an empty string. Everything else is truthy.
Since the value tested is boolean, JS knows that do the โifโ if the value is true and to do the โelseโ otherwise.
There may be some cases where you explicitly want to test === true - like if you are not 100% confident that it is boolean - but most of the time, a truthy check is sufficient.