JS returs a false negative when evaluating two strings on the first run only [duplicate]

I am tring to write a program to work with speech recognition but my program returns a false negative on its first run when evaluating two strings that are identical and should return true but they returned a false on the first try.

the function is fired after the speech recognition returns a Result.

var textResult = "text string";
var modeCommands = ['text', 'text one', 'text two'];
var command = null;

function loop(textResult, modeCommands, command) {
  console.log('loop function:', textResult, "modeCommands", modeCommands, "command", command);
  var commandPieces;
  const ObjectKeys = Object.keys(modeCommands);
  var resultPieces = textResult.split(' ');
  for (var i = 0; i < ObjectKeys.length; i++) {
    const arr = modeCommands[ObjectKeys[i]];
    for (var j = 0; j < arr.length; j++) {
      commandPieces = arr[j].split(' ');
      if (!command) {
        // the false negative happens on this evaluation 
        console.log('Comparing: resultPieces[0]', resultPieces[0].toString().toLowerCase().trim(), 'and commandPieces[0]', commandPieces[0].toString().toLowerCase().trim());
        if (resultPieces[0].toString().toLowerCase().trim() == commandPieces[0].toString().toLowerCase().trim()) {
          command = 'Success YaY';
        }
      }
    }
  }
  return command
}
var Result = loop(textResult, modeCommands, command);

// loop is supposed to return a string 'Success YaY' but when the user talks into the mic and speech recognition returns a result, and this function runs as a result of that, the evaluation of the two strings above returns a false negative for the first result returned aftecognition fires. after that everything works fine.
console.log('Result', Result)