Var let and const

I thought unlike described in the challenge ‘Explore Differences Between the var and let Keywords’ that let behaves in the way that you can override the variable. Instead const cannot be changed.
In the challenge is said var can be overridden and let not.

Your code so far


var catName;
var quote;
function catTalk() {
"use strict";

catName = "Oliver";
quote = catName + " says Meow!";

}
catTalk();

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:84.0) Gecko/20100101 Firefox/84.0.

Challenge: Explore Differences Between the var and let Keywords

Link to the challenge:

This is overwriting variable declarations and it cannot be done with let:

var camper = 'James';
var camper = 'David'; // OK
let camper = 'James';
let camper = 'David'; // throws an error

But this is perfectly fine:

let age = 10;
age = 30;
console.log(age); // 30
1 Like

I also do some courses in Codecademy and there is mentioned:

"You may recall that you can declare variables with both the let and const keywords. Variables declared with let can be reassigned.

Variables declared with the const keyword cannot be reassigned. However, elements in an array declared with const remain mutable. Meaning that we can change the contents of a const array, but cannot reassign a new array or a different value."

Is reassign different from overwriting then?

“Declarations” is what makes a difference. You cannot declare the same variable twice using let.

Overwriting variable declaration:

let camper = 'James';
let camper = 'David'; 

Overwriting / reassigning variable:

let age = 10;
age = 30;
1 Like

In which category goes:

let camper = ‘James’ ;
camper = ‘David’ ;

or

let age = 10 ;
let age = 30;

?

Here you’re just changing the value of camper variable from ‘James’ to ‘David’ - nothing wrong with that.

Here you’ll get a SyntaxError:

Uncaught SyntaxError: Identifier ‘age’ has already been declared

1 Like

Yeah, I don’t like the word “overwrite” as it is up to interpretation. I’m not aware that it has a technical definition. Are you referring to redeclaration or reassignment or changing the value?

Correct, let and const cannot be redeclared. The let can be changed and reassigned but const cannot. If the const is pointing to a reference type, the values in that reference type can be changed, even though the reference cannot be reassigned. A var is similar to a let but has its own legacy weird behavior like redeclaration and hoisting.

Don’t overthink it. Just use let for any primitive you want to change or any reference type you will want to reassign. You can pretty much use const for everything else.

Yes I think it was the difference between redeclaration and reassignment that was not clear enough for me.

To “declare” is to bring the variable into existence. This is done with the var, let, and const keywords.

To “assign” is to put a new value into the variable. This is done with the assignment operator, =. Variables declared with const cannot be re-assigned.

To “mutate” is to modify an existing complex value. It means adding, removing, reordering, pieces of data but not to discard and replace the whole object with something else. Values stored in const variables can still be mutated.

2 Likes

To add to ArielLeslie’s excellent list or definitions, I would include “initialize” - to give it an initial value at the time of its declaration. Initialization is optional for let and var but is required for const.

3 Likes

Thanks for the definitions and explanations. This helped for better understanding!

I think the examples might be more clear if the assignments just got removed. Although as mentioned the technical communication isn’t great either, so some rewriting of the challenge text might be warranted.

As said, the let keyword simply disallows the same identifier (variable name) to be declared more than one time per scope.

let name;
let name; // Uncaught SyntaxError: Identifier 'name' has already been declared
let name;
if (true) {
  let name; // works because of block scope
}
1 Like
let testArr = ["hey" ]; // value is an array
testArr = 6; // value is now a number - totally cool because the first initialization used 'let'

const testArr2 = ["whoa" ]; // value is an array
testArr2 = 300 // Throws a TypeError: Assignment to constant variable. So the value can't be changed when using `const`.

const testArr3 = [6, 7, 8]
testArr3.push(9)
console.log(testArr3) // returns  [6, 7, 8, 9]

const testArr4 = [6, 7, 8]
testArr4 = [...testArr4, 9] // Throws an error because of  'const'

let testArr5 = [6, 7, 8]
testArr5 = [...testArr4, 9] // Totally cool here because of 'let'. Returns [6, 7, 8, 9]