In the following JavaScript code, why does it throw a ReferenceError, and how would you fix it?
3Times asked
Apr 2026Last seen
Apr 2026First seen
💡 Model Answer
The error occurs because array_push is not a built‑in JavaScript function. In JavaScript, arrays have a method called push that adds elements to the end of the array. The correct code should be:
js
const arr = [];
arr.push('abc');
console.log(arr); // ['abc']If you need to mimic PHP’s array_push, you can create a helper:
js
function array_push(arr, ...items) {
return arr.push(...items);
}But using the native push is simpler and more idiomatic. The ReferenceError is thrown because the interpreter cannot find a variable or function named array_push in the current scope.
This answer was generated by AI for study purposes. Use it as a starting point — personalize it with your own experience.
🎤 Get questions like this answered in real-time
Assisting AI listens to your interview, captures questions live, and gives you instant AI-powered answers — invisible to screen sharing.
Get Assisting AI — Starts at ₹500