Q: how to extend array.push() in javascript such that after calling push method, element should not get pushed into the array? Example:
var animals = ['pigs', 'goats', 'sheep'];

console.log(animals.push('cows'));
// expected output: 4

console.log(animals);
// expected output: Array ["pigs", "goats", "sheep", "cows"]

animals.push('chickens');

//I want output such that, when i will push 'chickens' it should not get 
pushed into the array.
console.log(animals);
// expected output: Array ["pigs", "goats", "sheep", "cows"]


A: 
my requirement is like that when i will call push method it should
  print "hii" but should not push any element

Not a good idea, but you can always override the existing push function by doing
Array.prototype.push = function(){ console.log("hii") };

Demo


var arr = [1];
console.log(1,arr); //[1]

Array.prototype.push = function(){console.log("hii") };
arr.push(2); //hii
console.log(2,arr); //hii //[1]


Note


*

*You'll notice that push method is being called (internally) when the array is initialized and when its value is printed on console.


A: From your situation the problem should me somewhere else because if you would modify native function, libraries that use it may start to return some weird values. but if you really want to do this, I suggest you wrap .push function with your own one using Array.prototype. You can read more about this https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/prototype
