let & var

Get help with Code for Teens
Post Reply
let & var

Post by Audiferus » Sat Jun 27, 2020 6:30 pm

So I'm working through your book with my nephew. When I learned coding (a looooooooong time ago), I would get an error when I used "let" to define the same thing. I started the book a few months ago, and did everything so that when I went through it with my nephew, I'd be good to go. Cue, Covid, so he has lots of free time and we finally got around to working through it together, me as his teacher, religiously reading the book, and him, on Google Meet, following along and doing the exercises.

So a few months ago, I still got an error when I used "let" to define something, now I don't.... now he doesn't ... in fact, we can define things without using let or var or const being in front of it. What gives? Is the undeclared definition a var? a const? let?

Any advice? I basically told him to keep good form, and declare only once, but I'm not sure if that's just me being a purist or if it will later affect stuff.

Site Admin
Joined:Mon May 14, 2018 10:19 pm

Re: let & var

Post by JeremyMoritz » Sat Mar 27, 2021 2:39 pm

Great question, Audiferus!

Go ahead and be a purist! declare with `let` the first time and then don't use `let` again if you reassign it. Chrome used to throw an error if you used `let` a 2nd time, but it's more forgiving now. Still, the correct practice is to use it only the first time.

`var` is the loosest of the three, which is why it is avoided my modern javascript developers. Best to understand it if you see it but try not to ever write it yourself.

Happy coding!
Jeremy Moritz
Author of Code for Teens

Post Reply