The Array Prototype Last interview question focuses on extending the built-in functionality of the Array object in JavaScript. You are asked to add a new method, .last(), to the Array prototype. This method should return the last element of the array if the array is not empty, and -1 if the array is empty. This Array Prototype Last coding problem is a fundamental exercise in understanding JavaScript's prototype-based inheritance and the this keyword.
Big tech companies like Google, Meta, and Microsoft use this question to evaluate a candidate's core knowledge of JavaScript. It tests whether you understand how to modify global objects safely and how context (the this binding) works within prototype methods. It's a simple task that reveals if you have a deep understanding of the language's architecture beyond just using modern frameworks.
This doesn't involve complex algorithms but rather a direct property access pattern. You check the length of the array (this.length) and return the element at this.length - 1 or a default value. It follows the "Safe Access" pattern, ensuring the code doesn't crash when acting on empty data.
Imagine you have two arrays:
Always use regular function syntax (not arrow functions) when adding methods to prototypes if you need to access the calling object via this. Practice extending other built-in objects like String or Number to get comfortable with this pattern.