Why is the result of ('b'+'a'+ + 'a' + 'a').toLowerCase() 'banana'?JavaScript post request like a form...
A Word/Phrase for the Process of Classifying Something as a Sin
Is this curved text blend possible in Illustrator?
Solution to German Tank Problem
Why did Gandalf use a sword against the Balrog?
What does the phrase "pull off sick wheelies and flips" mean here?
Visa National - No Exit Stamp From France on Return to the UK
If clocks themselves are based on light signals, wouldn't we expect the measured speed of light to always be the same constant?
How to take the beginning and end parts of a list with simpler syntax?
Why is the result of ('b'+'a'+ + 'a' + 'a').toLowerCase() 'banana'?
The cat ate your input again!
What is a good class if we remove subclasses?
How can this older-style irrigation tee be replaced?
Simplification of numbers
Am I overreacting to my team leader's unethical requests?
Can anybody explain why using multicolumn changes the width of the four-column tabular environment?
How to remove threat that antivirus program indicates has to be manually deleted?
How to divide item stack in MC PE?
How to assign many blockers at the same time?
What is an internal dimension/glue/muglue?
Do beef farmed pastures net remove carbon emissions?
How does "Te vas a cansar" mean "You're going to get tired"?
Why did I get only 5 points even though I won?
How can I decide if my homebrew item should require attunement?
Heating Margarine in Pan = loss of calories?
Why is the result of ('b'+'a'+ + 'a' + 'a').toLowerCase() 'banana'?
JavaScript post request like a form submitHow do you access the matched groups in a JavaScript regular expression?Why is setTimeout(fn, 0) sometimes useful?What is the purpose of the var keyword and when should I use it (or omit it)?Get all unique values in a JavaScript array (remove duplicates)Storing Objects in HTML5 localStorageWhat is JSONP, and why was it created?Why does Google prepend while(1); to their JSON responses?How to decide when to use Node.js?Why does ++[[]][+[]]+[+[]] return the string “10”?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}
I was just practising JavaScript when one of my friends came across this JavaScript code.
('b'+'a'+ + 'a' + 'a').toLowerCase()
The above code answers "banana"
. Can anyone explain why?
javascript
New contributor
|
show 7 more comments
I was just practising JavaScript when one of my friends came across this JavaScript code.
('b'+'a'+ + 'a' + 'a').toLowerCase()
The above code answers "banana"
. Can anyone explain why?
javascript
New contributor
29
because of+ + 'a'
===NaN
– The Reason
14 hours ago
5
That second plus is an unary operator:+"a"
isNaN
.
– Gerardo Furtado
14 hours ago
17
came across this javascript code
--> Where ever this code was came across surely ought to have the explanation.
– Krishna Prashatt
14 hours ago
4
And for those eager for more. See full fun list
– Giddy Naya
14 hours ago
19
for future "learning" experience, why not do some investigating yourself. For example, remove thetoLowerCase()
... replace theb
,a
,a
,a
witha
,b
,c
,d
... which will surely make the result more apparent
– Jaromanda X
13 hours ago
|
show 7 more comments
I was just practising JavaScript when one of my friends came across this JavaScript code.
('b'+'a'+ + 'a' + 'a').toLowerCase()
The above code answers "banana"
. Can anyone explain why?
javascript
New contributor
I was just practising JavaScript when one of my friends came across this JavaScript code.
('b'+'a'+ + 'a' + 'a').toLowerCase()
The above code answers "banana"
. Can anyone explain why?
javascript
javascript
New contributor
New contributor
edited 2 hours ago
John Kugelman
259k58 gold badges420 silver badges470 bronze badges
259k58 gold badges420 silver badges470 bronze badges
New contributor
asked 14 hours ago
Harshvardhan SharmaHarshvardhan Sharma
3013 silver badges9 bronze badges
3013 silver badges9 bronze badges
New contributor
New contributor
29
because of+ + 'a'
===NaN
– The Reason
14 hours ago
5
That second plus is an unary operator:+"a"
isNaN
.
– Gerardo Furtado
14 hours ago
17
came across this javascript code
--> Where ever this code was came across surely ought to have the explanation.
– Krishna Prashatt
14 hours ago
4
And for those eager for more. See full fun list
– Giddy Naya
14 hours ago
19
for future "learning" experience, why not do some investigating yourself. For example, remove thetoLowerCase()
... replace theb
,a
,a
,a
witha
,b
,c
,d
... which will surely make the result more apparent
– Jaromanda X
13 hours ago
|
show 7 more comments
29
because of+ + 'a'
===NaN
– The Reason
14 hours ago
5
That second plus is an unary operator:+"a"
isNaN
.
– Gerardo Furtado
14 hours ago
17
came across this javascript code
--> Where ever this code was came across surely ought to have the explanation.
– Krishna Prashatt
14 hours ago
4
And for those eager for more. See full fun list
– Giddy Naya
14 hours ago
19
for future "learning" experience, why not do some investigating yourself. For example, remove thetoLowerCase()
... replace theb
,a
,a
,a
witha
,b
,c
,d
... which will surely make the result more apparent
– Jaromanda X
13 hours ago
29
29
because of
+ + 'a'
=== NaN
– The Reason
14 hours ago
because of
+ + 'a'
=== NaN
– The Reason
14 hours ago
5
5
That second plus is an unary operator:
+"a"
is NaN
.– Gerardo Furtado
14 hours ago
That second plus is an unary operator:
+"a"
is NaN
.– Gerardo Furtado
14 hours ago
17
17
came across this javascript code
--> Where ever this code was came across surely ought to have the explanation.– Krishna Prashatt
14 hours ago
came across this javascript code
--> Where ever this code was came across surely ought to have the explanation.– Krishna Prashatt
14 hours ago
4
4
And for those eager for more. See full fun list
– Giddy Naya
14 hours ago
And for those eager for more. See full fun list
– Giddy Naya
14 hours ago
19
19
for future "learning" experience, why not do some investigating yourself. For example, remove the
toLowerCase()
... replace the b
, a
, a
, a
with a
, b
, c
, d
... which will surely make the result more apparent– Jaromanda X
13 hours ago
for future "learning" experience, why not do some investigating yourself. For example, remove the
toLowerCase()
... replace the b
, a
, a
, a
with a
, b
, c
, d
... which will surely make the result more apparent– Jaromanda X
13 hours ago
|
show 7 more comments
1 Answer
1
active
oldest
votes
+'a'
resolves to NaN
("Not a Number") because it coerces a string to a number, while the character a
cannot be parsed as a number.
Adding NaN
to "ba"
turns NaN
into the string "NaN"
due to type conversion, gives baNaN
.
And then there is an a
behind, giving baNaNa
.
To lowercase it becomes banana
.
The space between + +
is to make the first one string concatenation and the second one a unary plus (i.e. "positive") operator.
You have the same result if you use 'ba'+(+'a')+'a'
, which is equivalent to 'ba'+'NaN'+'a'
.
7
'ba'+(+'a')+'a'
is equivalent to'ba' + NaN + 'a'
. The conversion to the string happens because'ba'
beeing a string as the time when'ba' + NaN
is performed.1 + + 'a' + 1
isNaN
because it is1 + NaN + 1
and not1 + 'NaN' + 1
which would result in1NaN1
– t.niese
14 hours ago
I believe there is some confusion between arithmetic operators and the unary plus in your answer. Technically speaking, the unary plus does not perform an arithmetic addition.
– Gerardo Furtado
14 hours ago
@GerardoFurtado thanks. I made a typo when I wrote unary addition instead of unary plus. But it is a numeric-related operator nonetheless.
– SOFe
14 hours ago
2
I guess that's not really important. I just checked the ES3 spec and realized the term "arithmetic operator" never appeared in the spec, so it's just how we define our own words. But indeed, you're right to say that "unary plus just converts the operand to Number type" (which is a pointless operator anyway).
– SOFe
14 hours ago
4
@SOFe In my opinion the distinction is important. For better explaining, you have two different operators that use the same symbol (+). I'm using the ECMA specifications here: One is the addition operator (ecma-international.org/ecma-262/10.0/…). The other one is the unary operator (ecma-international.org/ecma-262/10.0/#sec-unary-plus-operator). They are different. To summarise, it doesn't "attempts to perform an arithmetic operation on a string", as you mentioned in your answer.
– Gerardo Furtado
14 hours ago
|
show 2 more comments
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
});
}
});
Harshvardhan Sharma is a new contributor. Be nice, and check out our Code of Conduct.
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%2f57456188%2fwhy-is-the-result-of-ba-a-a-tolowercase-banana%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
+'a'
resolves to NaN
("Not a Number") because it coerces a string to a number, while the character a
cannot be parsed as a number.
Adding NaN
to "ba"
turns NaN
into the string "NaN"
due to type conversion, gives baNaN
.
And then there is an a
behind, giving baNaNa
.
To lowercase it becomes banana
.
The space between + +
is to make the first one string concatenation and the second one a unary plus (i.e. "positive") operator.
You have the same result if you use 'ba'+(+'a')+'a'
, which is equivalent to 'ba'+'NaN'+'a'
.
7
'ba'+(+'a')+'a'
is equivalent to'ba' + NaN + 'a'
. The conversion to the string happens because'ba'
beeing a string as the time when'ba' + NaN
is performed.1 + + 'a' + 1
isNaN
because it is1 + NaN + 1
and not1 + 'NaN' + 1
which would result in1NaN1
– t.niese
14 hours ago
I believe there is some confusion between arithmetic operators and the unary plus in your answer. Technically speaking, the unary plus does not perform an arithmetic addition.
– Gerardo Furtado
14 hours ago
@GerardoFurtado thanks. I made a typo when I wrote unary addition instead of unary plus. But it is a numeric-related operator nonetheless.
– SOFe
14 hours ago
2
I guess that's not really important. I just checked the ES3 spec and realized the term "arithmetic operator" never appeared in the spec, so it's just how we define our own words. But indeed, you're right to say that "unary plus just converts the operand to Number type" (which is a pointless operator anyway).
– SOFe
14 hours ago
4
@SOFe In my opinion the distinction is important. For better explaining, you have two different operators that use the same symbol (+). I'm using the ECMA specifications here: One is the addition operator (ecma-international.org/ecma-262/10.0/…). The other one is the unary operator (ecma-international.org/ecma-262/10.0/#sec-unary-plus-operator). They are different. To summarise, it doesn't "attempts to perform an arithmetic operation on a string", as you mentioned in your answer.
– Gerardo Furtado
14 hours ago
|
show 2 more comments
+'a'
resolves to NaN
("Not a Number") because it coerces a string to a number, while the character a
cannot be parsed as a number.
Adding NaN
to "ba"
turns NaN
into the string "NaN"
due to type conversion, gives baNaN
.
And then there is an a
behind, giving baNaNa
.
To lowercase it becomes banana
.
The space between + +
is to make the first one string concatenation and the second one a unary plus (i.e. "positive") operator.
You have the same result if you use 'ba'+(+'a')+'a'
, which is equivalent to 'ba'+'NaN'+'a'
.
7
'ba'+(+'a')+'a'
is equivalent to'ba' + NaN + 'a'
. The conversion to the string happens because'ba'
beeing a string as the time when'ba' + NaN
is performed.1 + + 'a' + 1
isNaN
because it is1 + NaN + 1
and not1 + 'NaN' + 1
which would result in1NaN1
– t.niese
14 hours ago
I believe there is some confusion between arithmetic operators and the unary plus in your answer. Technically speaking, the unary plus does not perform an arithmetic addition.
– Gerardo Furtado
14 hours ago
@GerardoFurtado thanks. I made a typo when I wrote unary addition instead of unary plus. But it is a numeric-related operator nonetheless.
– SOFe
14 hours ago
2
I guess that's not really important. I just checked the ES3 spec and realized the term "arithmetic operator" never appeared in the spec, so it's just how we define our own words. But indeed, you're right to say that "unary plus just converts the operand to Number type" (which is a pointless operator anyway).
– SOFe
14 hours ago
4
@SOFe In my opinion the distinction is important. For better explaining, you have two different operators that use the same symbol (+). I'm using the ECMA specifications here: One is the addition operator (ecma-international.org/ecma-262/10.0/…). The other one is the unary operator (ecma-international.org/ecma-262/10.0/#sec-unary-plus-operator). They are different. To summarise, it doesn't "attempts to perform an arithmetic operation on a string", as you mentioned in your answer.
– Gerardo Furtado
14 hours ago
|
show 2 more comments
+'a'
resolves to NaN
("Not a Number") because it coerces a string to a number, while the character a
cannot be parsed as a number.
Adding NaN
to "ba"
turns NaN
into the string "NaN"
due to type conversion, gives baNaN
.
And then there is an a
behind, giving baNaNa
.
To lowercase it becomes banana
.
The space between + +
is to make the first one string concatenation and the second one a unary plus (i.e. "positive") operator.
You have the same result if you use 'ba'+(+'a')+'a'
, which is equivalent to 'ba'+'NaN'+'a'
.
+'a'
resolves to NaN
("Not a Number") because it coerces a string to a number, while the character a
cannot be parsed as a number.
Adding NaN
to "ba"
turns NaN
into the string "NaN"
due to type conversion, gives baNaN
.
And then there is an a
behind, giving baNaNa
.
To lowercase it becomes banana
.
The space between + +
is to make the first one string concatenation and the second one a unary plus (i.e. "positive") operator.
You have the same result if you use 'ba'+(+'a')+'a'
, which is equivalent to 'ba'+'NaN'+'a'
.
edited 5 hours ago
Bergi
400k66 gold badges633 silver badges958 bronze badges
400k66 gold badges633 silver badges958 bronze badges
answered 14 hours ago
SOFeSOFe
3,1833 gold badges24 silver badges47 bronze badges
3,1833 gold badges24 silver badges47 bronze badges
7
'ba'+(+'a')+'a'
is equivalent to'ba' + NaN + 'a'
. The conversion to the string happens because'ba'
beeing a string as the time when'ba' + NaN
is performed.1 + + 'a' + 1
isNaN
because it is1 + NaN + 1
and not1 + 'NaN' + 1
which would result in1NaN1
– t.niese
14 hours ago
I believe there is some confusion between arithmetic operators and the unary plus in your answer. Technically speaking, the unary plus does not perform an arithmetic addition.
– Gerardo Furtado
14 hours ago
@GerardoFurtado thanks. I made a typo when I wrote unary addition instead of unary plus. But it is a numeric-related operator nonetheless.
– SOFe
14 hours ago
2
I guess that's not really important. I just checked the ES3 spec and realized the term "arithmetic operator" never appeared in the spec, so it's just how we define our own words. But indeed, you're right to say that "unary plus just converts the operand to Number type" (which is a pointless operator anyway).
– SOFe
14 hours ago
4
@SOFe In my opinion the distinction is important. For better explaining, you have two different operators that use the same symbol (+). I'm using the ECMA specifications here: One is the addition operator (ecma-international.org/ecma-262/10.0/…). The other one is the unary operator (ecma-international.org/ecma-262/10.0/#sec-unary-plus-operator). They are different. To summarise, it doesn't "attempts to perform an arithmetic operation on a string", as you mentioned in your answer.
– Gerardo Furtado
14 hours ago
|
show 2 more comments
7
'ba'+(+'a')+'a'
is equivalent to'ba' + NaN + 'a'
. The conversion to the string happens because'ba'
beeing a string as the time when'ba' + NaN
is performed.1 + + 'a' + 1
isNaN
because it is1 + NaN + 1
and not1 + 'NaN' + 1
which would result in1NaN1
– t.niese
14 hours ago
I believe there is some confusion between arithmetic operators and the unary plus in your answer. Technically speaking, the unary plus does not perform an arithmetic addition.
– Gerardo Furtado
14 hours ago
@GerardoFurtado thanks. I made a typo when I wrote unary addition instead of unary plus. But it is a numeric-related operator nonetheless.
– SOFe
14 hours ago
2
I guess that's not really important. I just checked the ES3 spec and realized the term "arithmetic operator" never appeared in the spec, so it's just how we define our own words. But indeed, you're right to say that "unary plus just converts the operand to Number type" (which is a pointless operator anyway).
– SOFe
14 hours ago
4
@SOFe In my opinion the distinction is important. For better explaining, you have two different operators that use the same symbol (+). I'm using the ECMA specifications here: One is the addition operator (ecma-international.org/ecma-262/10.0/…). The other one is the unary operator (ecma-international.org/ecma-262/10.0/#sec-unary-plus-operator). They are different. To summarise, it doesn't "attempts to perform an arithmetic operation on a string", as you mentioned in your answer.
– Gerardo Furtado
14 hours ago
7
7
'ba'+(+'a')+'a'
is equivalent to 'ba' + NaN + 'a'
. The conversion to the string happens because 'ba'
beeing a string as the time when 'ba' + NaN
is performed. 1 + + 'a' + 1
is NaN
because it is 1 + NaN + 1
and not 1 + 'NaN' + 1
which would result in 1NaN1
– t.niese
14 hours ago
'ba'+(+'a')+'a'
is equivalent to 'ba' + NaN + 'a'
. The conversion to the string happens because 'ba'
beeing a string as the time when 'ba' + NaN
is performed. 1 + + 'a' + 1
is NaN
because it is 1 + NaN + 1
and not 1 + 'NaN' + 1
which would result in 1NaN1
– t.niese
14 hours ago
I believe there is some confusion between arithmetic operators and the unary plus in your answer. Technically speaking, the unary plus does not perform an arithmetic addition.
– Gerardo Furtado
14 hours ago
I believe there is some confusion between arithmetic operators and the unary plus in your answer. Technically speaking, the unary plus does not perform an arithmetic addition.
– Gerardo Furtado
14 hours ago
@GerardoFurtado thanks. I made a typo when I wrote unary addition instead of unary plus. But it is a numeric-related operator nonetheless.
– SOFe
14 hours ago
@GerardoFurtado thanks. I made a typo when I wrote unary addition instead of unary plus. But it is a numeric-related operator nonetheless.
– SOFe
14 hours ago
2
2
I guess that's not really important. I just checked the ES3 spec and realized the term "arithmetic operator" never appeared in the spec, so it's just how we define our own words. But indeed, you're right to say that "unary plus just converts the operand to Number type" (which is a pointless operator anyway).
– SOFe
14 hours ago
I guess that's not really important. I just checked the ES3 spec and realized the term "arithmetic operator" never appeared in the spec, so it's just how we define our own words. But indeed, you're right to say that "unary plus just converts the operand to Number type" (which is a pointless operator anyway).
– SOFe
14 hours ago
4
4
@SOFe In my opinion the distinction is important. For better explaining, you have two different operators that use the same symbol (+). I'm using the ECMA specifications here: One is the addition operator (ecma-international.org/ecma-262/10.0/…). The other one is the unary operator (ecma-international.org/ecma-262/10.0/#sec-unary-plus-operator). They are different. To summarise, it doesn't "attempts to perform an arithmetic operation on a string", as you mentioned in your answer.
– Gerardo Furtado
14 hours ago
@SOFe In my opinion the distinction is important. For better explaining, you have two different operators that use the same symbol (+). I'm using the ECMA specifications here: One is the addition operator (ecma-international.org/ecma-262/10.0/…). The other one is the unary operator (ecma-international.org/ecma-262/10.0/#sec-unary-plus-operator). They are different. To summarise, it doesn't "attempts to perform an arithmetic operation on a string", as you mentioned in your answer.
– Gerardo Furtado
14 hours ago
|
show 2 more comments
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Harshvardhan Sharma is a new contributor. Be nice, and check out our Code of Conduct.
Harshvardhan Sharma is a new contributor. Be nice, and check out our Code of Conduct.
Harshvardhan Sharma is a new contributor. Be nice, and check out our Code of Conduct.
Harshvardhan Sharma is a new contributor. Be nice, and check out our Code of Conduct.
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%2f57456188%2fwhy-is-the-result-of-ba-a-a-tolowercase-banana%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
29
because of
+ + 'a'
===NaN
– The Reason
14 hours ago
5
That second plus is an unary operator:
+"a"
isNaN
.– Gerardo Furtado
14 hours ago
17
came across this javascript code
--> Where ever this code was came across surely ought to have the explanation.– Krishna Prashatt
14 hours ago
4
And for those eager for more. See full fun list
– Giddy Naya
14 hours ago
19
for future "learning" experience, why not do some investigating yourself. For example, remove the
toLowerCase()
... replace theb
,a
,a
,a
witha
,b
,c
,d
... which will surely make the result more apparent– Jaromanda X
13 hours ago