Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: 'falsey' values that are not undefined

  1. #1
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,497
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default 'falsey' values that are not undefined

    Yes, I think about these things sometimes. What are the 'falsey' values that are not undefined (useful in iterating over an object when .hasOwnProperty() is either unavailable or inadvisable, or iterating over an array when you want to know if something, anything, even a 'falsey' value is defined for a given index).

    Anyways, so far I have the empty string, 0, null, and false:

    Code:
    ['', 0, null, false]
    Anyone have any additional ideas?
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  2. #2
    Join Date
    Mar 2011
    Posts
    1,672
    Thanks
    59
    Thanked 102 Times in 100 Posts
    Blog Entries
    4

    Default

    Code:
    ["", '', ``, 0, -0, 0.0, 0x0, null, false, undefined, NaN, void 0]

  3. The Following User Says Thank You to keyboard For This Useful Post:

    jscheuer1 (04-27-2018)

  4. #3
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    1,922
    Thanks
    92
    Thanked 98 Times in 96 Posts

    Default

    Is the following table what you are looking into?

    https://dorey.github.io/JavaScript-Equality-Table/

    A while back I looked into something similar with php regarding various types of empty values. See the following php documentation: PHP type comparison tables

    Please let me know if I am misunderstanding.
    To choose the lesser of two evils is still to choose evil. My personal site

  5. The Following User Says Thank You to james438 For This Useful Post:

    jscheuer1 (04-27-2018)

  6. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,497
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Quote Originally Posted by keyboard View Post
    Code:
    ["", '', ``, 0, -0, 0.0, 0x0, null, false, undefined, NaN, void 0]
    To begin, the first three all appear to be === to the empty string. Have I missed something there? (from the console):

    Code:
    "" === ''
    true
    `` === ''
    true
    And I said, "that are not undefined" so that lops off one. Unquoted 0.0, and -0, as far as I know*, evaluate to 0, so two more bite the dust. NaN and void 0 I'm not sure of. Just checked in the console, and:

    Code:
    NaN == false
    returns false, so is not 'falsey'.

    and:

    Code:
    void 0
    returns undefined, so is technically not what I'm looking for. But is worthy of further consideration.

    The void keyword by itself is odd. From what I can tell, nothing can be set to it, so it also shouldn't enter into this. However, odd as it is, void merits further study.

    Thanks for the input, and if you would like to refute anything I've said, or simply comment further upon it, please do.


    *from the console:

    Code:
    0.0 === 0
    true
    -0 === 0
    true
    Last edited by jscheuer1; 04-27-2018 at 03:57 AM. Reason: add info
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  7. #5
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,497
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Quote Originally Posted by james438 View Post
    Is the following table what you are looking into?
    Yes, but it is in error. Or at least as I read it. It appears to me to say null === undefined, but it doesn't.

    Then again it might just mean that null == undefined, which I knew, but that's not what I'm after.
    Last edited by jscheuer1; 04-27-2018 at 04:01 AM. Reason: add info
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  8. #6
    Join Date
    Mar 2011
    Posts
    1,672
    Thanks
    59
    Thanked 102 Times in 100 Posts
    Blog Entries
    4

    Default

    To begin, the first three all appear to be === to the empty string. Have I missed something there? (from the console):
    Yeah they are all an empty string, just included them all for my own sake of complete coverage.

    And I said, "that are not undefined" so that lops off one.
    Yeah. Again I just included undefined for coverage sake.

    Unquoted 0.0, and -0, as far as I know*, evaluate to 0, so two more bite the dust.
    Yup. They all evaluate to 0 just because of the way JS stores numbers. Same with 0x0.
    Starting to see a pattern of me just including stuff because I think its cool haha.

    NaN == false
    returns false, so is not 'falsey'.
    NaN == true also evaluates to false.
    Also
    Code:
    if(NaN) {
    	console.log(true);
    } else {
    	console.log(false);
    }
    returns false. So I believe it is falsey.

    void 0
    returns undefined, so is technically not what I'm looking for. But is worthy of further consideration.
    Yeah it just evaluates an expression and returns undefined.



    Most of the stuff on here I just included because it was interesting.
    To be honest I still have no idea what you were trying to do in the first place haha (the bit about how it would be useful when iterating without hasOwnProperty).

  9. #7
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,497
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    NaN is creepy and I may have to include it. Using undefined kind of defeats the purpose because [][0] is undefined, and so is [undefined][0]. So there's no way around it without something like hasOwnProperty(), which I don't believe was intended for such use and I don't think works on arrays anyway. MMMmmm. It does work on arrays:

    Code:
    [undefined].hasOwnProperty(0)
    is true. Unfortunately:

    Code:
    [].hasOwnProperty(0)
    is a syntax error. But you can do:

    Code:
    a = []
    []
    a[1] = 'bob'
    "bob"
    a.hasOwnProperty(0)
    false
    a[0] = undefined
    undefined
    a.hasOwnProperty(0)
    true
    So, combined with length, you could work something out. But what I'm working with should be OK as long as someone doesn't use undefined and is trying to mean false when the default is true:

    Code:
    	var runningflags = {};
    	function lotto(cfg){
    		cfg = cfg || {};
    		var defaults = {
    			totalnumbers: 10,  // how many numbers to generate
    			lowerbound: 1,     // lower bound for generated numbers
    			upperbound: 70,    // upper bound for generated numbers
    			interval: 20,      // how quickly calculations repeat until finalized (milliseconds)
    			caltime: 1000,     // how long until calculations finalized (milliseconds)
    			id: 'lottery1',    // id of target element that shows result
    			noadjacent: true,  // should there be at least 2 between selected numbers
    			padsingle: false,  // should single digits be padded with preceding 0
    			sort: true,        // should result be sorted
    			dupes: false       // allow duplicate numbers
    		};
    		this.setprops(defaults, cfg); . . .
    and:

    Code:
    	lotto.prototype = {
    		setprops: runningflags.hasOwnProperty? function(d, c){for(var p in d){this[p] = c.hasOwnProperty(p)? c[p] : d[p];}
    		} : function(d, c){for(var p in d){this[p] = c[p] || [0, '', null, false].indexOf(c[p]) > -1? c[p] : d[p];}}, . . .
    This will allow lowerboud to be set in the cfg to 0 and things like noadjacent and sort in the cfg to be set to false, even when .hasOwnProperty() is unavailable. Now I know that often, if not aways, when .hasOwnProperty() is unavailable that [].indexOf() is also unavailable. But that's easy to fix:

    Code:
    if(![].indexOf){ // for IE 8 and less
    	Array.prototype.indexOf = function(v, i){
    		var l = this.length;
    		if(typeof i === 'number'){if(i < 0){i = l + i;}} else {i = 0;}
    		--i;
    		while(++i < l){if(v === this[i]){return i;}}
    		return -1;
    	}
    }
    (taken loosely from the MDN Polyfill for Array.indexOf)

    Maybe they have a poly for hasOwnProperty() - nope, just checked.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  10. #8
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,497
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    I think I've figured this out and it should also work for arrays, but would be a little more cumbersome. Anyways, for objects:

    Code:
    function setprops(d, c){
    var p, tmp = {};
    for(p in c){tmp[p] = true;}
    for(p in d){this[p] = tmp[p]? c[p] : d[p];}
    }
    Even simpler:

    Code:
    function(d, c){for(var p in d){this[p] = p in c? c[p] : d[p];}}
    Last edited by jscheuer1; 04-27-2018 at 02:23 PM. Reason: add info
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  11. #9
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,497
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    The more I looked into what hasOwnProperty means and why it might be useful, things became more complicated again, both for when it's available and when it's not. So for now I'm using this:

    Code:
    		setprops: runningflags.hasOwnProperty? function(d, c){for(var p in d){if(d.hasOwnProperty(p)){this[p] = c.hasOwnProperty(p)? c[p] : d[p];}}
    		} : function(d, c){for(var p in d){if((!d.prototype || !p in d.prototype) && !p in Object.prototype){this[p] = p in c? c[p] : d[p];}}},
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  12. #10
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,497
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    You can't polyfill hasOwnProperty because all or at least some of the browsers that don't support it also don't support assigning things to the Object.prototype. Here's the next best thing (more or less, still figuring, use a more distinct variable name for the function or encapsulate it in the area where it's needed):

    Code:
    var op;
    if(Object.prototype.hasOwnProperty){op = function(obj, p){return Object.prototype.hasOwnProperty.apply(obj, [p]);};}
    else {op = function(obj, p){return p in obj && (!obj.prototype || !p in obj.prototype) && !p in Object.prototype;};}
    Last edited by jscheuer1; 04-28-2018 at 03:14 AM. Reason: possible code improvement
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  13. The Following User Says Thank You to jscheuer1 For This Useful Post:

    keyboard (04-28-2018)

Similar Threads

  1. Replies: 5
    Last Post: 09-28-2012, 01:31 AM
  2. Resolved Array Values/Other Values
    By bluewalrus in forum JavaScript
    Replies: 8
    Last Post: 12-13-2010, 12:59 AM
  3. $f is undefined
    By creative creator in forum JavaScript
    Replies: 2
    Last Post: 06-18-2010, 01:25 PM
  4. Replies: 1
    Last Post: 07-14-2008, 11:57 PM
  5. Replies: 3
    Last Post: 05-27-2008, 05:36 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •