Why does [{“value”:“tag1”} turns into [object Object] when logged?












3















On my node.js server I have the following code:



var tags = [{"value":"tag1"},{"value":"tag2"}];
console.log("tags: " + tags);


I expected the console to say this:



tags: [{"value":"tag1"},{"value":"tag2"}]


But instead got this:



tags: [object Object],[object Object]


Why does this happen? It's causing problems in my code because I'm trying to access the values but can't.










share|improve this question




















  • 1





    using the + operator to concatenate an object with a string is like calling 'string' + obj.toString(), which ({}).toString() is [object Object]. Js attempts to convert the second item in the operation to the type of the first item, a string, hence why you're not seeing the contents of the array but rather [object Object].

    – harryparkdotio
    Jan 2 at 6:20













  • If you want to log it, do this - console.log({tags:tags}) rather than console.log("tags: " + tags). in this case it is trying to add a string to object thus giving you unexpected result.

    – Komal Bansal
    Jan 2 at 6:46
















3















On my node.js server I have the following code:



var tags = [{"value":"tag1"},{"value":"tag2"}];
console.log("tags: " + tags);


I expected the console to say this:



tags: [{"value":"tag1"},{"value":"tag2"}]


But instead got this:



tags: [object Object],[object Object]


Why does this happen? It's causing problems in my code because I'm trying to access the values but can't.










share|improve this question




















  • 1





    using the + operator to concatenate an object with a string is like calling 'string' + obj.toString(), which ({}).toString() is [object Object]. Js attempts to convert the second item in the operation to the type of the first item, a string, hence why you're not seeing the contents of the array but rather [object Object].

    – harryparkdotio
    Jan 2 at 6:20













  • If you want to log it, do this - console.log({tags:tags}) rather than console.log("tags: " + tags). in this case it is trying to add a string to object thus giving you unexpected result.

    – Komal Bansal
    Jan 2 at 6:46














3












3








3


1






On my node.js server I have the following code:



var tags = [{"value":"tag1"},{"value":"tag2"}];
console.log("tags: " + tags);


I expected the console to say this:



tags: [{"value":"tag1"},{"value":"tag2"}]


But instead got this:



tags: [object Object],[object Object]


Why does this happen? It's causing problems in my code because I'm trying to access the values but can't.










share|improve this question
















On my node.js server I have the following code:



var tags = [{"value":"tag1"},{"value":"tag2"}];
console.log("tags: " + tags);


I expected the console to say this:



tags: [{"value":"tag1"},{"value":"tag2"}]


But instead got this:



tags: [object Object],[object Object]


Why does this happen? It's causing problems in my code because I'm trying to access the values but can't.







javascript arrays node.js string object






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 7 at 2:00









Jack Bashford

12.1k31846




12.1k31846










asked Jan 2 at 6:14









Cole TaylorCole Taylor

355




355








  • 1





    using the + operator to concatenate an object with a string is like calling 'string' + obj.toString(), which ({}).toString() is [object Object]. Js attempts to convert the second item in the operation to the type of the first item, a string, hence why you're not seeing the contents of the array but rather [object Object].

    – harryparkdotio
    Jan 2 at 6:20













  • If you want to log it, do this - console.log({tags:tags}) rather than console.log("tags: " + tags). in this case it is trying to add a string to object thus giving you unexpected result.

    – Komal Bansal
    Jan 2 at 6:46














  • 1





    using the + operator to concatenate an object with a string is like calling 'string' + obj.toString(), which ({}).toString() is [object Object]. Js attempts to convert the second item in the operation to the type of the first item, a string, hence why you're not seeing the contents of the array but rather [object Object].

    – harryparkdotio
    Jan 2 at 6:20













  • If you want to log it, do this - console.log({tags:tags}) rather than console.log("tags: " + tags). in this case it is trying to add a string to object thus giving you unexpected result.

    – Komal Bansal
    Jan 2 at 6:46








1




1





using the + operator to concatenate an object with a string is like calling 'string' + obj.toString(), which ({}).toString() is [object Object]. Js attempts to convert the second item in the operation to the type of the first item, a string, hence why you're not seeing the contents of the array but rather [object Object].

– harryparkdotio
Jan 2 at 6:20







using the + operator to concatenate an object with a string is like calling 'string' + obj.toString(), which ({}).toString() is [object Object]. Js attempts to convert the second item in the operation to the type of the first item, a string, hence why you're not seeing the contents of the array but rather [object Object].

– harryparkdotio
Jan 2 at 6:20















If you want to log it, do this - console.log({tags:tags}) rather than console.log("tags: " + tags). in this case it is trying to add a string to object thus giving you unexpected result.

– Komal Bansal
Jan 2 at 6:46





If you want to log it, do this - console.log({tags:tags}) rather than console.log("tags: " + tags). in this case it is trying to add a string to object thus giving you unexpected result.

– Komal Bansal
Jan 2 at 6:46












6 Answers
6






active

oldest

votes


















1














When you create a concatenated string using the + operator, the .toString() method is called on the non-string parts to convert them to readable strings – and this method returns [object Object] for plain objects.



If you want to see the actual content of the array, use :





  • console.log("tags: ", tags); (when used in the browser's console, allows for an "interactive" log : you'll be able to click on the array and unfold its content) ;

  • or console.log("tags: " + JSON.stringify(tags)); if you just want to see the content of the array printed (use JSON.stringify(tags, null, 2) for pretty print with 2-spaces indent).






share|improve this answer

































    4














    When you do "tags: " + tags, the toString method of objects is called in order to do the operation.



    Change



    console.log("tags: " + tags);


    into



    console.log("tags: ", tags);


    so that the console.log function of node can do its own more interesting conversion.






    share|improve this answer































      4














      You have two options:



      1: Use the comma , instead of concatenating the strings together, to avoid toString() being called and creating [object Object]:






      var tags = [{"value": "tag1"}, {"value": "tag2"}];
      console.log("Tags: ", tags);





      2: Use JSON.stringify() on the object to convert it into a string which can be read:






      var tags = [{"value": "tag1"}, {"value": "tag2"}];
      console.log("Tags: ", JSON.stringify(tags));








      share|improve this answer































        1















        Why does this happen?




        It happens because when you try to concatenate any variable with a string using + operator, javascript converts the value of the variable to a string.






        share|improve this answer































          1














          You can also use JSON to log Objects properly if you want to concatenate the strings.






          var tags = [{
          "value": "tag1"
          }, {
          "value": "tag2"
          }];
          console.log("tags: " + JSON.stringify(tags))








          share|improve this answer

































            1














            '+' stringifies the object, thus results [object Object], You need to use JSON.stringify() to convert your object to a JSON string before using console with '+', otherwise use console with ",".






            share|improve this answer























              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%2f54002010%2fwhy-does-valuetag1-turns-into-object-object-when-logged%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              6 Answers
              6






              active

              oldest

              votes








              6 Answers
              6






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              1














              When you create a concatenated string using the + operator, the .toString() method is called on the non-string parts to convert them to readable strings – and this method returns [object Object] for plain objects.



              If you want to see the actual content of the array, use :





              • console.log("tags: ", tags); (when used in the browser's console, allows for an "interactive" log : you'll be able to click on the array and unfold its content) ;

              • or console.log("tags: " + JSON.stringify(tags)); if you just want to see the content of the array printed (use JSON.stringify(tags, null, 2) for pretty print with 2-spaces indent).






              share|improve this answer






























                1














                When you create a concatenated string using the + operator, the .toString() method is called on the non-string parts to convert them to readable strings – and this method returns [object Object] for plain objects.



                If you want to see the actual content of the array, use :





                • console.log("tags: ", tags); (when used in the browser's console, allows for an "interactive" log : you'll be able to click on the array and unfold its content) ;

                • or console.log("tags: " + JSON.stringify(tags)); if you just want to see the content of the array printed (use JSON.stringify(tags, null, 2) for pretty print with 2-spaces indent).






                share|improve this answer




























                  1












                  1








                  1







                  When you create a concatenated string using the + operator, the .toString() method is called on the non-string parts to convert them to readable strings – and this method returns [object Object] for plain objects.



                  If you want to see the actual content of the array, use :





                  • console.log("tags: ", tags); (when used in the browser's console, allows for an "interactive" log : you'll be able to click on the array and unfold its content) ;

                  • or console.log("tags: " + JSON.stringify(tags)); if you just want to see the content of the array printed (use JSON.stringify(tags, null, 2) for pretty print with 2-spaces indent).






                  share|improve this answer















                  When you create a concatenated string using the + operator, the .toString() method is called on the non-string parts to convert them to readable strings – and this method returns [object Object] for plain objects.



                  If you want to see the actual content of the array, use :





                  • console.log("tags: ", tags); (when used in the browser's console, allows for an "interactive" log : you'll be able to click on the array and unfold its content) ;

                  • or console.log("tags: " + JSON.stringify(tags)); if you just want to see the content of the array printed (use JSON.stringify(tags, null, 2) for pretty print with 2-spaces indent).







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Jan 2 at 6:39

























                  answered Jan 2 at 6:28









                  flawyteflawyte

                  5,02233050




                  5,02233050

























                      4














                      When you do "tags: " + tags, the toString method of objects is called in order to do the operation.



                      Change



                      console.log("tags: " + tags);


                      into



                      console.log("tags: ", tags);


                      so that the console.log function of node can do its own more interesting conversion.






                      share|improve this answer




























                        4














                        When you do "tags: " + tags, the toString method of objects is called in order to do the operation.



                        Change



                        console.log("tags: " + tags);


                        into



                        console.log("tags: ", tags);


                        so that the console.log function of node can do its own more interesting conversion.






                        share|improve this answer


























                          4












                          4








                          4







                          When you do "tags: " + tags, the toString method of objects is called in order to do the operation.



                          Change



                          console.log("tags: " + tags);


                          into



                          console.log("tags: ", tags);


                          so that the console.log function of node can do its own more interesting conversion.






                          share|improve this answer













                          When you do "tags: " + tags, the toString method of objects is called in order to do the operation.



                          Change



                          console.log("tags: " + tags);


                          into



                          console.log("tags: ", tags);


                          so that the console.log function of node can do its own more interesting conversion.







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Jan 2 at 6:15









                          Denys SéguretDenys Séguret

                          280k56587605




                          280k56587605























                              4














                              You have two options:



                              1: Use the comma , instead of concatenating the strings together, to avoid toString() being called and creating [object Object]:






                              var tags = [{"value": "tag1"}, {"value": "tag2"}];
                              console.log("Tags: ", tags);





                              2: Use JSON.stringify() on the object to convert it into a string which can be read:






                              var tags = [{"value": "tag1"}, {"value": "tag2"}];
                              console.log("Tags: ", JSON.stringify(tags));








                              share|improve this answer




























                                4














                                You have two options:



                                1: Use the comma , instead of concatenating the strings together, to avoid toString() being called and creating [object Object]:






                                var tags = [{"value": "tag1"}, {"value": "tag2"}];
                                console.log("Tags: ", tags);





                                2: Use JSON.stringify() on the object to convert it into a string which can be read:






                                var tags = [{"value": "tag1"}, {"value": "tag2"}];
                                console.log("Tags: ", JSON.stringify(tags));








                                share|improve this answer


























                                  4












                                  4








                                  4







                                  You have two options:



                                  1: Use the comma , instead of concatenating the strings together, to avoid toString() being called and creating [object Object]:






                                  var tags = [{"value": "tag1"}, {"value": "tag2"}];
                                  console.log("Tags: ", tags);





                                  2: Use JSON.stringify() on the object to convert it into a string which can be read:






                                  var tags = [{"value": "tag1"}, {"value": "tag2"}];
                                  console.log("Tags: ", JSON.stringify(tags));








                                  share|improve this answer













                                  You have two options:



                                  1: Use the comma , instead of concatenating the strings together, to avoid toString() being called and creating [object Object]:






                                  var tags = [{"value": "tag1"}, {"value": "tag2"}];
                                  console.log("Tags: ", tags);





                                  2: Use JSON.stringify() on the object to convert it into a string which can be read:






                                  var tags = [{"value": "tag1"}, {"value": "tag2"}];
                                  console.log("Tags: ", JSON.stringify(tags));








                                  var tags = [{"value": "tag1"}, {"value": "tag2"}];
                                  console.log("Tags: ", tags);





                                  var tags = [{"value": "tag1"}, {"value": "tag2"}];
                                  console.log("Tags: ", tags);





                                  var tags = [{"value": "tag1"}, {"value": "tag2"}];
                                  console.log("Tags: ", JSON.stringify(tags));





                                  var tags = [{"value": "tag1"}, {"value": "tag2"}];
                                  console.log("Tags: ", JSON.stringify(tags));






                                  share|improve this answer












                                  share|improve this answer



                                  share|improve this answer










                                  answered Jan 2 at 6:22









                                  Jack BashfordJack Bashford

                                  12.1k31846




                                  12.1k31846























                                      1















                                      Why does this happen?




                                      It happens because when you try to concatenate any variable with a string using + operator, javascript converts the value of the variable to a string.






                                      share|improve this answer




























                                        1















                                        Why does this happen?




                                        It happens because when you try to concatenate any variable with a string using + operator, javascript converts the value of the variable to a string.






                                        share|improve this answer


























                                          1












                                          1








                                          1








                                          Why does this happen?




                                          It happens because when you try to concatenate any variable with a string using + operator, javascript converts the value of the variable to a string.






                                          share|improve this answer














                                          Why does this happen?




                                          It happens because when you try to concatenate any variable with a string using + operator, javascript converts the value of the variable to a string.







                                          share|improve this answer












                                          share|improve this answer



                                          share|improve this answer










                                          answered Jan 2 at 6:18









                                          NickNick

                                          3261418




                                          3261418























                                              1














                                              You can also use JSON to log Objects properly if you want to concatenate the strings.






                                              var tags = [{
                                              "value": "tag1"
                                              }, {
                                              "value": "tag2"
                                              }];
                                              console.log("tags: " + JSON.stringify(tags))








                                              share|improve this answer






























                                                1














                                                You can also use JSON to log Objects properly if you want to concatenate the strings.






                                                var tags = [{
                                                "value": "tag1"
                                                }, {
                                                "value": "tag2"
                                                }];
                                                console.log("tags: " + JSON.stringify(tags))








                                                share|improve this answer




























                                                  1












                                                  1








                                                  1







                                                  You can also use JSON to log Objects properly if you want to concatenate the strings.






                                                  var tags = [{
                                                  "value": "tag1"
                                                  }, {
                                                  "value": "tag2"
                                                  }];
                                                  console.log("tags: " + JSON.stringify(tags))








                                                  share|improve this answer















                                                  You can also use JSON to log Objects properly if you want to concatenate the strings.






                                                  var tags = [{
                                                  "value": "tag1"
                                                  }, {
                                                  "value": "tag2"
                                                  }];
                                                  console.log("tags: " + JSON.stringify(tags))








                                                  var tags = [{
                                                  "value": "tag1"
                                                  }, {
                                                  "value": "tag2"
                                                  }];
                                                  console.log("tags: " + JSON.stringify(tags))





                                                  var tags = [{
                                                  "value": "tag1"
                                                  }, {
                                                  "value": "tag2"
                                                  }];
                                                  console.log("tags: " + JSON.stringify(tags))






                                                  share|improve this answer














                                                  share|improve this answer



                                                  share|improve this answer








                                                  edited Jan 2 at 6:22









                                                  Jack Bashford

                                                  12.1k31846




                                                  12.1k31846










                                                  answered Jan 2 at 6:17









                                                  Manash MandalManash Mandal

                                                  663




                                                  663























                                                      1














                                                      '+' stringifies the object, thus results [object Object], You need to use JSON.stringify() to convert your object to a JSON string before using console with '+', otherwise use console with ",".






                                                      share|improve this answer




























                                                        1














                                                        '+' stringifies the object, thus results [object Object], You need to use JSON.stringify() to convert your object to a JSON string before using console with '+', otherwise use console with ",".






                                                        share|improve this answer


























                                                          1












                                                          1








                                                          1







                                                          '+' stringifies the object, thus results [object Object], You need to use JSON.stringify() to convert your object to a JSON string before using console with '+', otherwise use console with ",".






                                                          share|improve this answer













                                                          '+' stringifies the object, thus results [object Object], You need to use JSON.stringify() to convert your object to a JSON string before using console with '+', otherwise use console with ",".







                                                          share|improve this answer












                                                          share|improve this answer



                                                          share|improve this answer










                                                          answered Jan 2 at 6:30









                                                          the_ultimate_developerthe_ultimate_developer

                                                          1,024722




                                                          1,024722






























                                                              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%2f54002010%2fwhy-does-valuetag1-turns-into-object-object-when-logged%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

                                                              Can a sorcerer learn a 5th-level spell early by creating spell slots using the Font of Magic feature?

                                                              Does disintegrating a polymorphed enemy still kill it after the 2018 errata?

                                                              A Topological Invariant for $pi_3(U(n))$