Why does indexOf not break?












9















I made a typo in TypeScript which was picked up during code review.



I used someArray.indexOf[someObject] instead of someArray.indexOf(someObject);



I would expect an error from the IDE/Compiler. Instead, no errors were raised and the result was simply undefined.



Can anyone explain this?










share|improve this question

























  • What did you assign the result of? Because what you wrote is valid you are taking the someObject member of the indexOf method. Well, trying to. The only error would come from TypeScript compilation and only if you try to assign the result to something that doesn't match the expected type.

    – vlaz
    51 mins ago






  • 1





    Welcome to javascript, where everything is an object!

    – Jean-Baptiste Yunès
    50 mins ago






  • 2





    @Jean-BaptisteYunès including null...

    – vlaz
    50 mins ago











  • @vlaz sarcasm ?

    – Florian
    36 mins ago











  • @Florian unfortunately, no: typeof null; // "object"

    – vlaz
    34 mins ago
















9















I made a typo in TypeScript which was picked up during code review.



I used someArray.indexOf[someObject] instead of someArray.indexOf(someObject);



I would expect an error from the IDE/Compiler. Instead, no errors were raised and the result was simply undefined.



Can anyone explain this?










share|improve this question

























  • What did you assign the result of? Because what you wrote is valid you are taking the someObject member of the indexOf method. Well, trying to. The only error would come from TypeScript compilation and only if you try to assign the result to something that doesn't match the expected type.

    – vlaz
    51 mins ago






  • 1





    Welcome to javascript, where everything is an object!

    – Jean-Baptiste Yunès
    50 mins ago






  • 2





    @Jean-BaptisteYunès including null...

    – vlaz
    50 mins ago











  • @vlaz sarcasm ?

    – Florian
    36 mins ago











  • @Florian unfortunately, no: typeof null; // "object"

    – vlaz
    34 mins ago














9












9








9








I made a typo in TypeScript which was picked up during code review.



I used someArray.indexOf[someObject] instead of someArray.indexOf(someObject);



I would expect an error from the IDE/Compiler. Instead, no errors were raised and the result was simply undefined.



Can anyone explain this?










share|improve this question
















I made a typo in TypeScript which was picked up during code review.



I used someArray.indexOf[someObject] instead of someArray.indexOf(someObject);



I would expect an error from the IDE/Compiler. Instead, no errors were raised and the result was simply undefined.



Can anyone explain this?







javascript angular typescript






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 44 mins ago







De Wet van As

















asked 54 mins ago









De Wet van AsDe Wet van As

736




736













  • What did you assign the result of? Because what you wrote is valid you are taking the someObject member of the indexOf method. Well, trying to. The only error would come from TypeScript compilation and only if you try to assign the result to something that doesn't match the expected type.

    – vlaz
    51 mins ago






  • 1





    Welcome to javascript, where everything is an object!

    – Jean-Baptiste Yunès
    50 mins ago






  • 2





    @Jean-BaptisteYunès including null...

    – vlaz
    50 mins ago











  • @vlaz sarcasm ?

    – Florian
    36 mins ago











  • @Florian unfortunately, no: typeof null; // "object"

    – vlaz
    34 mins ago



















  • What did you assign the result of? Because what you wrote is valid you are taking the someObject member of the indexOf method. Well, trying to. The only error would come from TypeScript compilation and only if you try to assign the result to something that doesn't match the expected type.

    – vlaz
    51 mins ago






  • 1





    Welcome to javascript, where everything is an object!

    – Jean-Baptiste Yunès
    50 mins ago






  • 2





    @Jean-BaptisteYunès including null...

    – vlaz
    50 mins ago











  • @vlaz sarcasm ?

    – Florian
    36 mins ago











  • @Florian unfortunately, no: typeof null; // "object"

    – vlaz
    34 mins ago

















What did you assign the result of? Because what you wrote is valid you are taking the someObject member of the indexOf method. Well, trying to. The only error would come from TypeScript compilation and only if you try to assign the result to something that doesn't match the expected type.

– vlaz
51 mins ago





What did you assign the result of? Because what you wrote is valid you are taking the someObject member of the indexOf method. Well, trying to. The only error would come from TypeScript compilation and only if you try to assign the result to something that doesn't match the expected type.

– vlaz
51 mins ago




1




1





Welcome to javascript, where everything is an object!

– Jean-Baptiste Yunès
50 mins ago





Welcome to javascript, where everything is an object!

– Jean-Baptiste Yunès
50 mins ago




2




2





@Jean-BaptisteYunès including null...

– vlaz
50 mins ago





@Jean-BaptisteYunès including null...

– vlaz
50 mins ago













@vlaz sarcasm ?

– Florian
36 mins ago





@vlaz sarcasm ?

– Florian
36 mins ago













@Florian unfortunately, no: typeof null; // "object"

– vlaz
34 mins ago





@Florian unfortunately, no: typeof null; // "object"

– vlaz
34 mins ago












4 Answers
4






active

oldest

votes


















12














Quite easy.



someArray.indexOf you know that this is a function, which is also an object and can have properties.



By doing someArray.indexOf[someObject], you are trying to reach the property with the key valued to the value of someObject.



Of course, it is not defined on the indexOf function, so it returns undefined.



Quick example that illustrates the syntax and the fact that a function can have properties ;) :






const array = ;
array.indexOf['anyValue'] = 'test';
console.log(array.indexOf.anyValue);








share|improve this answer



















  • 2





    I think answers here are missing the TypeScript tag. It's entirely reasonable to expect a compilation error in TS. Then again, it depends if you have index: number and index = arr.indexOf[obj] then that should be a compilation error. But index: any wouldn't throw a compilation error.

    – vlaz
    47 mins ago











  • @vlaz +1. sjahan gives OP a quick explanation of the undefined result but the main question remains...

    – Florian
    30 mins ago



















3














Array.indexOf is a function.



Functions are objects.



You were accessing the someObject property of the Array.indexOf function.



You would have got undefined.






const array = [1, 2, 3]
const someObject = 'asdasd'

console.log(array.indexOf[someObject])
// undefined








share|improve this answer































    0














    In short: Because that is the way the language is designed.



    JavaScript evaluates an attempt to access a property that doesn't exist as undefined.



    It doesn't raise an exception.






    share|improve this answer































      0














      Functions in JavaScript are first class objects.



      When you access function Array.indexOf() via bracket notation Array.indexOf['prop'] you actually trying to access a property which does not exist on indexOf so you get undefined.






      share|improve this answer
























      • What about TypeScript?

        – vlaz
        41 mins ago











      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54254396%2fwhy-does-indexof-not-break%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      4 Answers
      4






      active

      oldest

      votes








      4 Answers
      4






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      12














      Quite easy.



      someArray.indexOf you know that this is a function, which is also an object and can have properties.



      By doing someArray.indexOf[someObject], you are trying to reach the property with the key valued to the value of someObject.



      Of course, it is not defined on the indexOf function, so it returns undefined.



      Quick example that illustrates the syntax and the fact that a function can have properties ;) :






      const array = ;
      array.indexOf['anyValue'] = 'test';
      console.log(array.indexOf.anyValue);








      share|improve this answer



















      • 2





        I think answers here are missing the TypeScript tag. It's entirely reasonable to expect a compilation error in TS. Then again, it depends if you have index: number and index = arr.indexOf[obj] then that should be a compilation error. But index: any wouldn't throw a compilation error.

        – vlaz
        47 mins ago











      • @vlaz +1. sjahan gives OP a quick explanation of the undefined result but the main question remains...

        – Florian
        30 mins ago
















      12














      Quite easy.



      someArray.indexOf you know that this is a function, which is also an object and can have properties.



      By doing someArray.indexOf[someObject], you are trying to reach the property with the key valued to the value of someObject.



      Of course, it is not defined on the indexOf function, so it returns undefined.



      Quick example that illustrates the syntax and the fact that a function can have properties ;) :






      const array = ;
      array.indexOf['anyValue'] = 'test';
      console.log(array.indexOf.anyValue);








      share|improve this answer



















      • 2





        I think answers here are missing the TypeScript tag. It's entirely reasonable to expect a compilation error in TS. Then again, it depends if you have index: number and index = arr.indexOf[obj] then that should be a compilation error. But index: any wouldn't throw a compilation error.

        – vlaz
        47 mins ago











      • @vlaz +1. sjahan gives OP a quick explanation of the undefined result but the main question remains...

        – Florian
        30 mins ago














      12












      12








      12







      Quite easy.



      someArray.indexOf you know that this is a function, which is also an object and can have properties.



      By doing someArray.indexOf[someObject], you are trying to reach the property with the key valued to the value of someObject.



      Of course, it is not defined on the indexOf function, so it returns undefined.



      Quick example that illustrates the syntax and the fact that a function can have properties ;) :






      const array = ;
      array.indexOf['anyValue'] = 'test';
      console.log(array.indexOf.anyValue);








      share|improve this answer













      Quite easy.



      someArray.indexOf you know that this is a function, which is also an object and can have properties.



      By doing someArray.indexOf[someObject], you are trying to reach the property with the key valued to the value of someObject.



      Of course, it is not defined on the indexOf function, so it returns undefined.



      Quick example that illustrates the syntax and the fact that a function can have properties ;) :






      const array = ;
      array.indexOf['anyValue'] = 'test';
      console.log(array.indexOf.anyValue);








      const array = ;
      array.indexOf['anyValue'] = 'test';
      console.log(array.indexOf.anyValue);





      const array = ;
      array.indexOf['anyValue'] = 'test';
      console.log(array.indexOf.anyValue);






      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered 51 mins ago









      sjahansjahan

      3,1751827




      3,1751827








      • 2





        I think answers here are missing the TypeScript tag. It's entirely reasonable to expect a compilation error in TS. Then again, it depends if you have index: number and index = arr.indexOf[obj] then that should be a compilation error. But index: any wouldn't throw a compilation error.

        – vlaz
        47 mins ago











      • @vlaz +1. sjahan gives OP a quick explanation of the undefined result but the main question remains...

        – Florian
        30 mins ago














      • 2





        I think answers here are missing the TypeScript tag. It's entirely reasonable to expect a compilation error in TS. Then again, it depends if you have index: number and index = arr.indexOf[obj] then that should be a compilation error. But index: any wouldn't throw a compilation error.

        – vlaz
        47 mins ago











      • @vlaz +1. sjahan gives OP a quick explanation of the undefined result but the main question remains...

        – Florian
        30 mins ago








      2




      2





      I think answers here are missing the TypeScript tag. It's entirely reasonable to expect a compilation error in TS. Then again, it depends if you have index: number and index = arr.indexOf[obj] then that should be a compilation error. But index: any wouldn't throw a compilation error.

      – vlaz
      47 mins ago





      I think answers here are missing the TypeScript tag. It's entirely reasonable to expect a compilation error in TS. Then again, it depends if you have index: number and index = arr.indexOf[obj] then that should be a compilation error. But index: any wouldn't throw a compilation error.

      – vlaz
      47 mins ago













      @vlaz +1. sjahan gives OP a quick explanation of the undefined result but the main question remains...

      – Florian
      30 mins ago





      @vlaz +1. sjahan gives OP a quick explanation of the undefined result but the main question remains...

      – Florian
      30 mins ago













      3














      Array.indexOf is a function.



      Functions are objects.



      You were accessing the someObject property of the Array.indexOf function.



      You would have got undefined.






      const array = [1, 2, 3]
      const someObject = 'asdasd'

      console.log(array.indexOf[someObject])
      // undefined








      share|improve this answer




























        3














        Array.indexOf is a function.



        Functions are objects.



        You were accessing the someObject property of the Array.indexOf function.



        You would have got undefined.






        const array = [1, 2, 3]
        const someObject = 'asdasd'

        console.log(array.indexOf[someObject])
        // undefined








        share|improve this answer


























          3












          3








          3







          Array.indexOf is a function.



          Functions are objects.



          You were accessing the someObject property of the Array.indexOf function.



          You would have got undefined.






          const array = [1, 2, 3]
          const someObject = 'asdasd'

          console.log(array.indexOf[someObject])
          // undefined








          share|improve this answer













          Array.indexOf is a function.



          Functions are objects.



          You were accessing the someObject property of the Array.indexOf function.



          You would have got undefined.






          const array = [1, 2, 3]
          const someObject = 'asdasd'

          console.log(array.indexOf[someObject])
          // undefined








          const array = [1, 2, 3]
          const someObject = 'asdasd'

          console.log(array.indexOf[someObject])
          // undefined





          const array = [1, 2, 3]
          const someObject = 'asdasd'

          console.log(array.indexOf[someObject])
          // undefined






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 51 mins ago









          0xc14m1z0xc14m1z

          1,409512




          1,409512























              0














              In short: Because that is the way the language is designed.



              JavaScript evaluates an attempt to access a property that doesn't exist as undefined.



              It doesn't raise an exception.






              share|improve this answer




























                0














                In short: Because that is the way the language is designed.



                JavaScript evaluates an attempt to access a property that doesn't exist as undefined.



                It doesn't raise an exception.






                share|improve this answer


























                  0












                  0








                  0







                  In short: Because that is the way the language is designed.



                  JavaScript evaluates an attempt to access a property that doesn't exist as undefined.



                  It doesn't raise an exception.






                  share|improve this answer













                  In short: Because that is the way the language is designed.



                  JavaScript evaluates an attempt to access a property that doesn't exist as undefined.



                  It doesn't raise an exception.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered 51 mins ago









                  QuentinQuentin

                  642k718661036




                  642k718661036























                      0














                      Functions in JavaScript are first class objects.



                      When you access function Array.indexOf() via bracket notation Array.indexOf['prop'] you actually trying to access a property which does not exist on indexOf so you get undefined.






                      share|improve this answer
























                      • What about TypeScript?

                        – vlaz
                        41 mins ago
















                      0














                      Functions in JavaScript are first class objects.



                      When you access function Array.indexOf() via bracket notation Array.indexOf['prop'] you actually trying to access a property which does not exist on indexOf so you get undefined.






                      share|improve this answer
























                      • What about TypeScript?

                        – vlaz
                        41 mins ago














                      0












                      0








                      0







                      Functions in JavaScript are first class objects.



                      When you access function Array.indexOf() via bracket notation Array.indexOf['prop'] you actually trying to access a property which does not exist on indexOf so you get undefined.






                      share|improve this answer













                      Functions in JavaScript are first class objects.



                      When you access function Array.indexOf() via bracket notation Array.indexOf['prop'] you actually trying to access a property which does not exist on indexOf so you get undefined.







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered 42 mins ago









                      GibboKGibboK

                      34.3k107317542




                      34.3k107317542













                      • What about TypeScript?

                        – vlaz
                        41 mins ago



















                      • What about TypeScript?

                        – vlaz
                        41 mins ago

















                      What about TypeScript?

                      – vlaz
                      41 mins ago





                      What about TypeScript?

                      – vlaz
                      41 mins ago


















                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54254396%2fwhy-does-indexof-not-break%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      404 Error Contact Form 7 ajax form submitting

                      How to know if a Active Directory user can login interactively

                      TypeError: fit_transform() missing 1 required positional argument: 'X'