How JS works for multiple variables with same name? [duplicate] - javascript

This question already has answers here:
What is the difference between "let" and "var"?
(39 answers)
Closed 2 years ago.
Why there is no Syntax error in the following code?
var num = 8;
var num = 10;
console.log(num) // 10
whereas
var num = 8;
let num = 10;
console.log(num) // already decleared error

The first case will be rendered as:
var num;
num = 8;
num = 10;
console.log(num); // output : 10
The second case will be rendered as:
var num;
num = 8;
let num; // throws Uncaught SyntaxError: Identifier 'num' has already been declared
num = 10
console.log(num);

Because unlike var, variables cannot be re-declared using let, trying to do that will throw a Syntax error: Identifier has already been declared.
What is important if you will avoid anti-patterns like global variables, if you will keep your vars in the scope and make the methods small, you will avoid redeclaration of the vars bugs.. probably.
What is important number two: var declaration works faster then const and let, so in loops, if you HAVE TO OPTIMIZE the method you can use var instead of let/const.

Related

Assign index (i) value at the end of variable name using a for loop (JS) [duplicate]

This question already has answers here:
"Variable" variables in JavaScript
(9 answers)
Closed 2 years ago.
I've been struggling with a for loop issue. I would like to declare variables using a for loop, such that with each iteration of the for loop I have a new variable with an added index number as the end.
Here's an example of what I mean
for (var i = 1; i <= 8; i++) {
ingroupProfileText+i = console.log(i);
}
So, with each iteration, the loop is effectively doing the following:
ingroupProfileText1 = console.log(1);
ingroupProfileText2 = console.log(2);
ingroupProfileText3 = console.log(3);
ingroupProfileText4 = console.log(4);
ingroupProfileText5 = console.log(5);
ingroupProfileText6 = console.log(6);
ingroupProfileText7 = console.log(7);
ingroupProfileText8 = console.log(8);
I've looked around and I keep coming across suggestions where some suggest to use an array, eval, or window. I want something locally, and I haven't been able to make it work either way.
Any help would be much appreciated :)
Why not use object**[instead of eval which is evil and hard to maintain]** and use the key as a variable.
Just a suggestion for such a dynamic things.
let indexes = {};
for (var i = 1; i <= 8; i++) {
indexes["ingroupProfileText" + i] = i;
}
const {ingroupProfileText1, ingroupProfileText2} = indexes
console.log({ingroupProfileText1, ingroupProfileText2})
console.log(indexes["ingroupProfileText5"])
That is not possible. You can't declare variables outside of that loop. You have only 3 options.
Array
Eval
Window
And you should use an array for this purpose.

Unexpected Javascript array behavior [duplicate]

This question already has answers here:
Is JavaScript a pass-by-reference or pass-by-value language?
(33 answers)
Closed 3 years ago.
I signed the variable bubbleL1 to 1, but why the first variable in the bubbles list, which is bubbleL1 still shows undefined? I know this maybe a stupid question, but I really don't know why.
let bubbleL1, bubbleL2, bubbleL3, bubbleR1, bubbleR2, bubbleR3;
let bubbles = [bubbleL1, bubbleL2, bubbleL3, bubbleR1, bubbleR2, bubbleR3];
bubbleL1 = 1;
console.log(bubbleL1) // 1
console.log(bubbles) // [undefined, undefined, undefined, undefined, undefined, undefined]
What I want is a list with a specific name for each item in it (for the declared reason, I really don't just use bubbles[0], bubbles[1]...)
Let's say we have a list named bubbles, and we also have six variables called bubbleL1, bubbleL2, bubbleL3, bubbleR1, bubbleR2, bubbleR3. I want to put all these six variables into the bubbles list, so later I can assign values to each variables that in the list, like this:
bubbles.forEach((bubble) => {
bubble = "something";
})
bubbleL1 is a primitive and therefore copied by value.
let x = 3;
let sum = x;
sum = 3 + 5;
console.log(x); // 3
Objects and arrays, on the other hand, will exhibit your expected copy-by-ref behavior:
let x = {a: 3};
let sum = x;
sum.a = 3 + 5;
console.log(x.a); // 8

Is the use of , (comma) instead of a proper variable definition like const, var or let, in a chain of declarations, a good JS practice? [duplicate]

This question already has answers here:
What is the difference between a single comma-separated variable declaration, and multiple declarations?
(2 answers)
JavaScript variable definition: Commas vs. Semicolons
(10 answers)
Closed 3 years ago.
i've recently discovered that i can use , (comma) instead of declaring the variable properly like const num1, when in a chain mode, what i want to know is if there is any side effects in using it?
the std way =>
const num1 = 1
const num2 = 2
const num3 = 3
The , way =>
const num1 = 1
, num2 = 2
, num3 = 3
! the console shows no syntax error on both of them !
It is totally equivalent same.
You can refer Declaring and initializing two variables
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/var
There's actually no difference, as long as you know that the comma syntax keeps the kind of variable (block scoping, constant) the same all the way through. So this:
const num1 = 1;
const num2 = 2;
const num3 = 3;
Is exactly the same as this:
const num1 = 1,
num2 = 2,
num3 = 3;
Note that the second example is equivalent to
const num1 = 1, num2 = 2, num3 = 3;
This makes it clear that you are just declaring a list of variables separated with a comma. Traditionally, this is formatted as
const num1 = 1,
num2 = 2,
num3 = 3;
Note that here the comma is at the end of the line. You just happened to stumble on this a slightly different form of this same thing.

How to set one variable to another in same line without globalizing [duplicate]

This question already has answers here:
Assign two variables to the same value with one expression? [duplicate]
(5 answers)
Closed 4 years ago.
In this situation:
(function() {
const a = b = 10;
})();
console.log('a:', a); // ReferenceError: a is not defined
console.log('b:', b); // 10
The b gets defined as a var in the global scope. Is it possible o do this equals trick but make b a const as well in the same scope? Just like a?
You could set b as a parameter to your IIFE function. That way b won't be accessible outside of function scope.
(function(b) {
const a = b = 10;
console.log(b)
})();
console.log('b:', b);
I don't think so. This "trick" is possible because b = 10 returns 10. However, const b = 10 returns undefined.
Why not just use:
const a = 10;
const b = a;
?
It's not slower, and it's more readable, even if you could nest the assignments I don't really see a reason to do it

Advantages of using let over var within a function [duplicate]

This question already has answers here:
What is the difference between "let" and "var"?
(39 answers)
Closed 6 years ago.
Say I have a piece of code like this:
const number = 3;
function fooFunction() {
let numberTwo = 5;
var answer = number + numberTwo;
return answer;
}
finalAnswer = fooFunction();
console.log(finalAnswer);
Assuming an ES2015 compatible browser, what would be the advantages/disadvantages of using the above code, over:
const number = 3;
function fooFunction() {
var numberTwo = 5;
var answer = number + numberTwo;
return answer;
}
finalAnswer = fooFunction();
console.log(finalAnswer);
Are there any advantages or disadvantages, given they both return the same number?
As others have mentioned, in your example you can use let and var interchangeably. The difference is that let is block-scoped and var is not.
For example with var you can do this (prints 'foo'):
function printFoo(param) {
if (param) {
var x = "foo";
}
console.log(x);
}
printFoo("hello");
You cannot do this with let because let x is scoped to the if block and therefore is not defined.
Within the code sample you've provided, the two are interchangeable. Where let comes in handy is in limiting the scope to block-level as opposed to function-level. Using let inside of a for loop, for example.
It's worth noting that variables created with the var keyword are hoisted, whereas variables created with the let keyword are not.
There is an excellent breakdown of this topic by Kyle Simpson on David Walsh's blog: https://davidwalsh.name/for-and-against-let#implicit-hazards

Categories

Resources