What is an object in JavaScript?
An object is a collection of key-value pairs used to represent related data and behavior. Property keys are strings or symbols, while values can be any JavaScript value, including functions and other objects.
JavaScript interview questions covering objects, properties, object methods, copying, arrays, collection methods, iteration, Map, Set and core data handling.
Open a question for the full answer, code and interview guidance.
An object is a collection of key-value pairs used to represent related data and behavior. Property keys are strings or symbols, while values can be any JavaScript value, including functions and other objects.
The most common approach is an object literal using braces. Objects can also be created with constructor functions, classes, Object.create, or the Object constructor.
Dot notation accesses a property with a fixed valid identifier, such as user.name. Bracket notation accepts an expression and is required when the property name is dynamic, contains characters that are invalid in an identifier, or is stored in a variable.
JavaScript objects are dynamic by default. A property can be added or updated by assigning to it with dot or bracket notation. The delete operator removes an own property when the property is configurable.
An object method is a function stored as an object property. Method shorthand allows the function keyword to be omitted inside an object literal.
For a regular function invoked as obj.method(), this normally refers to obj. However, this is not permanently attached to the function; its value depends on how the function is called.
Object.keys returns an array containing the object's own enumerable string-keyed property names. It does not include inherited properties or symbol keys.
Object.values returns an array of an object's own enumerable string-keyed property values. Object.entries returns an array of [key, value] pairs for those properties.
Object.assign copies enumerable own properties from one or more source objects to a target object and returns the target. Later sources overwrite earlier properties with the same key.
A shallow copy creates a new top-level object or array but keeps references to nested objects. Therefore, modifying a nested object through the copy can also affect the original.
Object.freeze prevents adding, deleting, or changing the object's own properties and makes those properties non-configurable. In strict mode, invalid mutation attempts can throw errors.
An array is an ordered, zero-indexed collection designed to store multiple values. JavaScript arrays are objects with special behavior for integer-like indexes and a length property.
Use Array.isArray(value). It reliably determines whether a value is an Array, including cases where typeof would only report "object".
push adds one or more elements to the end of an array, while pop removes and returns the last element. unshift adds elements to the beginning, while shift removes and returns the first element.
slice returns a shallow copy of a selected portion of an array without modifying the original. splice changes the original array by removing, replacing, or inserting elements.
map executes a callback for each element and returns a new array containing the callback results. forEach executes a callback for each element but returns undefined.
filter returns a new array containing only the elements for which the callback returns a truthy value. It does not modify the original array.
reduce processes an array and combines its elements into a single accumulated result. The result can be a number, string, object, array, map-like structure, or another value.
find returns the first element that satisfies the callback, or undefined when no element matches. filter returns a new array containing every matching element.
indexOf searches for a specific value using strict equality and returns its index, or -1 when not found. findIndex accepts a callback and returns the index of the first element satisfying the condition.
some returns true when at least one array element satisfies the callback. every returns true only when all elements satisfy it.
sort rearranges the original array in place. Without a compare function, elements are converted to strings and compared according to string ordering, which can produce unexpected results for numbers.
for...of iterates over values produced by an iterable such as an array, string, map, or set. for...in iterates over enumerable string property keys of an object, including inherited enumerable properties unless they are filtered.
Map is a key-value collection whose keys can be values of any type. Set is a collection of unique values. Both are iterable and provide dedicated methods such as set, get, has, add, and delete.
Both can represent key-value data, but Map accepts keys of any type, preserves insertion order for iteration, has a size property, and provides purpose-built methods such as get, set, has, and delete.