Var vs let vs const explain?

As I know the const is used for read only things. The let is for reassigned things.

Now then a jQuery selector like

var divthisone = $(’#divthisone’); means it will be a const? because it not will be changed? right?

and for let will be a number like

var mynumber = 12345; means it will be a let? because can reassigned later? that case if it not change it can be a const?

Thanks.

Basically, yes. Const means that the identifier can’t be reassigned - it doesn’t mean that the value it references can’t change. If it’s a primitive value like a number, then they’re kinda locked together anyway - with const n = 1, n will always be 1. But if it’s not a primitive const o = { foo: 1 } or const a = [1,2,3], you can still manipulate that object or that array.

Use const as much as possible, let if it needs to be reassigned. You’ll find you often don’t need to reassign, const is fine in most cases. var you don’t need.

1 Like