How does a prepending plus sign treat new Date?











up vote
0
down vote

favorite












I've recently come across this intriguing line of code:






var date = +new Date;

console.log(date);





So I experimented with putting + before strings to better understand what was going on and this was the result:






var one = +"1"; // -> 1
var negativeTwo = -"2"; // -> -2
var notReallyANumber = +"number: 1"; // -> NaN

console.log(one, negativeTwo, notReallyANumber);





It seems that whenever a + sign or a - sign is placed before a string, it converts that string into a number, and if a negative sign is placed before a string, the resulting number will be negative.



Finally, if the string is not numeric, the result will be NaN.



Why is that?



How does putting + or - signs before a string convert it into a number and how does it also apply to new Date?



EDIT:




How does a + sign affect new Date? How does the value Wed Nov 07 2018 21:50:30 GMT-0500 for example, convert into a numerical representation?











share|improve this question




















  • 4




    developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
    – Daniel A. White
    Nov 8 at 2:45










  • @CertainPerformance the question has been edited and is no longer a duplicate.
    – WEB_UI
    Nov 8 at 2:57










  • In JavaScript, each Object has a valueOf() method. Putting + in front of an Object calls that method on it, and Date overrides it: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… (also, +new Date is a syntax error, but JS is lenient and turns that into +new Date())
    – Chris G
    Nov 8 at 3:05

















up vote
0
down vote

favorite












I've recently come across this intriguing line of code:






var date = +new Date;

console.log(date);





So I experimented with putting + before strings to better understand what was going on and this was the result:






var one = +"1"; // -> 1
var negativeTwo = -"2"; // -> -2
var notReallyANumber = +"number: 1"; // -> NaN

console.log(one, negativeTwo, notReallyANumber);





It seems that whenever a + sign or a - sign is placed before a string, it converts that string into a number, and if a negative sign is placed before a string, the resulting number will be negative.



Finally, if the string is not numeric, the result will be NaN.



Why is that?



How does putting + or - signs before a string convert it into a number and how does it also apply to new Date?



EDIT:




How does a + sign affect new Date? How does the value Wed Nov 07 2018 21:50:30 GMT-0500 for example, convert into a numerical representation?











share|improve this question




















  • 4




    developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
    – Daniel A. White
    Nov 8 at 2:45










  • @CertainPerformance the question has been edited and is no longer a duplicate.
    – WEB_UI
    Nov 8 at 2:57










  • In JavaScript, each Object has a valueOf() method. Putting + in front of an Object calls that method on it, and Date overrides it: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… (also, +new Date is a syntax error, but JS is lenient and turns that into +new Date())
    – Chris G
    Nov 8 at 3:05















up vote
0
down vote

favorite









up vote
0
down vote

favorite











I've recently come across this intriguing line of code:






var date = +new Date;

console.log(date);





So I experimented with putting + before strings to better understand what was going on and this was the result:






var one = +"1"; // -> 1
var negativeTwo = -"2"; // -> -2
var notReallyANumber = +"number: 1"; // -> NaN

console.log(one, negativeTwo, notReallyANumber);





It seems that whenever a + sign or a - sign is placed before a string, it converts that string into a number, and if a negative sign is placed before a string, the resulting number will be negative.



Finally, if the string is not numeric, the result will be NaN.



Why is that?



How does putting + or - signs before a string convert it into a number and how does it also apply to new Date?



EDIT:




How does a + sign affect new Date? How does the value Wed Nov 07 2018 21:50:30 GMT-0500 for example, convert into a numerical representation?











share|improve this question















I've recently come across this intriguing line of code:






var date = +new Date;

console.log(date);





So I experimented with putting + before strings to better understand what was going on and this was the result:






var one = +"1"; // -> 1
var negativeTwo = -"2"; // -> -2
var notReallyANumber = +"number: 1"; // -> NaN

console.log(one, negativeTwo, notReallyANumber);





It seems that whenever a + sign or a - sign is placed before a string, it converts that string into a number, and if a negative sign is placed before a string, the resulting number will be negative.



Finally, if the string is not numeric, the result will be NaN.



Why is that?



How does putting + or - signs before a string convert it into a number and how does it also apply to new Date?



EDIT:




How does a + sign affect new Date? How does the value Wed Nov 07 2018 21:50:30 GMT-0500 for example, convert into a numerical representation?







var date = +new Date;

console.log(date);





var date = +new Date;

console.log(date);





var one = +"1"; // -> 1
var negativeTwo = -"2"; // -> -2
var notReallyANumber = +"number: 1"; // -> NaN

console.log(one, negativeTwo, notReallyANumber);





var one = +"1"; // -> 1
var negativeTwo = -"2"; // -> -2
var notReallyANumber = +"number: 1"; // -> NaN

console.log(one, negativeTwo, notReallyANumber);






javascript operators






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 8 at 2:55

























asked Nov 8 at 2:44









WEB_UI

3811420




3811420








  • 4




    developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
    – Daniel A. White
    Nov 8 at 2:45










  • @CertainPerformance the question has been edited and is no longer a duplicate.
    – WEB_UI
    Nov 8 at 2:57










  • In JavaScript, each Object has a valueOf() method. Putting + in front of an Object calls that method on it, and Date overrides it: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… (also, +new Date is a syntax error, but JS is lenient and turns that into +new Date())
    – Chris G
    Nov 8 at 3:05
















  • 4




    developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
    – Daniel A. White
    Nov 8 at 2:45










  • @CertainPerformance the question has been edited and is no longer a duplicate.
    – WEB_UI
    Nov 8 at 2:57










  • In JavaScript, each Object has a valueOf() method. Putting + in front of an Object calls that method on it, and Date overrides it: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… (also, +new Date is a syntax error, but JS is lenient and turns that into +new Date())
    – Chris G
    Nov 8 at 3:05










4




4




developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
– Daniel A. White
Nov 8 at 2:45




developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
– Daniel A. White
Nov 8 at 2:45












@CertainPerformance the question has been edited and is no longer a duplicate.
– WEB_UI
Nov 8 at 2:57




@CertainPerformance the question has been edited and is no longer a duplicate.
– WEB_UI
Nov 8 at 2:57












In JavaScript, each Object has a valueOf() method. Putting + in front of an Object calls that method on it, and Date overrides it: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… (also, +new Date is a syntax error, but JS is lenient and turns that into +new Date())
– Chris G
Nov 8 at 3:05






In JavaScript, each Object has a valueOf() method. Putting + in front of an Object calls that method on it, and Date overrides it: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… (also, +new Date is a syntax error, but JS is lenient and turns that into +new Date())
– Chris G
Nov 8 at 3:05














1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted










+ converts the following expression into a number, if it can. If the following expression is an object, then that object's valueOf function is called, so as to returns the primitive value of the specified object, which can then be (attempted) to be coerced to a number.




How does a + sign affect new Date? How does the value Wed Nov 07 2018 21:50:30 GMT-0500 for example, convert into a numerical representation?




Date.prototype.valueOf returns the integer timestamp of the date object in question:






console.log(
new Date().valueOf()
);





And this method is indeed called when + is before a Date object, as you can see here (just for demonstration, this shouldn't be in real code):






Date.prototype.valueOf = () => 5;
console.log(+new Date());





So, the Date object is converted to a number via valueOf. (This is before the + coerce-to-number operation has actually occurred, but since it's already a number, it doesn't affect anything further)






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',
    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%2f53200822%2fhow-does-a-prepending-plus-sign-treat-new-date%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote



    accepted










    + converts the following expression into a number, if it can. If the following expression is an object, then that object's valueOf function is called, so as to returns the primitive value of the specified object, which can then be (attempted) to be coerced to a number.




    How does a + sign affect new Date? How does the value Wed Nov 07 2018 21:50:30 GMT-0500 for example, convert into a numerical representation?




    Date.prototype.valueOf returns the integer timestamp of the date object in question:






    console.log(
    new Date().valueOf()
    );





    And this method is indeed called when + is before a Date object, as you can see here (just for demonstration, this shouldn't be in real code):






    Date.prototype.valueOf = () => 5;
    console.log(+new Date());





    So, the Date object is converted to a number via valueOf. (This is before the + coerce-to-number operation has actually occurred, but since it's already a number, it doesn't affect anything further)






    share|improve this answer

























      up vote
      1
      down vote



      accepted










      + converts the following expression into a number, if it can. If the following expression is an object, then that object's valueOf function is called, so as to returns the primitive value of the specified object, which can then be (attempted) to be coerced to a number.




      How does a + sign affect new Date? How does the value Wed Nov 07 2018 21:50:30 GMT-0500 for example, convert into a numerical representation?




      Date.prototype.valueOf returns the integer timestamp of the date object in question:






      console.log(
      new Date().valueOf()
      );





      And this method is indeed called when + is before a Date object, as you can see here (just for demonstration, this shouldn't be in real code):






      Date.prototype.valueOf = () => 5;
      console.log(+new Date());





      So, the Date object is converted to a number via valueOf. (This is before the + coerce-to-number operation has actually occurred, but since it's already a number, it doesn't affect anything further)






      share|improve this answer























        up vote
        1
        down vote



        accepted







        up vote
        1
        down vote



        accepted






        + converts the following expression into a number, if it can. If the following expression is an object, then that object's valueOf function is called, so as to returns the primitive value of the specified object, which can then be (attempted) to be coerced to a number.




        How does a + sign affect new Date? How does the value Wed Nov 07 2018 21:50:30 GMT-0500 for example, convert into a numerical representation?




        Date.prototype.valueOf returns the integer timestamp of the date object in question:






        console.log(
        new Date().valueOf()
        );





        And this method is indeed called when + is before a Date object, as you can see here (just for demonstration, this shouldn't be in real code):






        Date.prototype.valueOf = () => 5;
        console.log(+new Date());





        So, the Date object is converted to a number via valueOf. (This is before the + coerce-to-number operation has actually occurred, but since it's already a number, it doesn't affect anything further)






        share|improve this answer












        + converts the following expression into a number, if it can. If the following expression is an object, then that object's valueOf function is called, so as to returns the primitive value of the specified object, which can then be (attempted) to be coerced to a number.




        How does a + sign affect new Date? How does the value Wed Nov 07 2018 21:50:30 GMT-0500 for example, convert into a numerical representation?




        Date.prototype.valueOf returns the integer timestamp of the date object in question:






        console.log(
        new Date().valueOf()
        );





        And this method is indeed called when + is before a Date object, as you can see here (just for demonstration, this shouldn't be in real code):






        Date.prototype.valueOf = () => 5;
        console.log(+new Date());





        So, the Date object is converted to a number via valueOf. (This is before the + coerce-to-number operation has actually occurred, but since it's already a number, it doesn't affect anything further)






        console.log(
        new Date().valueOf()
        );





        console.log(
        new Date().valueOf()
        );





        Date.prototype.valueOf = () => 5;
        console.log(+new Date());





        Date.prototype.valueOf = () => 5;
        console.log(+new Date());






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 8 at 3:07









        CertainPerformance

        68.1k143353




        68.1k143353






























            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%2f53200822%2fhow-does-a-prepending-plus-sign-treat-new-date%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







            這個網誌中的熱門文章

            Xamarin.form Move up view when keyboard appear

            Post-Redirect-Get with Spring WebFlux and Thymeleaf

            Anylogic : not able to use stopDelay()