Changing variables

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

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

2 Likes

Thanks! The link to the previous topic is:
https://discuss.codecademy.com/t/why-to-use-var-it-is-working-also-without-it/203614/2

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

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

2 Likes

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