FAQ: Variables - Create a Variable: const

I see. Thank you very much!

So then what if you accidently assign const to a value you don’t want there? can you delete it?

We cannot delete a const object. The code will need to be edited.

So… const variables must be assigned a value, and we verified that in this exercise. I was curious if they could be assigned a null value, and it looks like they can. Is this function useful/applicable to anything? I wouldn’t think so given that const variables, can’t be reassigned, but thought I’d check here. TIA!

Since null is already a defined primitive, it wouldn’t need to be assigned to a const variable. There would be no point to it.

1 Like

Can anyone share a real world example of the different use cases between let and const? I understand you can’t reassign const, but I’m not sure what that would mean for the developer or for the user.

Think of const as a tool that from time to time, helps prevent you from shooting yourself in the foot. It’s not really necessary - you could just make everything let (or var), but it’s good code hygiene.

As your code grows in size and complexity, you may reassign something somewhere and not realize it was reassigned somewhere else. If the variable were a const, you won’t be able to do that. (And your linter will pick it up even before runtime, hopefully).

You probably want to manipulate that array, not reassign it. You probably don’t ever want to clobber that arrow function definition.

Personally, I make pretty much everything I can into a const and only switch it to let when I realize it’s something that needs to be reassigned.

1 Like