I was watching hitesh sir's interview video , I got to know that you can add new properties to arrays using prototypes, but these are not considered original properties. To display only the original properties of an array, you have to use the hasOwnProperty
method.
Array.prototype.customProperty = "Bro, I am a custom property";
let myArray = [1, 2, 3];
function displayOriginalProperties(array) {
for (let key in array) {
if (array.hasOwnProperty(key)) {
console.log(</mark>`` Original Property: ${key} = ${array[key]}
``)
} } }
displayOriginalProperties(myArray);
Output will be:
Original Property: 0 = 1
Original Property: 1 = 2
Original Property: 2 = 3
so we can say that by using hasOwnProperty
helps you display only the original properties of an array, excluding those added through prototypes.