res.send displayed the wrong result but console.log give to the same response the correct result

I’ve been trying to display the response by using res.send(), but it throws me a wrong result, but when console.logging that same response it gives me the correct response (which is not expected).

Here’s the code:

var alldata = wc2014.rounds.concat(wc2018.rounds);
   
    for(i = 0; i < alldata.length; i++){
      var a = alldata[0].matches;
      var b = alldata[1].matches;
      var result = a.concat(b);
    }

   
   for (var key in result) {
      if(result[key].goals1.length > result[key].goals2.length){
        var team1 = result[key].team1.name; 
        console.log(team1);
      }
      else{
        var team2 = result[key].team2.name;
        console.log(team2); 
      }
    }  

    res.send(team1 || team2);

console.log

Germany
France

res.send()

France

Answer

Try this outside of your “for” loop :

console.log(team1)
console.log(team2)
console.log(team1 || team2)

The rsult would be :

France
Germany
France

It means team1 value is France and team2 also Germany “(team1 || team2) ” => if team1 is not empty it’s equal to team1 If team1 is empty or any falsy value the statement is equal to team2 And because team1 has a value(France) the res.send (team1 || team2) Is like res.send(team1) and it will send “France” as a response.