Changing variables


#1

I read in another topic that we shouldn’t define variables without using let, var or const, the reason being that in ‘strict mode’, a variable defined without let, var or const will be considered a global variable, and it’s not best practice. However, in the following example where we changed the variable meal to ‘Tacos’, why is the third line of code not considered a global variable?

let meal = ‘Enchiladas’;
console.log(meal);
meal = ‘Tacos’;
console.log(meal);
// output: Enchiladas
// output: Tacos


#2

including this topic would be very useful so we can also read it, please include this topic

it is a global variable, or rather, we are updating a variable. let, const or var are only needed when declaring variable, not updating variable


#3

Thanks! The link to the previous topic is:

If it is a global variable, when is a global variable considered an acceptable practice, and when is it not?


#4

declaring global variable like you did here:

let meal = ‘Enchiladas’;
console.log(meal);
meal = ‘Tacos’;
console.log(meal);
// output: Enchiladas
// output: Tacos

what you don’t want is declaring a global variable within a function, given you might overwrite a global defined variable


#5

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