Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript object literal length === undefined?

I am working on this animation function but I have a problem. I can't seem to perform what should be an easy task, I can not get the length of an object. If you check out that jsFiddle you can see that I am running alert(properties.length); and it is returning undefined. Can anyone see why this might be?

like image 506
Olical Avatar asked Jan 14 '11 11:01

Olical


People also ask

Why is the length of an object undefined?

Objects are used to store the information in key-value pairs, where a value can be any other data type or function. Unlike arrays and strings, objects don't have predefined length property to get the length of an object. So when we call length property on an object it will return undefined.

How do you check if JavaScript object is undefined?

In a JavaScript program, the correct way to check if an object property is undefined is to use the typeof operator. If the value is not defined, typeof returns the 'undefined' string.

Why is my object undefined JavaScript?

A variable that has not been assigned a value is of type undefined . A method or statement also returns undefined if the variable that is being evaluated does not have an assigned value. A function returns undefined if a value was not returned .

Why is length undefined JavaScript?

The "Cannot read property 'length' of undefined" error occurs when accessing the length property on an undefined value. To solve the error, make sure to only access the length property on data types that support it - arrays or strings. Here is an example of how the error occurs.


1 Answers

This is supported in node.js and newer environments.

var obj = {a: "a", b: "b"}; Object.keys(obj).length // 2 
like image 74
Jamund Ferguson Avatar answered Oct 10 '22 19:10

Jamund Ferguson