Can var declaration also go override another functions let declaration?

const call = () => {
var a = 2;

console.log(a);
}

const call2 = () => {
let a = 1;

console.log(a);
}

call(); -> 2
call2(); -> 1
const call = () => {
var a = 2;

console.log(a);
}

const call2 = () => {
let a = 1;

console.log(a);
}

call(); -> 2
call2(); -> 1
IS this correct? Var cannot also go across the other functions right?
8 Replies
Jochem
Jochem13mo ago
it stays in the function scope if it's defined there https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/var but in general, just never ever use var it's only still there for backwards compatibility reasons
Mert Efe
Mert Efe13mo ago
in general yeah but for certain cases i think handy
Jochem
Jochem13mo ago
it's a dangerous tool, I'd just steer clear just add let a; in front of the block of code you're using it in if you really need a defined variable using var it goes all the way to the top of the scope you're in, and that's just going to cause havoc if you have a couple hundred lines of code and forgot you put var a = 2; on line 158 and reuse a somewhere before that
Mert Efe
Mert Efe13mo ago
const mapServices = async (services: any, orderDetails: any, t: any, vat_rates?: IVatRates) => {
const { vat_rate_decide2 } = vat_rates;

let serviceArray: any = [];

await BluebirdPromise?.mapSeries(services, async function (service: any, index: any, arrayLength: any) {


({ service, orderDetails, serviceArray } = await priceCalculationService.calculateMapServices(service, orderDetails, vat_rate_decide2));

return;
});

return {
serviceArray,
orderDetails,
};
};
const mapServices = async (services: any, orderDetails: any, t: any, vat_rates?: IVatRates) => {
const { vat_rate_decide2 } = vat_rates;

let serviceArray: any = [];

await BluebirdPromise?.mapSeries(services, async function (service: any, index: any, arrayLength: any) {


({ service, orderDetails, serviceArray } = await priceCalculationService.calculateMapServices(service, orderDetails, vat_rate_decide2));

return;
});

return {
serviceArray,
orderDetails,
};
};
Ok i didn't use var f.e but if not works like this i was thinking to use var before ({ service and don't see any harm here i think we shouldn't always talk from handbook directly , no danger in this f.e if i want to override with var after destructure yea but sometimes that's the intention
Jochem
Jochem13mo ago
then be explicit about it
ErickO
ErickO13mo ago
it certainly shouldn't be there's no good reason to be using var these days, any "that's the behavior I want" use case is someone trying to write "clever" code and that is just bad code also not relevant but... if you're gonna use any everywhere in your typescript, are you even using typescript?
Mert Efe
Mert Efe13mo ago
if it's not relevant why are you asking? should I stop my work and explain you the context and the where and why and how to satisfy your egoic issue Jochem explained clearly and i expressed my opinion if it's in the function scope your concerns are totally baseless and irrelevant just talking from the book the sentences you memorized question is not even about if i should use var or not
ErickO
ErickO13mo ago
Calm down, I did not disrespect you at any moment for you to come at me like that, "egoic" issue? it was a simple question how is it even related to ego? the question was answered by Jochem yes! and thus I was not answering it again why would I right? I am backing up the notion that var is not something that should be used these days
if it's in the function scope your concerns are totally baseless and irrelevant
how are they "baseless" first of all, and irrelevant they are not, a function scope doesn't magically fix the issues var has, it will not pollute the global scope but it WILL pollute your function scope, any if/else statements any for loops where a var is declared will pollute the function ANY block where a var is declared escapes that and goes up to the nearest function. Hoisting is not a good nor safe way of doing anything, using a variable before you even initialize it should be an error and with var it simply isn't. Redeclaring a variable should also not be allowed yet you can do just that with var so if you or anyone on your team forget there's already a user var and redeclare it you just lost the other one with 0 errors raised.
just talking from the book the sentences you memorized
Maybe you do that, but I don't, I talk from real experience on this field and when I say something I make SURE what I say is right, you may think you are a smarter developer than the countless experts telling you to stop using var but you are not. At the end of the day it is your code and your career so do whatever you want, it'll only hurt yourself down the road. Also be careful with this line of work, if such an innocuous question about your code triggers such a response you'll have a really bad time with all the code reviews done within teams.