0

I have the following variables which are arrays:

const gumBrands = ['orbit', 'trident', 'chiclet', 'strident'];

const mintBrands = ['altoids', 'certs', 'breath savers', 'tic tac'];

Below I have the following function that uses the variables as input arguments:

function shallowCopy (arrOne, arrTwo) {

    if (arrOne.constructor === 'Array'){

        return [...arrOne, ...arrTwo]; 
    }

    else {
        console.log('test this'); 
    }
}


shallowCopy(gumBrands, mintBrands)

I am expecting my code to return:

[ 'orbit',
  'trident',
  'chiclet',
  'strident',
  'altoids',
  'certs',
  'breath savers',
  'tic tac' ]

Instead the code runs my else statement and returns: test this

What am I doing wrong?

PineNuts0
  • 4,740
  • 21
  • 67
  • 112

1 Answers1

0

.constructor does not contain the string "Array" but a reference to the global Array object.

Note that arrays can be subclassed, and their .constructor is different. You might want to consider to check with instanceof Array.

Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151