Difference between “value.toString” and “value.toString()”












1














somewhere when I was going through a piece code I saw a line and I could not find any proper explanation of it on the internet.



value.toString and value.toString()


please if anyone could help me with the difference between the above two?










share|improve this question




















  • 1




    The first statement verifies that value has a truthy value for its toString property and the second statement attempts to execute it as a function.
    – André Dion
    Nov 23 '18 at 11:43










  • @RıdvanSumset, huh?
    – André Dion
    Nov 23 '18 at 11:45










  • toString() is a method to convert a value to string. there is no such thing toString. maybe it's a property (or a method) under an object in your code.
    – Rıdvan Sumset
    Nov 23 '18 at 11:45












  • @AndréDion I edited my comment to make it clearer for you.
    – Rıdvan Sumset
    Nov 23 '18 at 11:51
















1














somewhere when I was going through a piece code I saw a line and I could not find any proper explanation of it on the internet.



value.toString and value.toString()


please if anyone could help me with the difference between the above two?










share|improve this question




















  • 1




    The first statement verifies that value has a truthy value for its toString property and the second statement attempts to execute it as a function.
    – André Dion
    Nov 23 '18 at 11:43










  • @RıdvanSumset, huh?
    – André Dion
    Nov 23 '18 at 11:45










  • toString() is a method to convert a value to string. there is no such thing toString. maybe it's a property (or a method) under an object in your code.
    – Rıdvan Sumset
    Nov 23 '18 at 11:45












  • @AndréDion I edited my comment to make it clearer for you.
    – Rıdvan Sumset
    Nov 23 '18 at 11:51














1












1








1







somewhere when I was going through a piece code I saw a line and I could not find any proper explanation of it on the internet.



value.toString and value.toString()


please if anyone could help me with the difference between the above two?










share|improve this question















somewhere when I was going through a piece code I saw a line and I could not find any proper explanation of it on the internet.



value.toString and value.toString()


please if anyone could help me with the difference between the above two?







javascript






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 '18 at 12:11









elegant-user

1,83641334




1,83641334










asked Nov 23 '18 at 11:40









Divya SinghDivya Singh

144




144








  • 1




    The first statement verifies that value has a truthy value for its toString property and the second statement attempts to execute it as a function.
    – André Dion
    Nov 23 '18 at 11:43










  • @RıdvanSumset, huh?
    – André Dion
    Nov 23 '18 at 11:45










  • toString() is a method to convert a value to string. there is no such thing toString. maybe it's a property (or a method) under an object in your code.
    – Rıdvan Sumset
    Nov 23 '18 at 11:45












  • @AndréDion I edited my comment to make it clearer for you.
    – Rıdvan Sumset
    Nov 23 '18 at 11:51














  • 1




    The first statement verifies that value has a truthy value for its toString property and the second statement attempts to execute it as a function.
    – André Dion
    Nov 23 '18 at 11:43










  • @RıdvanSumset, huh?
    – André Dion
    Nov 23 '18 at 11:45










  • toString() is a method to convert a value to string. there is no such thing toString. maybe it's a property (or a method) under an object in your code.
    – Rıdvan Sumset
    Nov 23 '18 at 11:45












  • @AndréDion I edited my comment to make it clearer for you.
    – Rıdvan Sumset
    Nov 23 '18 at 11:51








1




1




The first statement verifies that value has a truthy value for its toString property and the second statement attempts to execute it as a function.
– André Dion
Nov 23 '18 at 11:43




The first statement verifies that value has a truthy value for its toString property and the second statement attempts to execute it as a function.
– André Dion
Nov 23 '18 at 11:43












@RıdvanSumset, huh?
– André Dion
Nov 23 '18 at 11:45




@RıdvanSumset, huh?
– André Dion
Nov 23 '18 at 11:45












toString() is a method to convert a value to string. there is no such thing toString. maybe it's a property (or a method) under an object in your code.
– Rıdvan Sumset
Nov 23 '18 at 11:45






toString() is a method to convert a value to string. there is no such thing toString. maybe it's a property (or a method) under an object in your code.
– Rıdvan Sumset
Nov 23 '18 at 11:45














@AndréDion I edited my comment to make it clearer for you.
– Rıdvan Sumset
Nov 23 '18 at 11:51




@AndréDion I edited my comment to make it clearer for you.
– Rıdvan Sumset
Nov 23 '18 at 11:51












4 Answers
4






active

oldest

votes


















0














There is nothing Like "toString" in javascript.
Whereas the function toString() is used to convert value into the String value.



var a = 10;
a.toString();
// Here a will be converted into String ("10")





share|improve this answer





















  • “There is nothing Like "toString" in javascript. Whereas the function toString() [...]” - both are the same thing - foo.bar just references a method bar of the foo object, whereas foo.bar() would call the method.
    – misorude
    Nov 23 '18 at 11:52










  • toString returns the defination of the method. whereas toString() is a method of the Object Class
    – aditya agnihotri
    Nov 23 '18 at 12:33



















0














This statement



value.toString


checks whether the variable value has a property named toString. This statement



value.toString()


retrieves the property named toString and invokes it as a method of value.



I suppose the intention here is to avoid exceptions in case the value lacks a toString method:



// If the value has a toString method, invoke it:
value.toString && value.toString()


(Note that I replaced and with the && operator.)



Normally, every JavaScript value will have a toString method (even numbers and Booleans!), so this extra test is unnecessary. Hypothetically one could construct an object where the toString property is explicitly erased:



var value = { toString: undefined };


and in this case, it would make sense to test for the existence of toString before attempting to invoke it.






share|improve this answer





























    0














    value.toString() -> It will convert any value to string and return that value.



    value.toString -> It will work as a primitive value and it will create an object wrapper. That's why it will not give any error or warning instead it will return a reference to the toString method.



    So in short value.toSting() will return a string and value.toString will return a reference to the toString method.






    share|improve this answer























    • “and value.toString will return an undefined” - no, it will return a reference to the toString method.
      – misorude
      Nov 23 '18 at 11:53










    • Yes you are right it will return a reference to the toString method
      – Kunal
      Nov 23 '18 at 11:55



















    0














    The value.toString is a function in JavaScript which can be used
    as
    value.toString()



    If write and test value.toString in console you see below.



    ƒ toString() { [native code] }






    share|improve this answer























    • nope, value.toString() do not return that but toString does.
      – Rıdvan Sumset
      Nov 23 '18 at 12:01











    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%2f53446031%2fdifference-between-value-tostring-and-value-tostring%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









    0














    There is nothing Like "toString" in javascript.
    Whereas the function toString() is used to convert value into the String value.



    var a = 10;
    a.toString();
    // Here a will be converted into String ("10")





    share|improve this answer





















    • “There is nothing Like "toString" in javascript. Whereas the function toString() [...]” - both are the same thing - foo.bar just references a method bar of the foo object, whereas foo.bar() would call the method.
      – misorude
      Nov 23 '18 at 11:52










    • toString returns the defination of the method. whereas toString() is a method of the Object Class
      – aditya agnihotri
      Nov 23 '18 at 12:33
















    0














    There is nothing Like "toString" in javascript.
    Whereas the function toString() is used to convert value into the String value.



    var a = 10;
    a.toString();
    // Here a will be converted into String ("10")





    share|improve this answer





















    • “There is nothing Like "toString" in javascript. Whereas the function toString() [...]” - both are the same thing - foo.bar just references a method bar of the foo object, whereas foo.bar() would call the method.
      – misorude
      Nov 23 '18 at 11:52










    • toString returns the defination of the method. whereas toString() is a method of the Object Class
      – aditya agnihotri
      Nov 23 '18 at 12:33














    0












    0








    0






    There is nothing Like "toString" in javascript.
    Whereas the function toString() is used to convert value into the String value.



    var a = 10;
    a.toString();
    // Here a will be converted into String ("10")





    share|improve this answer












    There is nothing Like "toString" in javascript.
    Whereas the function toString() is used to convert value into the String value.



    var a = 10;
    a.toString();
    // Here a will be converted into String ("10")






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 23 '18 at 11:49









    aditya agnihotriaditya agnihotri

    1




    1












    • “There is nothing Like "toString" in javascript. Whereas the function toString() [...]” - both are the same thing - foo.bar just references a method bar of the foo object, whereas foo.bar() would call the method.
      – misorude
      Nov 23 '18 at 11:52










    • toString returns the defination of the method. whereas toString() is a method of the Object Class
      – aditya agnihotri
      Nov 23 '18 at 12:33


















    • “There is nothing Like "toString" in javascript. Whereas the function toString() [...]” - both are the same thing - foo.bar just references a method bar of the foo object, whereas foo.bar() would call the method.
      – misorude
      Nov 23 '18 at 11:52










    • toString returns the defination of the method. whereas toString() is a method of the Object Class
      – aditya agnihotri
      Nov 23 '18 at 12:33
















    “There is nothing Like "toString" in javascript. Whereas the function toString() [...]” - both are the same thing - foo.bar just references a method bar of the foo object, whereas foo.bar() would call the method.
    – misorude
    Nov 23 '18 at 11:52




    “There is nothing Like "toString" in javascript. Whereas the function toString() [...]” - both are the same thing - foo.bar just references a method bar of the foo object, whereas foo.bar() would call the method.
    – misorude
    Nov 23 '18 at 11:52












    toString returns the defination of the method. whereas toString() is a method of the Object Class
    – aditya agnihotri
    Nov 23 '18 at 12:33




    toString returns the defination of the method. whereas toString() is a method of the Object Class
    – aditya agnihotri
    Nov 23 '18 at 12:33













    0














    This statement



    value.toString


    checks whether the variable value has a property named toString. This statement



    value.toString()


    retrieves the property named toString and invokes it as a method of value.



    I suppose the intention here is to avoid exceptions in case the value lacks a toString method:



    // If the value has a toString method, invoke it:
    value.toString && value.toString()


    (Note that I replaced and with the && operator.)



    Normally, every JavaScript value will have a toString method (even numbers and Booleans!), so this extra test is unnecessary. Hypothetically one could construct an object where the toString property is explicitly erased:



    var value = { toString: undefined };


    and in this case, it would make sense to test for the existence of toString before attempting to invoke it.






    share|improve this answer


























      0














      This statement



      value.toString


      checks whether the variable value has a property named toString. This statement



      value.toString()


      retrieves the property named toString and invokes it as a method of value.



      I suppose the intention here is to avoid exceptions in case the value lacks a toString method:



      // If the value has a toString method, invoke it:
      value.toString && value.toString()


      (Note that I replaced and with the && operator.)



      Normally, every JavaScript value will have a toString method (even numbers and Booleans!), so this extra test is unnecessary. Hypothetically one could construct an object where the toString property is explicitly erased:



      var value = { toString: undefined };


      and in this case, it would make sense to test for the existence of toString before attempting to invoke it.






      share|improve this answer
























        0












        0








        0






        This statement



        value.toString


        checks whether the variable value has a property named toString. This statement



        value.toString()


        retrieves the property named toString and invokes it as a method of value.



        I suppose the intention here is to avoid exceptions in case the value lacks a toString method:



        // If the value has a toString method, invoke it:
        value.toString && value.toString()


        (Note that I replaced and with the && operator.)



        Normally, every JavaScript value will have a toString method (even numbers and Booleans!), so this extra test is unnecessary. Hypothetically one could construct an object where the toString property is explicitly erased:



        var value = { toString: undefined };


        and in this case, it would make sense to test for the existence of toString before attempting to invoke it.






        share|improve this answer












        This statement



        value.toString


        checks whether the variable value has a property named toString. This statement



        value.toString()


        retrieves the property named toString and invokes it as a method of value.



        I suppose the intention here is to avoid exceptions in case the value lacks a toString method:



        // If the value has a toString method, invoke it:
        value.toString && value.toString()


        (Note that I replaced and with the && operator.)



        Normally, every JavaScript value will have a toString method (even numbers and Booleans!), so this extra test is unnecessary. Hypothetically one could construct an object where the toString property is explicitly erased:



        var value = { toString: undefined };


        and in this case, it would make sense to test for the existence of toString before attempting to invoke it.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 23 '18 at 11:55









        Pedro LMPedro LM

        43727




        43727























            0














            value.toString() -> It will convert any value to string and return that value.



            value.toString -> It will work as a primitive value and it will create an object wrapper. That's why it will not give any error or warning instead it will return a reference to the toString method.



            So in short value.toSting() will return a string and value.toString will return a reference to the toString method.






            share|improve this answer























            • “and value.toString will return an undefined” - no, it will return a reference to the toString method.
              – misorude
              Nov 23 '18 at 11:53










            • Yes you are right it will return a reference to the toString method
              – Kunal
              Nov 23 '18 at 11:55
















            0














            value.toString() -> It will convert any value to string and return that value.



            value.toString -> It will work as a primitive value and it will create an object wrapper. That's why it will not give any error or warning instead it will return a reference to the toString method.



            So in short value.toSting() will return a string and value.toString will return a reference to the toString method.






            share|improve this answer























            • “and value.toString will return an undefined” - no, it will return a reference to the toString method.
              – misorude
              Nov 23 '18 at 11:53










            • Yes you are right it will return a reference to the toString method
              – Kunal
              Nov 23 '18 at 11:55














            0












            0








            0






            value.toString() -> It will convert any value to string and return that value.



            value.toString -> It will work as a primitive value and it will create an object wrapper. That's why it will not give any error or warning instead it will return a reference to the toString method.



            So in short value.toSting() will return a string and value.toString will return a reference to the toString method.






            share|improve this answer














            value.toString() -> It will convert any value to string and return that value.



            value.toString -> It will work as a primitive value and it will create an object wrapper. That's why it will not give any error or warning instead it will return a reference to the toString method.



            So in short value.toSting() will return a string and value.toString will return a reference to the toString method.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 23 '18 at 11:56

























            answered Nov 23 '18 at 11:52









            KunalKunal

            1699




            1699












            • “and value.toString will return an undefined” - no, it will return a reference to the toString method.
              – misorude
              Nov 23 '18 at 11:53










            • Yes you are right it will return a reference to the toString method
              – Kunal
              Nov 23 '18 at 11:55


















            • “and value.toString will return an undefined” - no, it will return a reference to the toString method.
              – misorude
              Nov 23 '18 at 11:53










            • Yes you are right it will return a reference to the toString method
              – Kunal
              Nov 23 '18 at 11:55
















            “and value.toString will return an undefined” - no, it will return a reference to the toString method.
            – misorude
            Nov 23 '18 at 11:53




            “and value.toString will return an undefined” - no, it will return a reference to the toString method.
            – misorude
            Nov 23 '18 at 11:53












            Yes you are right it will return a reference to the toString method
            – Kunal
            Nov 23 '18 at 11:55




            Yes you are right it will return a reference to the toString method
            – Kunal
            Nov 23 '18 at 11:55











            0














            The value.toString is a function in JavaScript which can be used
            as
            value.toString()



            If write and test value.toString in console you see below.



            ƒ toString() { [native code] }






            share|improve this answer























            • nope, value.toString() do not return that but toString does.
              – Rıdvan Sumset
              Nov 23 '18 at 12:01
















            0














            The value.toString is a function in JavaScript which can be used
            as
            value.toString()



            If write and test value.toString in console you see below.



            ƒ toString() { [native code] }






            share|improve this answer























            • nope, value.toString() do not return that but toString does.
              – Rıdvan Sumset
              Nov 23 '18 at 12:01














            0












            0








            0






            The value.toString is a function in JavaScript which can be used
            as
            value.toString()



            If write and test value.toString in console you see below.



            ƒ toString() { [native code] }






            share|improve this answer














            The value.toString is a function in JavaScript which can be used
            as
            value.toString()



            If write and test value.toString in console you see below.



            ƒ toString() { [native code] }







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 23 '18 at 12:10

























            answered Nov 23 '18 at 11:55









            elegant-userelegant-user

            1,83641334




            1,83641334












            • nope, value.toString() do not return that but toString does.
              – Rıdvan Sumset
              Nov 23 '18 at 12:01


















            • nope, value.toString() do not return that but toString does.
              – Rıdvan Sumset
              Nov 23 '18 at 12:01
















            nope, value.toString() do not return that but toString does.
            – Rıdvan Sumset
            Nov 23 '18 at 12:01




            nope, value.toString() do not return that but toString does.
            – Rıdvan Sumset
            Nov 23 '18 at 12:01


















            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.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • 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%2f53446031%2fdifference-between-value-tostring-and-value-tostring%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

            Trompette piccolo

            How do I get these specific pathlines to nodes?

            What visual should I use to simply compare current year value vs last year in Power BI desktop