ES6 - Complete a Promise with resolve and reject

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

Link to the challenge:

In this case

if(responseFromServer) {

and

if(responseFromServer === true) {

are functionally equivalent.

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.

Does that makes sense?

1 Like

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.

it does, thank your for making it more simple ^.^

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.