Member-only story

How to overwrite JavaScript array inbuilt property

Pravin M
Dec 23, 2023

--

How to overwrite JavaScript array inbuilt property
How to overwrite JavaScript arrays inbuilt property

Source: How to overwrite JavaScript array inbuilt property

For more questions and answers visit our website at Frontend Interview Questions

In JavaScript, you can overwrite or modify built-in properties and methods of objects, including those of the Array prototype. However, it is generally not recommended to modify the behavior of built-in objects because it can lead to unexpected consequences and compatibility issues with other code.

That being said, if you still need to override a built-in property or method, you can achieve it by modifying the object’s prototype. Here’s an example of how you can override the push() method of the Array prototype:


Array.prototype.push=function() {
console.log(this);
}
var arr=[1,2,3];
arr.push(4);//it will console the array with values i.e [1,2,3] as push function has been overridden

--

--

Pravin M
Pravin M

Written by Pravin M

I am a frontend developer with 10+ years of experience Blog :- https://www.frontendinterviewquestions.com

No responses yet