JavaScript parentheses (1,2,3,4,5) [duplicate] - javascript

This question already has answers here:
Javascript expression in parentheses
(2 answers)
Closed 8 years ago.
I just came across some notation in JavaScript like so:
var a = (1,2,3,4,5);
This will always return the last value, in the above case 5. I'm aware of using brackets to namespace my JavaScript code, but have never seen it used this way.
Is there any use for this notation, or is it just some JavaScript byproduct?

It's the comma operator. As the mdn states (link) it always returns the later value. In your example it doesn't make much sense, since it will always assign a = 5. But consider this:
for (var i = 0, j = 9; i <= 9; i++, j--) {
...
}
It's used to increment and decrement in a single statement: i++, j--
Edit:
The parentheses in your example are necessary because its a variable declaration. In other cases they can be left out.

Parens are used to groups operations together. This is helpful for both setting operation precedence (e.g. x = (2+3) * 5 vs x = 2 + 3 * 5) and for making your code a little easier to read.
I suspect this is more a question about the comma operator. This is for making multiple assignments or operations on the same line. Here is a nice article about it: http://javascriptweblog.wordpress.com/2011/04/04/the-javascript-comma-operator/

Related

Why do people write js like this? [duplicate]

This question already has answers here:
Why does any JavaScript code want to "cut the binding"?
(1 answer)
JavaScript syntax (0, fn)(args)
(2 answers)
Closed 2 years ago.
var type = (0, _reactIs.isMemo)(nodeOrComponent) ? nodeOrComponent.type.type : nodeOrComponent.type;
(0, _reactIs.isMemo) really confuse me. What's the meaning of this?
ps: I know (0, _reactIs.isMemo) this expression's value is _reactIs.isMemo
The comma operator there ensures that what's inside the parentheses is evaluated as an expression without a calling context.
To take a shorter example, if the code was:
var type = obj.fn(someArg);
then fn would be called with a calling context of obj. But the original untranspiled code, whatever it is, does not have such a calling context, so in order to be faithful to the original code, the calling context has to be removed, which can be done with the comma operator:
var type = (0, obj.fn)(someArg);
Another way of doing the same thing would be:
var fn = obj.fn;
var type = fn(someArg);
(but that takes more characters, so minifiers prefer the comma operator version)
This is a silly-looking minification trick that's often seen with imported modules. Usually, you'd only be looking at the source code, which won't have this sillyness.

What does ++ mean in jQuery/JavaScript? [duplicate]

This question already has an answer here:
What does this symbol mean in JavaScript?
(1 answer)
Closed 6 years ago.
I've seen various people use stuff like i++, and I know that is also used in a for-loop.
But what exactly does ++ do to a variable? I cannot seem to find any documentation on what it does.
The ++ notation is the increment operators.
i++
is the same thing of
i = i +1
here you can see the completly list of this operators:
http://www.w3schools.com/js/js_operators.asp
It's to increment.
var i = 1;
i++; // i becomes 2.

Difference between return ("abcd") and return "abcd"? [duplicate]

This question already has answers here:
Why use parentheses when returning in JavaScript?
(9 answers)
Closed 6 years ago.
I'm struggling with some JavaScript code that has examples that return a string in parens. Is there any difference between this:
var x = function() {
return "abcd";
}
And
var x = function() {
return ("abcd");
}
They are the same.
The parenthesis, i.e. grouping operator, here will just work as higher precedence, so that'll be evaluated first, and then the value will be returned.
There's no difference, at least in the way it is written.
Consider,
var x = 1 + 2;
vs
var x = (1 + 2);
Some prefer writing brackets around return statement but it is optional and often unnecessary. But I would advise stick to your existing code style.
In reference to this question the parenthesis do not have a specific meaning as a string is being returned. There is no expression evaluation involved so both the return statements work in the same way. In case we have an expression that needs to be evaluated, the parenthesis will provide a liberty to first evaluate the expression and then return the desired value according to the precedence rules.

IIFE beginning with one parenthesis bracket not working [duplicate]

This question already has answers here:
What are the rules for JavaScript's automatic semicolon insertion (ASI)?
(7 answers)
strange observation on IIFE in node.js (Windows)
(2 answers)
Closed 6 years ago.
I am looking at IIFE's in javascript, and as far as I was aware it was simply a style choice as to how you write the IIFE, and that both approaches below should work.
var sum = 0
(function test(n){
sum += n
}(1));
console.log(sum);
This logs: Uncaught TypeError: 0 is not a function.
Alternatively, when I begin the IIFE with ! it works
var sum = 0
!function test(n){
sum += n
}(2);
console.log(sum) //logs 2
As you can see when I begin the IIFE with a ! it works as expected. I am very confused now as I thought it was simply a stylistic choice as to how you implemented the IIFE. Can anyone explain?
This has nothing to do with the fact that you are only using one parenthesis, it's the lack of a semicolon at the end of the first line.
Look at it without the line break to see the problem.
var sum = 0(function test(n){
sum += n
}(1));
If you follow the semicolon-free approach, then you need to guard newlines starting with [ or ( with a semicolon.
For example:
var sum = 0
;(function test(n){
sum += n
}(1))

do I need eval? [duplicate]

This question already has answers here:
Why does eval() exist?
(6 answers)
Closed 9 years ago.
I've searched the web for what eval does and here is what I have found:
The eval() method evaluates JavaScript code represented as a string.
And I've read this question, which says that it is evil.
But I really don't understand what does it do, i.e I don't see when to use eval.
I mean:
var x= 3;
var y =5;
var z = eval("x+y");
// is the same as:
var z = x+y;
so as I see it's just adding characters to my code. Can somebody give me an example of why eval was created in the first place?
It allows execute dynamicly generated code, not just "x+y".
Eval allows you to evaluate a string as javascript code. This means you can do things like evaluate a string to call a function:
function add(x,y){
return (x+y);
}
stringToEval = 'add(5,6)';
eval(stringToEval);
This code would run the function by evaluating the string as a function call.
So I would say you do not need to use eval here. Just add the two variables.

Categories

Resources