Home | SkillForge Blog | How To Loop Through An Object In JavaScript

How To Loop Through An Object In JavaScript

JavaScript, Web Design

In JavaScript there’s a way to access all the items in an object called the for in loop.  It allows you to iterate (or loop) through all the key value pairs in an object.  For example, if we had this object:

var jsobj = {

company: “SkillForge”,

course: “JavaScript”,

isFun: “Yes”


We would be able to get the keys and the values out of it using the for in loop.  To pull the keys out we could do this:

for(x in jsobj){

document.write(x + “<br>”);


In this loop, x is a variable that will be holding all of the keys (company, course, and isFun) and each time the loop runs it would print the keys out to the page using document.write.  If we wanted to pull out the values we could do it this way:

for(x in jsobj){

document.write(jsobj[x] + “<br>”);


In this loop, x still equals the keys so we need to use square bracket notation to pull the values using the keys.  Using both of these methods, we now have access to the keys or values of the object.  If you’d like to learn more please check out our JavaScript training.  Have an amazing day!