From the course: JSON Essential Training
Unlock the full course today
Join today to access over 24,900 courses taught by industry experts.
Test for an empty object - JavaScript Tutorial
From the course: JSON Essential Training
Test for an empty object
- [Instructor] When you process data returned from a call to a web service, a useful first step is to check whether any data has been returned. A JSON string will generally contain some characters, even if they're just the opening and closing braces for an encoded object or the brackets for an array. So parsing is a common first step. If you're using JavaScript though, checking whether the object or array is empty isn't as straightforward as checking for empty data of some other types. Unlike an empty string, for example, which is falsy in JavaScript, an empty object is truthy. This means that a simple logical test, like checking if the value is null cannot distinguish between an empty object and an object with content. In a modern browser, you can use the Object.keys method to generate an array of your object's keys and then check if the length property of the array is zero. I've created a test file containing…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.