Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. JavaScript
  4. Condition Always true when checking for "undefined" in Javascript

Condition Always true when checking for "undefined" in Javascript

Scheduled Pinned Locked Moved JavaScript
csharphelpjavascriptasp-net
12 Posts 6 Posters 0 Views 1 Watching
  • 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.
  • V Offline
    V Offline
    Vimalsoft Pty Ltd
    wrote on last edited by
    #1

    Good Day all i am reluctantly maintaining a Classic Asp application that will be re-written in few months to come. i have an issue where i have a Code like this

    if(document.frmEntry.optMethod != "undefined")
                        {
                            if (document.frmEntry.optMethod.selectedIndex == 0) {
                                strPrompt=strPrompt+'Please specify the Valuation Method\\n';
                                if ( objFocus == null ) {
                                    objFocus = document.frmEntry.optMethod;
                                }
                            }
                        } 
    

    my problem here is that even if document.frmEntry.optMethod is not equal to "undefined" the code still go into the condition as if it was true. i have attached a proof in my debuger. http://www.vetauinvest.com/Example/IE_DEBUGGER.png[^] Thanks

    Vuyiswa Maseko, Spoted in Daniweb-- Sorry to rant. I hate websites. They are just wierd. They don't behave like normal code. C#/VB.NET/ASP.NET/SQL7/2000/2005/2008 http://www.vuyiswamaseko.com vuyiswa[at]dotnetfunda.com http://www.Dotnetfunda.com

    B Kornfeld Eliyahu PeterK G J T 5 Replies Last reply
    0
    • V Vimalsoft Pty Ltd

      Good Day all i am reluctantly maintaining a Classic Asp application that will be re-written in few months to come. i have an issue where i have a Code like this

      if(document.frmEntry.optMethod != "undefined")
                          {
                              if (document.frmEntry.optMethod.selectedIndex == 0) {
                                  strPrompt=strPrompt+'Please specify the Valuation Method\\n';
                                  if ( objFocus == null ) {
                                      objFocus = document.frmEntry.optMethod;
                                  }
                              }
                          } 
      

      my problem here is that even if document.frmEntry.optMethod is not equal to "undefined" the code still go into the condition as if it was true. i have attached a proof in my debuger. http://www.vetauinvest.com/Example/IE_DEBUGGER.png[^] Thanks

      Vuyiswa Maseko, Spoted in Daniweb-- Sorry to rant. I hate websites. They are just wierd. They don't behave like normal code. C#/VB.NET/ASP.NET/SQL7/2000/2005/2008 http://www.vuyiswamaseko.com vuyiswa[at]dotnetfunda.com http://www.Dotnetfunda.com

      B Offline
      B Offline
      Blikkies
      wrote on last edited by
      #2

      Try using a Falsy check. The following values are always falsy: false 0 (zero) "" (empty string) null undefined NaN (a special Number value meaning Not-a-Number!) Your code should look like as follows:

      if(!document.frmEntry.optMethod)
      {
      if (document.frmEntry.optMethod.selectedIndex == 0) {
      strPrompt=strPrompt+'Please specify the Valuation Method\n';
      if ( objFocus == null ) {
      objFocus = document.frmEntry.optMethod;
      }
      }
      }

      Kornfeld Eliyahu PeterK 1 Reply Last reply
      0
      • V Vimalsoft Pty Ltd

        Good Day all i am reluctantly maintaining a Classic Asp application that will be re-written in few months to come. i have an issue where i have a Code like this

        if(document.frmEntry.optMethod != "undefined")
                            {
                                if (document.frmEntry.optMethod.selectedIndex == 0) {
                                    strPrompt=strPrompt+'Please specify the Valuation Method\\n';
                                    if ( objFocus == null ) {
                                        objFocus = document.frmEntry.optMethod;
                                    }
                                }
                            } 
        

        my problem here is that even if document.frmEntry.optMethod is not equal to "undefined" the code still go into the condition as if it was true. i have attached a proof in my debuger. http://www.vetauinvest.com/Example/IE_DEBUGGER.png[^] Thanks

        Vuyiswa Maseko, Spoted in Daniweb-- Sorry to rant. I hate websites. They are just wierd. They don't behave like normal code. C#/VB.NET/ASP.NET/SQL7/2000/2005/2008 http://www.vuyiswamaseko.com vuyiswa[at]dotnetfunda.com http://www.Dotnetfunda.com

        Kornfeld Eliyahu PeterK Offline
        Kornfeld Eliyahu PeterK Offline
        Kornfeld Eliyahu Peter
        wrote on last edited by
        #3

        That should be undefined and NOT "undefined"!!!

        I'm not questioning your powers of observation; I'm merely remarking upon the paradox of asking a masked man who he is. (V)

        "It never ceases to amaze me that a spacecraft launched in 1977 can be fixed remotely from Earth." ― Brian Cox

        1 Reply Last reply
        0
        • V Vimalsoft Pty Ltd

          Good Day all i am reluctantly maintaining a Classic Asp application that will be re-written in few months to come. i have an issue where i have a Code like this

          if(document.frmEntry.optMethod != "undefined")
                              {
                                  if (document.frmEntry.optMethod.selectedIndex == 0) {
                                      strPrompt=strPrompt+'Please specify the Valuation Method\\n';
                                      if ( objFocus == null ) {
                                          objFocus = document.frmEntry.optMethod;
                                      }
                                  }
                              } 
          

          my problem here is that even if document.frmEntry.optMethod is not equal to "undefined" the code still go into the condition as if it was true. i have attached a proof in my debuger. http://www.vetauinvest.com/Example/IE_DEBUGGER.png[^] Thanks

          Vuyiswa Maseko, Spoted in Daniweb-- Sorry to rant. I hate websites. They are just wierd. They don't behave like normal code. C#/VB.NET/ASP.NET/SQL7/2000/2005/2008 http://www.vuyiswamaseko.com vuyiswa[at]dotnetfunda.com http://www.Dotnetfunda.com

          G Offline
          G Offline
          Graham Breach
          wrote on last edited by
          #4

          The "undefined" shown in the debugger is not a string, so you can't compare it as one. To really test if your value is undefined, you can use this:

          if(typeof document.frmEntry.optMethod != 'undefined')

          The typeof operator returns the type of the argument as a string.

          Kornfeld Eliyahu PeterK 1 Reply Last reply
          0
          • G Graham Breach

            The "undefined" shown in the debugger is not a string, so you can't compare it as one. To really test if your value is undefined, you can use this:

            if(typeof document.frmEntry.optMethod != 'undefined')

            The typeof operator returns the type of the argument as a string.

            Kornfeld Eliyahu PeterK Offline
            Kornfeld Eliyahu PeterK Offline
            Kornfeld Eliyahu Peter
            wrote on last edited by
            #5

            I can't see why use undefined as string - with or without typeof!? In JavaScript undefined is a value property of the global namespace and yous should use it according to it...

            I'm not questioning your powers of observation; I'm merely remarking upon the paradox of asking a masked man who he is. (V)

            "It never ceases to amaze me that a spacecraft launched in 1977 can be fixed remotely from Earth." ― Brian Cox

            V G 2 Replies Last reply
            0
            • B Blikkies

              Try using a Falsy check. The following values are always falsy: false 0 (zero) "" (empty string) null undefined NaN (a special Number value meaning Not-a-Number!) Your code should look like as follows:

              if(!document.frmEntry.optMethod)
              {
              if (document.frmEntry.optMethod.selectedIndex == 0) {
              strPrompt=strPrompt+'Please specify the Valuation Method\n';
              if ( objFocus == null ) {
              objFocus = document.frmEntry.optMethod;
              }
              }
              }

              Kornfeld Eliyahu PeterK Offline
              Kornfeld Eliyahu PeterK Offline
              Kornfeld Eliyahu Peter
              wrote on last edited by
              #6

              You are very true about 'false' check, but sometimes it is a good practice to write full code and explicitly state against what are you want to check. It's of course only a matter of maintainability/readability...

              I'm not questioning your powers of observation; I'm merely remarking upon the paradox of asking a masked man who he is. (V)

              "It never ceases to amaze me that a spacecraft launched in 1977 can be fixed remotely from Earth." ― Brian Cox

              1 Reply Last reply
              0
              • Kornfeld Eliyahu PeterK Kornfeld Eliyahu Peter

                I can't see why use undefined as string - with or without typeof!? In JavaScript undefined is a value property of the global namespace and yous should use it according to it...

                I'm not questioning your powers of observation; I'm merely remarking upon the paradox of asking a masked man who he is. (V)

                V Offline
                V Offline
                Vimalsoft Pty Ltd
                wrote on last edited by
                #7

                Thank you , this info is enough to solve the issue. thanks again

                Vuyiswa Maseko, Spoted in Daniweb-- Sorry to rant. I hate websites. They are just wierd. They don't behave like normal code. C#/VB.NET/ASP.NET/SQL7/2000/2005/2008 http://www.vuyiswamaseko.com vuyiswa[at]dotnetfunda.com http://www.Dotnetfunda.com

                1 Reply Last reply
                0
                • Kornfeld Eliyahu PeterK Kornfeld Eliyahu Peter

                  I can't see why use undefined as string - with or without typeof!? In JavaScript undefined is a value property of the global namespace and yous should use it according to it...

                  I'm not questioning your powers of observation; I'm merely remarking upon the paradox of asking a masked man who he is. (V)

                  G Offline
                  G Offline
                  Graham Breach
                  wrote on last edited by
                  #8

                  The typeof for an undefined value is always "undefined". The value undefined being in the global namespace opens up some possible problems:

                  function madness() {
                  // override with local variable
                  var a = 'fish', undefined = 'fish';

                  if(a === undefined) {
                  // both 'fish', so here we are
                  }
                  }

                  function mistake() {
                  var a = 'fish';

                  // common mistake, using '=' instead of '=='
                  if(undefined = a) {
                  // 'undefined' is not a constant or keyword, so the whole
                  // expression is valid and will be true
                  }
                  }

                  I know these are pretty unlikely scenarios, but better safe than sorry. In earlier browsers "undefined" was not a testable value so expressions like (a == undefined) would produce the helpful message "undefined is undefined".

                  Kornfeld Eliyahu PeterK 1 Reply Last reply
                  0
                  • V Vimalsoft Pty Ltd

                    Good Day all i am reluctantly maintaining a Classic Asp application that will be re-written in few months to come. i have an issue where i have a Code like this

                    if(document.frmEntry.optMethod != "undefined")
                                        {
                                            if (document.frmEntry.optMethod.selectedIndex == 0) {
                                                strPrompt=strPrompt+'Please specify the Valuation Method\\n';
                                                if ( objFocus == null ) {
                                                    objFocus = document.frmEntry.optMethod;
                                                }
                                            }
                                        } 
                    

                    my problem here is that even if document.frmEntry.optMethod is not equal to "undefined" the code still go into the condition as if it was true. i have attached a proof in my debuger. http://www.vetauinvest.com/Example/IE_DEBUGGER.png[^] Thanks

                    Vuyiswa Maseko, Spoted in Daniweb-- Sorry to rant. I hate websites. They are just wierd. They don't behave like normal code. C#/VB.NET/ASP.NET/SQL7/2000/2005/2008 http://www.vuyiswamaseko.com vuyiswa[at]dotnetfunda.com http://www.Dotnetfunda.com

                    J Offline
                    J Offline
                    jkirkerx
                    wrote on last edited by
                    #9

                    Vuyiswa Maseko wrote:

                    if(document.frmEntry.optMethod != "undefined")

                    I would of wrote it like this. I'm pretty sure undefined is like NaN, it's a recognized parameter.

                    if(document.frmEntry.optMethod !== undefined) {

                    }

                    1 Reply Last reply
                    0
                    • G Graham Breach

                      The typeof for an undefined value is always "undefined". The value undefined being in the global namespace opens up some possible problems:

                      function madness() {
                      // override with local variable
                      var a = 'fish', undefined = 'fish';

                      if(a === undefined) {
                      // both 'fish', so here we are
                      }
                      }

                      function mistake() {
                      var a = 'fish';

                      // common mistake, using '=' instead of '=='
                      if(undefined = a) {
                      // 'undefined' is not a constant or keyword, so the whole
                      // expression is valid and will be true
                      }
                      }

                      I know these are pretty unlikely scenarios, but better safe than sorry. In earlier browsers "undefined" was not a testable value so expressions like (a == undefined) would produce the helpful message "undefined is undefined".

                      Kornfeld Eliyahu PeterK Offline
                      Kornfeld Eliyahu PeterK Offline
                      Kornfeld Eliyahu Peter
                      wrote on last edited by
                      #10

                      Graham Breach wrote:

                      The typeof for an undefined value is always "undefined".

                      How that?

                      var undefined = 'fish';
                      alert(typeof undefined); // string!!!

                      I'm not questioning your powers of observation; I'm merely remarking upon the paradox of asking a masked man who he is. (V)

                      "It never ceases to amaze me that a spacecraft launched in 1977 can be fixed remotely from Earth." ― Brian Cox

                      G 1 Reply Last reply
                      0
                      • Kornfeld Eliyahu PeterK Kornfeld Eliyahu Peter

                        Graham Breach wrote:

                        The typeof for an undefined value is always "undefined".

                        How that?

                        var undefined = 'fish';
                        alert(typeof undefined); // string!!!

                        I'm not questioning your powers of observation; I'm merely remarking upon the paradox of asking a masked man who he is. (V)

                        G Offline
                        G Offline
                        Graham Breach
                        wrote on last edited by
                        #11

                        Yes. If you define undefined as a string variable, then it is no longer undefined and things get confusing. This is why I was testing the type of the value against the string "undefined" - the string is a constant.

                        1 Reply Last reply
                        0
                        • V Vimalsoft Pty Ltd

                          Good Day all i am reluctantly maintaining a Classic Asp application that will be re-written in few months to come. i have an issue where i have a Code like this

                          if(document.frmEntry.optMethod != "undefined")
                                              {
                                                  if (document.frmEntry.optMethod.selectedIndex == 0) {
                                                      strPrompt=strPrompt+'Please specify the Valuation Method\\n';
                                                      if ( objFocus == null ) {
                                                          objFocus = document.frmEntry.optMethod;
                                                      }
                                                  }
                                              } 
                          

                          my problem here is that even if document.frmEntry.optMethod is not equal to "undefined" the code still go into the condition as if it was true. i have attached a proof in my debuger. http://www.vetauinvest.com/Example/IE_DEBUGGER.png[^] Thanks

                          Vuyiswa Maseko, Spoted in Daniweb-- Sorry to rant. I hate websites. They are just wierd. They don't behave like normal code. C#/VB.NET/ASP.NET/SQL7/2000/2005/2008 http://www.vuyiswamaseko.com vuyiswa[at]dotnetfunda.com http://www.Dotnetfunda.com

                          T Offline
                          T Offline
                          Tushar Guru
                          wrote on last edited by
                          #12

                          You can try with remove ("") double quotes OR use ('') single quotes.

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups