JSFiddle - React, Tailwind, and code Playground

by hajpoj

JavaScript

var a = { a: 1, b:2, c:3}

// objects don't have a length property both of these return undefined. 
console.log(a[0]);
console.log(a.length);

// using the [] notation with an object is accessing the property.
// does not work
console.log(a[0]);

// works
console.log(a['a']);

// doesn't work because a.length is undefined. 
for( var i = 0; i< a.length; i ++) {
    console.log(a[i]);
}

// works. proper way to loop through all the keys of an object
for(p in a) {
    console.log(p);
}