SEO BRIEF — delete before publish | KW: javascript arrays | Slug: /javascript-arrays-objects/ | Secondary: javascript last element in array, javascript find object in array, remove undefined from array javascript, check if empty object javascript, array average javascript
Arrays and objects are the primary data structures in JavaScript. Most runtime bugs and awkward code come from not knowing the right method for the job. This is a practical reference covering the operations you reach for most often — with direct examples, no filler.
Get the Last Element of an Array
ES2023 added Array.prototype.at(): arr.at(-1) returns the last element. Negative indices count from the end.
Older approach: arr[arr.length – 1]. Both work. at() is cleaner and handles edge cases (empty array returns undefined) consistently.
Remove Undefined From an Array

arr.filter(Boolean) removes all falsy values including undefined, null, 0, and empty strings. If you want to remove only undefined: arr.filter(item => item !== undefined).
Be precise about what you actually want to remove. filter(Boolean) catches more than undefined — use the explicit check if zeros or empty strings are valid values in your array.
Find an Object in an Array
arr.find(item => item.id === targetId) returns the first matching element or undefined. arr.findIndex(item => item.id === targetId) returns the index instead.
arr.filter(item => item.active === true) returns all matching elements as a new array. Use find when you expect one result, filter when you expect multiple.
Check If an Object Is Empty
Object.keys(obj).length === 0 returns true if the object has no own enumerable properties. This is the most reliable general check.
Be aware: Object.keys does not include inherited properties or non-enumerable properties. For plain data objects this check is correct. For class instances with methods, you may need a different approach.
Calculate the Average of an Array
const avg = arr.reduce((sum, n) => sum + n, 0) / arr.length. The reduce accumulates the sum, divide by length for the average.
Guard against empty arrays: arr.length === 0 ? 0 : arr.reduce(…) / arr.length — dividing by zero returns Infinity in JavaScript, not an error.
Array unshift — Add Elements to the Front
arr.unshift(item) adds one or more elements to the beginning of an array and returns the new length. It mutates the original array.
For immutable patterns: [newItem, …arr] creates a new array with the item prepended without mutating the original.
keyBy — Index an Array of Objects by a Property
Lodash’s _.keyBy(arr, ‘id’) transforms an array into an object keyed by a property value — useful for O(1) lookups. Without Lodash: arr.reduce((acc, item) => ({ …acc, [item.id]: item }), {}).
This pattern converts [{id:1,name:’a’},{id:2,name:’b’}] into {1:{id:1,name:’a’}, 2:{id:2,name:’b’}}. Lookups by id become map[1] instead of arr.find(x => x.id === 1).
Frequently Asked Questions
How do I get the length of an array in JavaScript? arr.length returns the number of elements. It is a property, not a method — no parentheses.
What is the difference between map, filter, and reduce? map transforms every element and returns a new array of the same length. filter selects elements matching a condition and returns a new shorter array. reduce accumulates a single value from the array.
How do I convert a JSON array string to a JavaScript array? JSON.parse(jsonString) converts a JSON string to a JavaScript value. If the JSON represents an array, the result is an array.
How do I check if something is an array in JavaScript? Array.isArray(value) returns true if the value is an array. typeof value === ‘object’ is not reliable — it returns object for both arrays and plain objects.
