admin管理员组文章数量:1392003
I've e across a rather confusing statement in some JavaScript:
if (n = "value", a==b) {...
I take it that this assigns the value n
first and then performs a parison (a==b
) to determine whether to proceed with the if
statement. But why? Is there any advantage to doing this over say...
n = "value";
if (a==b) {...
or...
if (a==b) {n = "value"; ...
I've e across a rather confusing statement in some JavaScript:
if (n = "value", a==b) {...
I take it that this assigns the value n
first and then performs a parison (a==b
) to determine whether to proceed with the if
statement. But why? Is there any advantage to doing this over say...
n = "value";
if (a==b) {...
or...
if (a==b) {n = "value"; ...
Share
Improve this question
asked Feb 5, 2014 at 17:25
user1945782user1945782
3
-
1
Your third snippet is not the same as two previous snippets. In two first snippets
n
is set in any case, in the third snippetn
will be set only ifif
passes. – Teemu Commented Feb 5, 2014 at 17:36 -
Hmm. That's true. What I was getting at, I suppose, is whether the scope of
n
is restricted in anyway by delcaring it in theif
function (succinctly answered by Danilo below). In theory ifn
was restricted to the scope of theif
statement then it could be available to the final parison, so even if the test failsn
gets its value for the scope ofif
. – user1945782 Commented Feb 5, 2014 at 17:43 - @Question participants: (I know it's bad form on SO :-O ) but many thanks to you all for helping me understand this. I have actually used this now and found it makes the code more readable (in my current case) than less. – user1945782 Commented Feb 6, 2014 at 11:23
3 Answers
Reset to default 3In JavaScript, whenever you put more than one expression inside a pair of brackets, they are evaluated as the last expression, like in the example below:
var a = (1, 2);
var b = a + 1; // b = 2 + 1 = 3
So, in your case, the interpreter executes the attribution n = "value"
and then parses the if taking a == b
as condition. It's the same as:
n = "value";
if (a == b) {
// ...
}
This article explains this behaviour.
EDIT
However, this does not limit n
to the if
's scope. This same thing happens to var declarations in for
loops:
for (var i = 0; i < 10; i++) {
// Do stuff...
}
console.log(i); // Logs 10
EDIT 2
As Ethan Brown mentioned, is also good to tell about variable hoisting, which is basically the fact that, in JavaScript, values can be assigned to variables before declaring them. The following code shows this behaviour and was extracted from this MDN article:
bla = 2
var bla;
// The above code is valid, since
// it's implicitly understood as:
var bla;
bla = 2;
The same occurs with functions:
foo();
function foo() {
console.log('bar');
}
Your assessment of the meaning is correct. There is no advantage other than pactness. Many would consider this poor practice, but it doesn't bother me.
Where it gets tricky is when you start calling functions that have side effects...then you can do some really weird stuff.
You're right - it's just a really confusing way to phrase assigning a variable and then running an if
statement. It's valid code, but equivalent to the less puzzling version, so this is most likely just a case of someone being too clever.
本文标签: javascriptComma in an if statementStack Overflow
版权声明:本文标题:javascript - Comma in an if statement - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744759415a2623663.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论