Created
December 23, 2020 19:17
-
-
Save DoctorDerek/c5030801a83c0b5f1297c3fa656b071b to your computer and use it in GitHub Desktop.
How to Check If a JavaScript Object Is Empty of Properties
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const object1 = { "π": "πΉ" } | |
| Object.defineProperty(object1, "π", { enumerable: false }) | |
| object1["π"] = "π" | |
| object1[Symbol("π₯")] = "π" | |
| console.log(Object.keys(object1).length) // 1 | |
| console.log(Object.keys({}).length) // 0 | |
| const object2 = { "π": "πΉ" } | |
| Object.defineProperty(object2, "π", { enumerable: false }) | |
| object2["π"] = "π" | |
| object2[Symbol("π₯")] = "π" | |
| console.log(Object.getOwnPropertyNames(object2).length) // 2 | |
| console.log(Object.getOwnPropertyNames({}).length) // 0 | |
| const object3 = { "π": "πΉ" } | |
| Object.defineProperty(object3, "π", { enumerable: false }) | |
| object3["π"] = "π" | |
| object3[Symbol("π₯")] = "π" | |
| let count = 0 | |
| for (const key in object3) { | |
| object3.hasOwnProperty(key) && count++ | |
| } | |
| console.log(count) // 1 | |
| const object4 = { "π": "πΉ" } | |
| Object.defineProperty(object4, "π", { enumerable: false }) | |
| object4["π"] = "π" | |
| object4[Symbol("π₯")] = "π" | |
| console.log(Object.getOwnPropertySymbols(object4).length) // 1 | |
| console.log(Object.getOwnPropertySymbols({}).length) // 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment