Check whether two JavaScript arrays are equal, using basic library only I am writing a function that takes in two arrays and a test name and outputs statements (console.log) saying whether the two arrays are equal. I don't want to convert the arrays to strings or use non-core libraries to do this. I am also new to writing JS so I wanted to get a code review here.

Usage: FAILURE CASE:
var expected = [1, 1, 2, 3, 5, 8, 13];
var actual = [2, 3, 5, 8, 13, 21, 34]; 
assertArraysEqual(actual, expected, 'generates first 7 Fibonacci 
numbers');

console output:
 FAILED [generates first 7 Fibonacci numbers] Expected "1, 1, 2, 3, 5, 
8, 13", but got "2, 3, 5, 8, 13, 21, 34"




function assertArraysEqual(actual, expected, testName) {

  var lengthsEqual = (actual.length === expected.length)
  var contentsEqual = true
  for (var i=0; i&lt;actual.length; i++)
  {
    if (actual[i] !== expected[i]) contentsEqual=false
  }
  if(contentsEqual &amp;&amp; lengthsEqual){
   console.log("passed")
  }
  else{
    console.log('FAILED [' + testName + '] Expected "' + expected + '", but got "' + actual + '"');
  }
}
Your function should be dedicated to its functionality. Have all the printing done externally. You don't need testName as a parameter.
Have your function return true or false. It's much more useful and reusable that way. Having it return also allows you to exit the function early if you know the arrays are not equal. For example:

function assertArraysEqual(actual, expected) {
    if (actual.length !== expected.length) {
        return false;
    }
    ...

If this is for something like unit testing, you should use a unit test library to like Mocha or Qunit to validate the behavior of the function.
What data types are you dealing with? Are you only concerned with numbers? Do you want strict type checking and/or null checking?


If you're only concerned with validating arrays of numbers and you know the bounds of the input, you can easily rewrite your function like so:

    function assertArraysEqual(actual, expected) {
        if (actual.length !== expected.length) {
            return false;
        }
        for (var i = 0; i &lt; actual.length; ++i) {
            if (actual[i] !== expected[i]) {
                return false;
            }
        }
        return true;
    }