programming-examples/js/Array/Write a JavaScript function to get the first element of an array. Passing a parameter 'n' will return the first 'n' elements of the array..js

15 lines
404 B
JavaScript
Raw Normal View History

2019-11-15 12:59:38 +01:00
first = function(array, n) {
if (array == null)
return void 0;
if (n == null)
return array[0];
if (n < 0)
return [];
return array.slice(0, n);
};
console.log(first([7, 9, 0, -2]));
console.log(first([],3));
console.log(first([7, 9, 0, -2],3));
console.log(first([7, 9, 0, -2],6));
console.log(first([7, 9, 0, -2],-3));