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.
    • ulrikU
      ulrik
      last edited by ulrik

      How can I detect "null"?
      I have a variable holding a number, and in some cases it will return as a "null" and I want to detect when that happens, so how can I

      if (variable == bla...)
          do something else...
      

      I tried "undefined", "isDefined(), and it seems that "null" is defined, so that will not work.

      I tried

      Console.print(typeof var);
      

      which results in "number"

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

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

        @ulrik this will not work either

        if (r === null)
        

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

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

          @ulrik What about preventing it being null?

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

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

            @ulrik Ok I solved it in this case, this worked

            if (variable == "inf")
                break;
            

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

            1 Reply Last reply Reply Quote 0
            • 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
                                            • First post
                                              Last post

                                            48

                                            Online

                                            1.7k

                                            Users

                                            11.7k

                                            Topics

                                            101.9k

                                            Posts