Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can an Object be false?

Tags:

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 
like image 774
David Hellsing Avatar asked Jan 17 '10 14:01

David Hellsing


People also ask

Is an empty object false?

The empty object is not undefined. The only falsy values in JS are 0 , false , null , undefined , empty string, and NaN .

Is an object truthy?

Values not on the list of falsy values in JavaScript are called truthy values and include the empty array [] or the empty object {} . This means almost everything evaluates to true in JavaScript — any object and almost all primitive values, everything but the falsy values.

Can a value be both true and false JS?

A core mechanic of Javascript is the ability to distinguish between true and false values. The Javascript standard defines true and false values as a unique data type called a Javascript boolean. Javascript booleans may be true , false , or (in certain contexts) a value that evaluates to either true or false .

Does an empty object return true?

keys , it does return true when the object is empty ✅.

How to set all the properties of an object to false?

To set all properties of an Object to false, pass the object to the Object.keys () method to get an array of the object's keys and use the forEach () method to iterate over the array and set each of the object's properties to a value of false. We used the Object.keys method to get an array of the object's keys.

How to make an object evaluate to false in JavaScript?

(You can Object.keys (document.all).length or ""+document.all or document.all instanceof Object to verify that document.all is present.) As of ES8, no, you cannot make an object evaluates to false in JavaScript. False if undefined, null, false, zero, NaN, or empty string. If the type of the input is object, the result is true. No question asked.

How do I create a falsy object in JavaScript?

No valueOf, no special case. There is no way to create a falsy object in JavaScript. Only non-objects can be falsy. Sometimes you may run into object-like "stuff" that return false. Empty string, for example, are used like an object all the time. document.all is another falsy "object". These are not real objects, however.

What are some examples of objects that return false values?

Sometimes you may run into object-like "stuff" that return false. Empty string, for example, are used like an object all the time. document.all is another falsy "object". These are not real objects, however. They cannot have custom properties, cannot be used as prototype, and does not always behave like an object e.g. typeof or strict equal.


1 Answers

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.

like image 81
AnthonyWJones Avatar answered Sep 23 '22 16:09

AnthonyWJones