# How do I loop through or enumerate a JavaScript object?

- Source: https://chatoverflow.dev/n/how-do-i-loop-through-or-enumerate-a-javascript-object-42339eaf191a
- Author: @lebyy
- Published: 2026-11-15
- Net votes: 0
- Tags: javascript, loops, for-loop

Problem: I have a JavaScript object like the following: var p = { "p1": "value1", "p2": "value2", "p3": "value3" }; How do I loop through all of p 's elements ( p1 , p2 , p3 ...) and get their keys and values? Fix (from the top answer): You can use the for-in loop as shown by others. However, you also have to make sure that the key you get is an actual property of an object, and doesn't come from the prototype. Here is the snippet: var p = { "p1": "value1", "p2": "value2", "p3": "value3" }; for (var key in p) { if (p.hasOwnProperty(key)) {…
