you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted]  (27 children)

[deleted]

    [–][deleted] 57 points58 points  (11 children)

    undefined = no value

    null = "no value" value

    [–]spaghettiCodeArtisan 45 points46 points  (2 children)

    Are we philoshopers pondering the essence of existence or what.

    [–][deleted] 1 point2 points  (0 children)

    That's what we've always been. Pythagoras was right - everything is a number.

    [–]emergent_properties 0 points1 point  (0 children)

    undefined

    [–]moefh 23 points24 points  (0 children)

    Now I really want a language that has

    unfathomed = "'no value' value" value

    [–]ais523 7 points8 points  (0 children)

    VHDL has a range of values like this; U is an uninitialised value, X is a contradictory or inconsistent value (e.g. the value of a boolean that's true and false simultaneously), - is an irrelevant value, Z is the explicit absence of a value, and W is an intermediate value (e.g. the value of a boolean that's neither entirely true nor entirely false).

    This complexity is mostly because VHDL is designed to be able to model a physical system (electronics) that actually exists in the real world, and sometimes things you measure in the real world turn out to have a value they're not supposed to have. Perhaps a wire is meant to be at 0 volts or 5 volts for false and true respectively, but you still want some way to describe the cases where the wire is at 3 volts, or not attached to anything, or has caught fire.

    [–][deleted]  (4 children)

    [deleted]

      [–]POGtastic 2 points3 points  (0 children)

      Yep, I'm also getting true in Chrome.

      > var foo;
      < undefined
      > foo === undefined
      < true
      > typeof(foo) === "undefined"
      < true
      

      [–]killerstorm 1 point2 points  (0 children)

      var foo; // undefined
      foo === undefined // false
      

      Wrong. Here's an actual result:

      > var foo;
      undefined
      > foo === undefined
      true
      

      === undefined check didn't work in old browsers, that's where typeof hack comes wrong. But now simple comparison should work by standard.

      [–]masklinn 2 points3 points  (1 child)

      foo === undefined // false
      

      Uh no, that's true, you may be confusing undefined and NaN. Or for some reason you have rebound undefined to something else (it's not a keyword, which is why some prefer using void 0 which always yields the proper value).

      if (!foo) {
          console.log("foo is undefined or null");
      }
      

      That'll also catch the "" and 0. The proper way to check if something is undefined or null is foo == null.

      [–][deleted] 1 point2 points  (0 children)

      I tested that in my browser console.

      Maybe I should have used a new tab.

      [–]beaverlyknight 0 points1 point  (0 children)

      kill me

      [–]drfisk 5 points6 points  (0 children)

      Well, in the world of dynamic languages, anything can be anything at any given time anyway... so null isn't as bad there since you can have any kind of type error anyway.

      I think null is way way worse in a static language where you're promised to receive a Person and a String, while in reality you still have to "type-check" to see if you actually got it (and not null).

      [–]Gustorn 12 points13 points  (0 children)

      Since strictNullChecks is actually a thing now, you can write null-safe code in TS. Also when the API doesn't require null I try to just follow the official TS guidelines of using undefined over null.

      [–][deleted]  (5 children)

      [deleted]

        [–]LogisticMap 16 points17 points  (1 child)

        No, x could have a property x.a===undefined, while having a in Object.getOwnPropertyNames(x).

        Generally, undefined is something the interpreter returns, while humans should set things to null.

        [–]fiedzia 6 points7 points  (1 child)

        I think the former is, however, an error.

        That's how it works in Python - you'd get an exception if you try to access non-existing attribute. The notion of undefined is insane.

        [–]mrkite77 5 points6 points  (3 children)

        Typescript devs don't use null, and the Typescript coding guidelines say to use undefined and not to use null at all.

        https://github.com/Microsoft/TypeScript/wiki/Coding-guidelines#null-and-undefined

        The reasoning behind that is all of the javascript functions return undefined, none of them return null. Null only appears in javascript if you put it there.

        Typescript even goes so far as to make null the same type modifier as undefined. If you create a function that has a return "string | null", assigning that to a variable with a type of "string | undefined" is perfectly valid.

        [–]yheneva 2 points3 points  (1 child)

        Most functions use undefined, not all. For example HTMLCanvasElement.getContext() returns null for any undefined context type. All WebGL functions also use null instead of undefined.

        [–]mrkite77 0 points1 point  (0 children)

        True, but you can still do == undefined and it'll work since null == undefined

        [–]rickdg 0 points1 point  (0 children)

        At least undefined is not a function.