Variable meaning / value in a comma separated syntax [duplicate] - javascript

This question already has answers here:
What does i = (i, ++i, 1) + 1; do?
(7 answers)
Closed 4 years ago.
Could you tell me what this i means after the comma. I am struggling to understand this syntax or even find it in the internet.
let i;
this.questions = document.querySelectorAll('.question'), i;
i is not used anymore in the function
If setting a debugger after this lines. i is undefined

It doesn't seem to be of any use indeed.
As an information, it's just to declare / instantiate your variables. Take a look :
let i, j;
j = 1, i = 2;
console.log(i, j);

Related

Why does the function is created with small scope? [duplicate]

This question already has answers here:
How do JavaScript closures work?
(86 answers)
JavaScript closure inside loops – simple practical example
(44 answers)
Closed 1 year ago.
I don't understand why the behavior of the function is funny.
let printNumTwo;
for (let i = 0; i < 3; i++) {
if (i === 2) {
printNumTwo = function() {
return i;
};
}
}
let i =5
console.log(printNumTwo());
I thought the function is created globally and it will have to return 5. Why the function has small scope? Where could i read about what more.
PS Sorry for my english and my probably stuped question. I need more understandable info for this TY

JS: Creating n variables using for loop? [duplicate]

This question already has answers here:
Dynamic variable name in loop
(3 answers)
Closed 4 years ago.
If I have to create n variables a_1, a_2, a_3 ... a_n, where n is determined during runtime, how will I be able to do it?
Obviously, this code won't work:
var n = prompt("Enter number of variables?");
for (i=0; i<=n; i++) {
var a_i
}
As n is given by user it is not possible to pre-determine number of variables to be created.
In other words, Is it possible to create a variable with name from another variable in JS?
Yes, like this
for (i=0; i<=n; i++) {
window['a_'+i] = undefined;
}

Variable is jumping to a higher scope? [duplicate]

This question already has answers here:
What is the difference between "let" and "var"?
(39 answers)
Closed 4 years ago.
I noticed a bug in my code a couple days ago and have no idea why it's happening. It seems a variable defined in a lower scope is somehow jumping up to a higher one. Anyone know what's happening here? Dumbed down code:
console.log(a)
for(var k = 0; k < 5; k++)
var a = 5
console.log(a)
The first console log always prints undefined
But the second console log always prints 5?
Shouldn't variable a only exist in the for loop's scope and be cleared from memory once the for loop is done?
Variables defined with var are "function scoped", so they are accessible anywhere in the function. let and const however have "block scoping", they will behave like you expect:
{
let a = 1;
var b = 2;
}
console.log(a, b); // not defined, 2

Does this param need to be returned in javascript? [duplicate]

This question already has answers here:
Is JavaScript a pass-by-reference or pass-by-value language?
(33 answers)
Closed 5 years ago.
I am going through the heads first js book and encountered a problem.
On the Console it comes out as 7 still when it should be 8? This is in the book so I'm guessing I'm just missing something basic here.
var age = 7;
function addOne(x) {
x = x + 1;
}
addOne(age);
console.log(age);
Simply explained: You don't do anything with age. In your function you add one to 7 which is stored as 8 into x and after the function is processed you print out age which is still 7.
To answer your question: Returning the param x would let you return 8. So
var age = 7;
function addOne(x) {
x = x + 1;
return x;
}
console.log(addOne(age));`
should work for you.

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

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/

Categories

Resources