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))
Related
This question already has answers here:
Destructuring assignment and variable swapping
(1 answer)
Semicolon before square bracket
(2 answers)
ES6 Array destructuring weirdness
(4 answers)
Closed 1 year ago.
Please, someone cound explain this behavior with and without semicolon?
Without semicolou, it raise a ReferenceError: Cannot access 'b' before initialization
let a = 1
let b = 2
[a,b]=[b,a]
It's ok with semicolon:
let a = 1;
let b = 2;
[a,b]=[b,a]
It doesn't occurs using var.
This line, without semicolons, evaluates to let b = 2[a,b..., and since b is being declared in the same expression, it throws the error. Adding a semicolon after the 2 will remove the attempted index and resolve the error.
This question already has answers here:
How is division before multiplication in Javascript?
(2 answers)
Closed 4 years ago.
I'm having trouble understanding how JS is processing my expressions..
This evaluates incorrectly and drops the denominator:
var a = document.getElementById("Acceleration").value;
var u = document.getElementById("Initial_Velocity").value;
var v = document.getElementById("Final_Velocity").value;
document.getElementById("Distance").value = (v*v-u*u)/2*a;
This evaluates properly:
document.getElementById("Distance").value = (v*v-u*u)/(2*a);
I'm using Dreamweaver. Thanks.
Well the formulas are different. You left out the parentheses (2*a) in the first code sample.
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.
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.
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/