Beginner question about let keyword

Hi, I just started learning Javascript and I didn’t understand the output of this code.

I expected it to be “freeCodeCamp is cool” but instead it prints “freeCodeCamp is awesome” twice.

1- I thought let keyword makes the variable unchangeable, why did the line after that changed it?
2- Why does it print the output two times when I run it?

Your code so far


const FCC = "freeCodeCamp"; // Change this line
let fact = "is cool!"; // Change this line
fact = "is awesome!";
console.log(FCC, fact); // Change this line

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.3 Safari/605.1.15

Challenge: Declare a Read-Only Variable with the const Keyword

Link to the challenge:

That’s the const keyword.

That’s sort of a quirk with the system. It is printing out once because you have a console.log in your code and then printing out again when the code is actually tested. Nothing to worry about.

2 Likes

Let’s say you have:

let greet = 'hello'

you can’t do this:

greet[0] = 'J'

or this

let greet = 'hello'
let greet = 'hola'

but you can reasing it like:

greet = 'Jello'

With const you can’t modify a variable(the answerr to your question), BUT if you have an object create with const like:

const obj = {
    'my name' : 'pablo'
}

obj['my name'] = 'Andre'

You can still modify it. So you need to freeze it:

Object.freeze(obj);

With const you can modify objects unless you freeze them.

1 Like