- Let declarations
- Reassignment
- Const declarations
- Comments in JavaScript
Programs usually execute operations on data.
Variables are like named boxes that we use to store data. They allow us to associate a piece of data with a specific name, so that we can retrieve it later using that name.
let declarations
To declare a variable in JavaScript, we can use the
let keyword.After writing
let, we write the name we want to give to the variable, then an = sign, and then the value we want to store.For example:
let age = 25 console.log(age)
The name of a variable (technically called the "identifier") can usually contain letters, underscores (
_), the dollar sign ($) and numbers, although the first character cannot be a number.In the code above, we declared a variable called
age and stored the value 25 in it.Then, we printed the value using
console.log(age).If you run this code with
node main.js, the output will be:25
Identifers are case-sensitive, which means lower and upper-case count as differences in identifiers, so for example
let age = 25 let Age = 20 console.log(age)
will print 25, because those are considered two completely separate variables!
You can also store strings (text) in a variable:
let message = "hello again" console.log(message)
This will print:
hello again
Just like before, we used
console.log() to print the value stored in the variable.Now let’s do both together:
let age = 25 let message = "hello again" console.log(age) console.log(message)
Running this will print:
25 hello again
Reassignment
Variables declared with
let can be changed after they are created.This is called reassignment.
let score = 10 console.log(score) score = 15 console.log(score)
First, we assign
10 to score, then print it.Then we change the value of
score to 15 and print it again.The output will be:
10 15
This is very useful when the value changes over time, like in a game where the score increases.
Let’s add another variable to the mix:
let score = 10 let player = "Alice" console.log(score) console.log(player) score = 20 player = "Bob" console.log(score) console.log(player)
This will print:
10 Alice 20 Bob
As you can see, both
score and player were changed.const declarations
Most of the times though, we don’t want a variable to change after it is created. For that, we use
const.const is short for “constant”. Once you assign a value to a const variable, you cannot change it.const pi = 3.14 console.log(pi)
This prints:
3.14
But if you try to do this:
const pi = 3.14 console.log(pi) pi = 99 // this line will cause an error console.log(pi)
JavaScript will give you an error like:
TypeError: Assignment to constant variable.
This is because
pi was declared using const, and you cannot change its value after that. You're communicating to the JavaScript interpreter that you don't want that variable to change.This is useful because it reduces the chances of changing it by mistake. When programs become very big, with thousands of lines of code, it's impossible to keep up with everything that is happening all at once (that's the main reason we use computers, to execute complex processes that we cannot compute with our brains), so it becomes useful to have restrictions like this, that make the program more deterministic.
It is considered best practice to always declare our values as
const, unless we're sure we want to modify them later.Comments in JavaScript
Sometimes we want to write notes in our code that are not executed. These are called comments.
Comments are ignored by the program when it runs, but are useful for explaining things to ourselves or other people.
To write a single-line comment, use
//// This is a comment const x = 10 // This is also a comment console.log(x)
This will still print:
10
The comments are just there for humans to read.
You can also write multi-line comments using
/* and *//* This is a multi-line comment. It can span several lines. */ const y = 20 console.log(y)
This will print
20
And the comment will be ignored.
You can use comments to add small annotations to your code, so that you might remember what it does and why is it written in a certain way. It can also help other programmers understand it.