How to define custom Array methods in JavaScript? Last Updated : 17 Dec, 2023 Comments Improve Suggest changes Like Article Like Report JavaScript arrays are versatile data structures used to store collections of items. When you want to iterate through the elements of an array, it's common to use loops like for, for...of, or forEach. However, there's a nuance when it comes to iterating through arrays that have additional properties added through their prototype. In this article, we will explore how to print only the original properties of an array, excluding any prototype properties, ensuring a clean and accurate output. Understanding the Prototype Properties:Before we dive into the solution, let's understand why arrays might include prototype properties in their iteration. In JavaScript, you can add properties or methods to the Array prototype. For instance: Array.prototype.myProperty = function() { console.log("Hello");}If you have an array myArr with elements 10, 15, and 20 and you iterate over it, you'll notice that it prints the additional myProperty along with the array elements: Example: JavaScript Array.prototype.myProperty = function() { console.log("Hello"); } const myArr = [10, 15, 20] for (let ele in myArr) { console.log(myArr[ele]); } Output10 15 20 [Function (anonymous)]Filtering Original Properties:To print only the original properties of an array, excluding prototype properties, you need to perform a check using the hasOwnProperty method. Here's how you can do it: Example: JavaScript Array.prototype.myProperty = function() { console.log("Hello"); } const myArr = [10, 15, 20] for (let ele in myArr) { if (myArr.hasOwnProperty(ele)) { console.log(myArr[ele]); } } Output10 15 20This code ensures that only the original elements in the array are printed, and any prototype properties are ignored. Conclusion :When dealing with JavaScript arrays that may have prototype properties, it's essential to understand how to print only the original properties. By using the hasOwnProperty method, you can iterate through an array while excluding any added prototype properties, ensuring that you obtain the intended results. This knowledge will help you maintain code clarity and avoid unexpected behavior in your JavaScript projects. Comment More infoAdvertise with us Next Article How to define custom Array methods in JavaScript? soumyadip2307 Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Questions Similar Reads JavaScript Array Iteration Methods JavaScript Array iteration methods perform some operation on each element of an array. Array iteration means accessing each element of an array. There are some examples of Array iteration methods are given below: Using Array forEach() MethodUsing Array some() MethodUsing Array map() MethodMethod 1: 3 min read How to use push() & pop() Methods in JavaScript Arrays? Arrays are dynamic data structures in JavaScript that have the ability to hold several values of various types. The push() and pop() methods are used for adding and removing elements from an array's last index.1. Using push() Method in JavaScriptThe push() method is used to add or push the new value 2 min read How to Declare an Array in JavaScript? Array in JavaScript are used to store multiple values in a single variable. It can contain any type of data like - numbers, strings, booleans, objects, etc. There are varous ways to declare arrays in JavaScript, but the simplest and common is Array Litral Notations. Using Array Literal NotationThe b 3 min read How to Declare Two Dimensional Empty Array in JavaScript ? In this article, we are going to learn about Declare two-dimensional empty array by using JavaScript. A two-dimensional array is also known as a 2D array. It is a data structure in JavaScript that can hold values in rows and columns form. It is an array of arrays. Each element in a 2D array is acces 3 min read Important Array Methods of JavaScript JavaScript arrays are powerful tools for managing collections of data. They come with a wide range of built-in methods that allow developers to manipulate, transform, and interact with array elements.Some of the most important array methods in JavaScript areTable of Content1. JavaScript push() Metho 7 min read How to Define an Array with Conditional Elements in JavaScript? Sometime we may need to define an array with elements that depend on certain conditions. Such situation may occur when we have to dynamically generate arrays based on runtime logic such as including or excluding elements based on specific criteria. In this article, we'll explore various ways to defi 2 min read How to pass an array as a function parameter in JavaScript? Here are different ways to pass an array as a function paramter in JavaScript.1. Using apply() MethodThe apply() method invokes a function with a specified `this` value and an array of arguments. It takes two parameters: the first is the `this` context for the function, and the second is an array of 2 min read How to add method to String class in JavaScript ? In this article, the task is to add a method to the String class in JavaScript. Two approaches are described with the proper examples: Approaches to add Methods to String Class:Table of ContentUsing Object.defineProperty() methodUsing String.prototype.propertyName method Using ES6 ClassesUsing ES6 S 4 min read JavaScript Array forEach() Method The JavaScript Array forEach() method is a built-in function that executes a provided function once for each array element. It does not return a new array and does not modify the original array. It's commonly used for iteration and performing actions on each array element.Syntaxarray.forEach(callbac 3 min read JavaScript Array of() Method The Javascript array.of() method is an inbuilt method in JavaScript that creates a new array instance with variables present as the argument of the method.Syntax:Array.of(element0, element1, ....)Parameters: Parameters present are element0, element1, .... which are basically an element for which the 2 min read Like