JavaScript-Variables, Constants, and Comments

Gerald Clark
4 min readMay 6, 2024

The most fundamental aspect of JavaScript is this concept of variables. Variables allow us to store information like a name or a value and then use that value later on in the code.

The first way to create a variable is by using the “let” keyword. So if I want to make an age variable and have it set to a specific value, I would just say this:

I can now use this variable later on in my code. Using the information we’ve gathered so far, we can log this value to the console in our browser.

So the let keyword basically allows us to overwrite the variable in the code. For example:

Here I’m changing the value of age to be 30 after its already been set to 25.

If I run this in the browser I should see both values logged in the console.

What if I don’t want to change the value of the variable though? This is where the const keyword comes in handy.

Here I’ve created a const called points and initially set the value to 100. Right after that I set it to 50. If I run this, I should get an error in the browser.

You’d be surprised how easy it is to accidentally overwrite values once the code starts getting long. For most of this series I’ll be using the let keyword unless I know for a fact I want to keep something the same throughout the entire program.

There is another keyword. Its the var keyword.

This is just another way to type let. Some programs will use var often and others won’t. I’m just pointing this out in case I do this later and don’t realize it.

There are some names we can’t use for variables. I can’t say something like:

let const = 50;

Here is a list of keywords that we can’t name our variables after.
https://www.w3schools.com/js/js_reserved.asp

Once we really start diving in to making projects you’ll see a ton of this. Until then I’ll move on to comments.

Comments are just little messages you want to have in your code without actually having those messages be seen by the user of the application you’re making. You create a comment by pressing // then typing out a message.

These are great for explaining complex sections of code to other developers, allowing them to move on to their task with a full understanding of your code without having to figure it out themselves. They’re also good for organizing your code.

A nice extension I like to use is called Better Comments.

This extension allows you to make different colored comments which obviously could be very useful.

For example, to make a blue comment you would just preface your message with //?.

This has been a super quick rundown of variables, constants, and comments! Hope this was helpful! Next up is Data Types. Stick around for more! See ya.

--

--