+ 3

What is the difference between null and a new empty object?

null vs empty object

27th May 2017, 4:22 AM
Astghik Saghyan
Astghik Saghyan - avatar
8 ответов
+ 8
Most of the times, we set an object to NULL to prevent it from storing garbage values. Newly declared objects may not necessarily be empty. Assigning NULL can be considered a good practice to prevent possible errors due to undefined behaviour. This is in terms of C++ though. I'm not sure if there are more reasons to do so in JS.
27th May 2017, 4:31 AM
Hatsy Rei
Hatsy Rei - avatar
+ 2
null is used, when ther is an aim to creat an object in the future, but why I can creat a new empty object and fill it after. What is the reason to have null in js?
27th May 2017, 4:26 AM
Astghik Saghyan
Astghik Saghyan - avatar
0
null when a variable value is set to an inexistent value (absence of value); Example: var x=null; undefined is obtained when we have a variable and no value is set; Example: var y;
27th May 2017, 5:54 AM
Federico Viceconti
Federico Viceconti - avatar
0
but call u later write x="john"?
30th May 2017, 12:31 PM
Eriba Stephen
Eriba Stephen - avatar
0
but can u later write x="john"?
30th May 2017, 12:33 PM
Eriba Stephen
Eriba Stephen - avatar
0
hi
21st Jun 2017, 8:12 PM
Emrah Ehmedov
Emrah Ehmedov - avatar
0
what do you meat?
21st Jun 2017, 8:12 PM
Emrah Ehmedov
Emrah Ehmedov - avatar
0
null is a "non-value" used to mean "no object". undefined is another "non-value" used to mean "no value". undefined can be found in situations such as: - uninitialized variables - missing parameters - non-existent property of an object - default return value of a function (i.e. the function has no explicit return value) So, null can be used as a "value" (a value of "no object") or placeholder whenever an object is expected but no object value has been ready to assign yet. Also, note the following when considering when to use null or an empty object {}: (1) null === null // true (2) {} === {} // false (two different object literals) (3) !!null // false, null returns false (use the !! to negate and revert back to the truthiness of null) (4) !!{} // true, any object, even an empty object {}, returns true (use the !! to negate and revert back to the truthiness of {}) One more important point, although typeof null // 'object', not returning null In fact this is not accurate, as null does not have any "inherited" properties and methods found in an ordinary object. So it depends on the real case implementation of the code logic which to use (null or empty object).
17th Jul 2017, 2:36 PM
zp1