The main purpose of coding is to solve problems. What happens when you click on a button? That’s also a problem for us to solve.
So, let’s begin this article by solving a simple problem.
If you have 4 apples and you buy 27 more, how many apples do you have? Take a second and write your answer in your text editor.
What’s your answer?
// This? 31 // Or this? 4 + 27
But there’s still one problem with the code.
If you look at
4 + 27 without any context from our apple problem, do you know we’re calculating the number of apples you’re currently holding?
So, a better way is to use Algebra to substitute 4 and 27 with variables. When you do so, you’ll get the ability to write code that has meaning:
initialApples + applesBought
The process of substituting 4 with a variable called
initialApples is called declaring variables.
You declare variables with the following syntax:
const variableName = 'value'
There are four parts you’ll want to take note of:
variableName is the name of the variable you’re declaring. You can name it anything, as long as it follows these rules:
- It must be one word
- It must consist only of letters, numbers or underscores (0-9, a-z, A-Z,
- It cannot begin with a number.
- It cannot be any of these reserved keywords
If you need to use two or more words to name your variable, you should join the words together, but capitalize the first letter of each subsequent word. This weird capitalization is called camel case.
A good example of a camel-cased variable is
The value is what you want the variable to be. It can be primitives (like strings and numbers etc) or objects (like arrays and functions).
= in Math. Don’t get confused.
= means assignment. When you use
=, you set (or assign) the value on the right hand side (RHS) of the
= sign to the left hand side (LHS) of the
In the following statement, you set the variable
initialApples to the number 4.
const initialApples = 4
console.log this variable, you can see that
initialApples is 4.
console.log(initialApples) // 4
Evaluation before assignment
Every variable can only take up one value. So, if you have an equation that needs to be evaluated on the RHS, it will be evaluated before it is assigned to the variable.
const initialApples = 4 const applesToBuy = 27 const totalApples = initialApples + applesToBuy
applesToBuy (which results to 31) before assigning the results to
totalApples. This is why you get
31 if tried to log
console.log(totalApples) // 31
The const keyword
const is one of three keywords you can use to declare variables. There are two other keywords –
All three keywords declare variables, but they’re slightly different from each other.
Const vs let vs var
let are keywords made available to us in ES6. They are better keywords to create variables than
var because they’re block scoped while var is function scoped.
For now, let’s concentrate on the difference between
Const vs let
If you declare a variable with
const, you cannot reassign it with a new value. The following code produces an error:
const applesToBuy = 22 // Reassigning to a variable declared with const results in an error applesToBuy = 27
If you declare a variable with
let, you can reassign it with a new value.
let applesToBuy = 22 applesToBuy = 27 console.log(applesToBuy)
Should you use const or let?
Understanding whether you should use
let is more of an advanced topic.
When you’re starting out, using
let would be much simpler than using
However, as you write more programs, you’ll slowly realize that you want to refrain from reassigning your variables, and you’ll begin to use
let. Why you’ll want to do so is a different topic for another day.
Since you’re going to use
let anyway when you write more advanced programs, it’s better to get into the habit of preferring
let when you’re starting out.
In case you’re wondering, don’t use
var anymore, because there’s no need for it.
const are much better than
= means assignment.
When you declare variables, use camelCase to name your variables. Avoid the reserved keywords.
You can declare variables with either
var. As much as possible, you’ll want to use
let when you need to reassign values. There’s no longer a need to use