To keep the first ten items:
if (theArray.length > 10) theArray = theArray.slice(0, 10);
or, perhaps less intuitive:
if (theArray.length > 10) theArray.length = 10;
To keep the last ten items:
if (theArray.length > 10) theArray = theArray.slice(theArray.length - 10, 10);
You can use a negative value for the first parameter to specify length – n, and omitting the second parameter gets all items to the end, so the same can also be written as:
if (theArray.length > 10) theArray = theArray.slice(-10);
The splice method is used to remove items and replace with other items, but if you specify no new items it can be used to only remove items. To keep the first ten items:
if (theArray.length > 10) theArray.splice(10, theArray.length - 10);
To keep the last ten items:
if (theArray.length > 10) theArray.splice(0, theArray.length - 10);