return value out of for-loop simple callback helper function

I am trying to mach a value against an array inside a callback function. If it matches, return true out of that callback function & break the for-loop as soon as possible instead of continuing the loop for all values.

function arrayLoop(dataArray, callback){
  for(var i=0; i < data.length; i++){
      callback(dataArray[i]);
  }
}

Here is demo test

var arr = = [6,1,4,9,2];
var test = arrayLoop(arr, function(item){
    if (item==4) return true;
});
console.log(test); //it should show "true" in console.

Answer

If you want it to break and return only when a truthy value is returned from the callback, this should be possible:

function arrayLoop(dataArray, callback){
  for(var i=0; i < dataArray.length; i++){
      console.log(`iteration ${i}`);
      let result = callback(dataArray[i]);
      if (result) {
        console.log('done!');
        return result;
      }
  }
}

function isFour(item){
    if (item==4) return true;
}


var arr = [6,1,4,9,2];
var test = arrayLoop(arr, isFour);

console.log(test); //it should show "true" in console.

However, I am not sure there is much value in this; as many of the comments stated, there are probably native ways for you to achieve what you are trying to do in a cleaner, more performant manner. For instance, if all you are trying to do is evaluate if a certain item is in an array Array.prototype.includes would be a better approach:

var arr = [6,1,4,9,2];
var test = arr.includes(4);

console.log(test);