Same variable name, two different functions

Are both variables(err) somehow going to intervene with each other? Can I use the same name or do I have to name both variables differently?

const getUser = async () => {
  try{
    const user = await getUserDataFromDB();
  }catch(error){
    // Same variable name here!
    const err = [array of errors];
    throw err;
  }
};

const getPost = () => {
  try{
    const post = await getPostDataFromDB();
  }catch(error){
    // Same variable name here!
    const err = [array of errors];
    throw err;
  }
};

const variables are block scoped, you also get an error if you redeclare or reassign them, as you get no error and they are in different blocks there is no issue

sorry block, do you mean as because even though both variables have the same name but they are each in different function(block), so there is going to be no issues? what about let?

sorry english not my first language and javascript beginner here…

thanks

Just read this

these are the blocks in which the two err variables are scoped

let is also block scoped and does not let you redeclare a variable, but let you reassign it

const a = 1;
a = 2; // invalid, error
let b = 2;
b = 3;
let b = 4; // invalid, error
1 Like

so basically in general, if I have two variables with the same name but if both of the them are not in the same scope, it will not cause issues?

and back to the codes, so currently they are in block scope, what about if I do something like this:

const one = () => {
  let/const data = [array of objects];
};

const two= () => {
  let/const data = [array of objects];
};

are both variables still in different scope? (try…catch is removed)

yes, functions are a block of their own

more or less a block is everything delimited by graph parenthesis

It doesn’t matter what you use (var, let, const), they only live inside the scope they are defined in. Like

var foo = 1;
let bar = 2;
const baz = 3;
console.log(foo, bar, baz);

function scopeExample () {
  var foo = 4;
  let bar = 5;
  const baz = 6;
  console.log(foo, bar, baz);
}

scopeExample();

That logs:

1 2 3
4 5 6

The variables defined in the function are not the same as the ones defined outside the function despite having the same names. I can access the ones outside:

var foo = 1;
let bar = 2;
const baz = 3;
console.log(foo, bar, baz);

function scopeExample () {
  console.log(foo, bar, baz);
}

scopeExample();

That logs

1 2 3
1 2 3

Basically, I can see upwards from in the function to what’s outside it, into parent scopes. But I can’t look into other scopes alongside it or below it:

function scope1() {
  let foo = 1;
  scope2();
  console.log(bar) // nope, not visible from this scope
}

function scope2() {
  let bar = 2;
  scope1();
  console.log(foo) // nope, not visible from this scope
}

Oh~ I think I get it now, so take my original code as an example, any variables I declare/create inside of a function(doesn’t matter if is inside of try…catch or not) is contained inside of that function(function scope) so even though if I declare/create another variable with the same name inside of another function, both variables(same name) won’t intervene with each other because they are in different function(function scope)! am I correct???

1 Like

Yup, that is correct. If you think about it, logically, it has to be designed to work like that, otherwise you’d get in an awful mess. If it didn’t work like that, say you had a big application with lots of files, and you accidentally reused the same variable name (error) in a function, what would happen would be:

  • that function runs first, and errors, and the error gets assigned to that variable. Now the original place it was defined (somewhere else in the application) has that error value assigend to error, when it runs it will do weird things because it’s already going to register as having an error.
  • the original function runs first, and errors. Now in this place it’s defined error has that error value, when it runs it will do weird things because it’s already going to register as having an error.

The function is like a box, you can’t manipulate the stuff inside it unless you’re in the box. It’s its own little environment: it’s only what gets returned that is visible to the outside world. It can make use of stuff from outside the box, but the stuff outside cannot reach in and use stuff that only exists in the box.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.