HISE Logo Forum
    • Categories
    • Register
    • Login

    detect "null"

    Scheduled Pinned Locked Moved Scripting
    23 Posts 5 Posters 1.2k Views
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • ustkU
      ustk @d.healey
      last edited by ustk

      @d-healey I often use undefined or null as intentional state or value for variables (especially for object properties) so I can discriminate if a value is ready to read or not. So when I want to reset it I set it back to undefined or null
      Let’s say it my -1

      EDIT: I realise that I should more often use null rather than undefined

      Can't help pressing F5 in the forum...

      1 Reply Last reply Reply Quote 0
      • d.healeyD
        d.healey @ulrik
        last edited by d.healey

        @ulrik said in detect "null":

        @ulrik this will not work either

        if (r === null)
        

        It works for me. Try this.

        const t = null;
        
        Console.print(t == void);
        Console.print(t == undefined);
        Console.print(t == null);
        
        Console.print(t === void);
        Console.print(t === undefined);
        Console.print(t === null);
        

        Libre Wave - Freedom respecting instruments and effects
        My Patreon - HISE tutorials
        YouTube Channel - Public HISE tutorials

        ustkU ulrikU 2 Replies Last reply Reply Quote 0
        • P
          parabuh @ulrik
          last edited by

          @ulrik you can use shorthand

          if (!var) {
          }
          
          d.healeyD ulrikU 2 Replies Last reply Reply Quote 0
          • d.healeyD
            d.healey @parabuh
            last edited by

            @parabuh That won't tell you is the variable is null

            Libre Wave - Freedom respecting instruments and effects
            My Patreon - HISE tutorials
            YouTube Channel - Public HISE tutorials

            1 Reply Last reply Reply Quote 0
            • ustkU
              ustk @d.healey
              last edited by ustk

              @d-healey Hmmm... I just discovered that a value of 0 == undefined 🤔 Back to school for me!

              Can't help pressing F5 in the forum...

              d.healeyD 1 Reply Last reply Reply Quote 0
              • ulrikU
                ulrik @parabuh
                last edited by

                @parabuh that will not work

                Hise Develop branch
                MacOs 15.3.1, Xcode 16.2
                http://musikboden.se

                1 Reply Last reply Reply Quote 0
                • d.healeyD
                  d.healey @ustk
                  last edited by d.healey

                  @ustk I never use var == undefined I always use isDefined(var)

                  I just noticed this guy commented on my video (I think I might re-record that one)

                  e34e1582-5056-480b-8f88-6b3768d9d366-image.png

                  Libre Wave - Freedom respecting instruments and effects
                  My Patreon - HISE tutorials
                  YouTube Channel - Public HISE tutorials

                  1 Reply Last reply Reply Quote 1
                  • ulrikU
                    ulrik @d.healey
                    last edited by

                    @d-healey

                    if I declare a variable like this

                    const t = null;
                    

                    First I'll get

                    Console.print(t);
                    API call with undefined parameter 0
                    
                    Console.print(t == void);
                    result: 1
                    
                    Console.print(t == undefined);
                    result: 1
                    
                    Console.print(t == null);
                    result: 1
                    
                    Console.print(t === void);
                    result: 0
                    
                    Console.print(t === undefined);
                    result: 0
                    
                    Console.print(t === null);
                    result: 1
                    

                    but my variable show up as null, and if I use

                    Console.print(myvariable === null);
                    result: 0
                    

                    Are there different kind of "null"??

                    Hise Develop branch
                    MacOs 15.3.1, Xcode 16.2
                    http://musikboden.se

                    d.healeyD 1 Reply Last reply Reply Quote 0
                    • d.healeyD
                      d.healey @ulrik
                      last edited by

                      @ulrik Are you sure your variable is null?

                      Libre Wave - Freedom respecting instruments and effects
                      My Patreon - HISE tutorials
                      YouTube Channel - Public HISE tutorials

                      ulrikU 1 Reply Last reply Reply Quote 0
                      • ulrikU
                        ulrik @d.healey
                        last edited by ulrik

                        @d-healey that's what it's saying if I Console.print it
                        my var is inside an array and if trace log it will say something like this:

                        Interface: [
                          3,
                          null
                        ]
                        

                        Hise Develop branch
                        MacOs 15.3.1, Xcode 16.2
                        http://musikboden.se

                        d.healeyD ulrikU 2 Replies Last reply Reply Quote 0
                        • d.healeyD
                          d.healey @ulrik
                          last edited by

                          @ulrik I think you need to give us an example we can test.

                          This works for me:

                          const a = [3, null];
                          
                          Console.print(a[1] === null);
                          

                          So you must be doing something different.

                          Libre Wave - Freedom respecting instruments and effects
                          My Patreon - HISE tutorials
                          YouTube Channel - Public HISE tutorials

                          1 Reply Last reply Reply Quote 0
                          • ulrikU
                            ulrik @ulrik
                            last edited by

                            @ulrik Ok, I Console.print each member of the array and got this instead:

                            Interface: inf
                            

                            so if trace logged it show up as "null" and if logged independently it show up as "inf"

                            Hise Develop branch
                            MacOs 15.3.1, Xcode 16.2
                            http://musikboden.se

                            ulrikU 1 Reply Last reply Reply Quote 1
                            • ulrikU
                              ulrik @ulrik
                              last edited by

                              @ulrik @d-healey so what does "inf" stand for, infinite?

                              Hise Develop branch
                              MacOs 15.3.1, Xcode 16.2
                              http://musikboden.se

                              d.healeyD 1 Reply Last reply Reply Quote 0
                              • d.healeyD
                                d.healey @ulrik
                                last edited by

                                @ulrik No idea. I'd recommend you try to always populate your variables yourself so you know what it contains.

                                Libre Wave - Freedom respecting instruments and effects
                                My Patreon - HISE tutorials
                                YouTube Channel - Public HISE tutorials

                                Christoph HartC 1 Reply Last reply Reply Quote 1
                                • Christoph HartC
                                  Christoph Hart @d.healey
                                  last edited by

                                  Yes, inf is infinite:

                                  Console.print(1.0 / 0.0)
                                  

                                  But the question is: how did you get null in the first place? I honestly didn't know that existed, we've got undefined for a uninitialized variable, void for a invalid array or object field and inf and nan for illegal number values.

                                  var x = 1 / 0.0;
                                  x = 0.0 * x;
                                  
                                  var a = [1];
                                  var n;
                                  
                                  Console.print(typeof(a[9])); // => void
                                  Console.print(typeof(b)); // => undefined
                                  Console.print(typeof(1.0 / 0.0)); // => number
                                  Console.print(typeof(n)); // => undefined
                                  Console.print(x); // => nan
                                  
                                  ulrikU 1 Reply Last reply Reply Quote 0
                                  • ulrikU
                                    ulrik @Christoph Hart
                                    last edited by

                                    @Christoph-Hart ok thanks!

                                    As I said here:

                                    @ulrik Ok, I Console.print each member of the array and got this instead:

                                    Interface: inf
                                    

                                    so if trace logged it show up as "null" and if logged independently it show up as "inf"

                                    It seems the

                                    Console.print(trace(array))
                                    

                                    is not showing correct information?

                                    Hise Develop branch
                                    MacOs 15.3.1, Xcode 16.2
                                    http://musikboden.se

                                    Christoph HartC 1 Reply Last reply Reply Quote 0
                                    • Christoph HartC
                                      Christoph Hart @ulrik
                                      last edited by

                                      @ulrik I just skimmed through the JUCE codebase and the only time where null is used is when you convert a JSON object with a void field to a string (which would be the case if you do something like Console.print(trace(array) command). I guess that's just for compatibility reasons with the JSON standard and it is still being typed internally as void. I can't say why it is behaving like that with an inf number though, as this is clearly a number type and should be treated as such (eg. isDefined(1.0/0.0) returns true).

                                      However I got this output:

                                      3bc544cb-1827-4a81-8dc5-8be51f629e2b-image.png

                                      so apparently assigning void to a field is the same as assigning undefined (which I thought were two different things), however the isDefined() check works correctly on all of them.

                                      BTW, using inf as literal is not protected, so you can do stuff like:

                                      var inf = 90;
                                      Console.print(inf);
                                      
                                      ustkU 1 Reply Last reply Reply Quote 2
                                      • ustkU
                                        ustk @Christoph Hart
                                        last edited by ustk

                                        @Christoph-Hart Is it normal that a[2] set to null is throwing undefined ? It's weird to me because it is actually defined as null...

                                        EDIT: I think I have my answer...

                                        Link Preview Image
                                        What is the difference between null and undefined in JavaScript?

                                        What is the difference between null and undefined in JavaScript?

                                        favicon

                                        Stack Overflow (stackoverflow.com)

                                        console.log(null === undefined) // false (not the same type)
                                        console.log(null == undefined) // true (but the "same value")
                                        

                                        alt text

                                        Can't help pressing F5 in the forum...

                                        ulrikU 1 Reply Last reply Reply Quote 3
                                        • ulrikU
                                          ulrik @ustk
                                          last edited by

                                          @ustk 😂

                                          Hise Develop branch
                                          MacOs 15.3.1, Xcode 16.2
                                          http://musikboden.se

                                          1 Reply Last reply Reply Quote 0
                                          • First post
                                            Last post

                                          20

                                          Online

                                          1.7k

                                          Users

                                          11.8k

                                          Topics

                                          103.2k

                                          Posts