Difference between “value.toString” and “value.toString()”
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
add a comment |
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
1
The first statement verifies thatvalue
has a truthy value for itstoString
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 thingtoString
. 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
add a comment |
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
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
javascript
edited Nov 23 '18 at 12:11
elegant-user
1,86641434
1,86641434
asked Nov 23 '18 at 11:40
Divya SinghDivya Singh
144
144
1
The first statement verifies thatvalue
has a truthy value for itstoString
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 thingtoString
. 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
add a comment |
1
The first statement verifies thatvalue
has a truthy value for itstoString
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 thingtoString
. 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
add a comment |
4 Answers
4
active
oldest
votes
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")
“There is nothing Like "toString" in javascript. Whereas the function toString() [...]” - both are the same thing -foo.bar
just references a methodbar
of thefoo
object, whereasfoo.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
add a comment |
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.
add a comment |
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.
“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
add a comment |
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] }
nope,value.toString()
do not return that buttoString
does.
– Rıdvan Sumset
Nov 23 '18 at 12:01
add a comment |
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
});
}
});
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%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
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")
“There is nothing Like "toString" in javascript. Whereas the function toString() [...]” - both are the same thing -foo.bar
just references a methodbar
of thefoo
object, whereasfoo.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
add a comment |
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")
“There is nothing Like "toString" in javascript. Whereas the function toString() [...]” - both are the same thing -foo.bar
just references a methodbar
of thefoo
object, whereasfoo.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
add a comment |
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")
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")
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 methodbar
of thefoo
object, whereasfoo.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
add a comment |
“There is nothing Like "toString" in javascript. Whereas the function toString() [...]” - both are the same thing -foo.bar
just references a methodbar
of thefoo
object, whereasfoo.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
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Nov 23 '18 at 11:55
Pedro LMPedro LM
45727
45727
add a comment |
add a comment |
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.
“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
add a comment |
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.
“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
add a comment |
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.
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.
edited Nov 23 '18 at 11:56
answered Nov 23 '18 at 11:52
KunalKunal
16710
16710
“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
add a comment |
“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
add a comment |
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] }
nope,value.toString()
do not return that buttoString
does.
– Rıdvan Sumset
Nov 23 '18 at 12:01
add a comment |
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] }
nope,value.toString()
do not return that buttoString
does.
– Rıdvan Sumset
Nov 23 '18 at 12:01
add a comment |
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] }
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] }
edited Nov 23 '18 at 12:10
answered Nov 23 '18 at 11:55
elegant-userelegant-user
1,86641434
1,86641434
nope,value.toString()
do not return that buttoString
does.
– Rıdvan Sumset
Nov 23 '18 at 12:01
add a comment |
nope,value.toString()
do not return that buttoString
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
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.
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%2f53446031%2fdifference-between-value-tostring-and-value-tostring%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
1
The first statement verifies that
value
has a truthy value for itstoString
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 thingtoString
. 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