Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
146
rated 0 times [  149] [ 3]  / answers: 1 / hits: 24707  / 15 Years ago, sun, january 17, 2010, 12:00:00

Is there any way to make an object return false in javascript?



var obj = new Object();

console.log(!!obj) // prints true even if it's empty

More From » javascript

 Answers
12

No. An object that doesn't have any properties assigned is not considered empty.



The fact that a variable holds an instance of an object is enough to cause javascript to treat the variable as having the value of true when an expression requires a boolean value.



Edit



There are clearly some nuances to be cleared up looking at the other answers here.



null is not an object, it is the distinct lack of an object. The question refers to an Object, that is one that has just been created.


[#97815] Wednesday, January 13, 2010, 15 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
daquanmilesw

Total Points: 57
Total Questions: 102
Total Answers: 110

Location: Wallis and Futuna
Member since Sat, Aug 6, 2022
2 Years ago
daquanmilesw questions
;