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 affectnew Date
? How does the valueWed Nov 07 2018 21:50:30 GMT-0500
for example, convert into a numerical representation?
javascript operators
add a comment |
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 affectnew Date
? How does the valueWed Nov 07 2018 21:50:30 GMT-0500
for example, convert into a numerical representation?
javascript operators
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, eachObject
has avalueOf()
method. Putting+
in front of an Object calls that method on it, andDate
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
add a comment |
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 affectnew Date
? How does the valueWed Nov 07 2018 21:50:30 GMT-0500
for example, convert into a numerical representation?
javascript operators
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 affectnew Date
? How does the valueWed 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
javascript operators
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, eachObject
has avalueOf()
method. Putting+
in front of an Object calls that method on it, andDate
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
add a comment |
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, eachObject
has avalueOf()
method. Putting+
in front of an Object calls that method on it, andDate
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
add a comment |
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)
add a comment |
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)
add a comment |
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)
add a comment |
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)
+
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());
answered Nov 8 at 3:07
CertainPerformance
68.1k143353
68.1k143353
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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 avalueOf()
method. Putting+
in front of an Object calls that method on it, andDate
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