You are browsing the archive for object.

JavaScript Quiz #8 (One line Quiz)

May 9, 2013 in JavaScript

Assume that we have the following short JavaScript code:

    var result = (2..valueOf() + ({z : 10, x : 20}).x);

What is the output of each alert?

Know the complete answer

Iterate over a JSON object

February 29, 2012 in JavaScript, Web 2.0

One of the most useful advantages of the JSON format is that it is browser friendly. You donot need to write complicated code for navigating or iterating over a JSON structure. In this post, I will show you how to iterate over a JSON object.

Assume that you are having the following JSON object.

<script type="text/javascript">
var personStr = "{\"Name\":\"Hazem Saleh\",\"Occupation\":\"Software Engineer\",\"Nationality\":\"Egyptian\",\"Age\":\"102\"}";

var personObject = eval("(" + personStr + ")");

You may notice that I put the JavaScript String between parentheses. This is mandatory to avoid the json invalid label browser error.

if you want to simply iterate over the JSON object you should use the standard JavaScript for in loop as follows.

<script type="text/javascript">
for (var key in personObject) {
  if (personObject.hasOwnProperty(key)) {
    alert(key + " -> " + personObject[key]);

I wish that this tip can be helpful to you.

Skip to toolbar