Variables
In Schools, we all have encountered those maths problems where we were given few letters with some value assigned to it. Let me jog your memory using a very simple math problem as shown below.
let value of x be 3 and y be 4, then what will be x + y ?
I know it’s a very basic problem. Right? It was pretty easy and our mind was trained to see these X and Y letters as numbers. So if we saw these problems, our minds immediately replaced x + y with 3 + 4 and we got the answer 7.
But did you observe something here? While problem literally statement said “add X with Y”, but we never got confused with the letters! We immediately replaced letters with their respective numeric values and performed the arithmetic problem.
Think of variables exactly the same way you thought of the letters in the above math problem. When you saw x, you immediately knew that it meant 3 and same for y respectively.
What is a Variable
Section titled “What is a Variable”A variable is like a container that holds a particular value on which a certain operation will be performed in the future. As a box contains some stuff, a variable reserves some space in the memory of your computer which may contain some value.
Declaring Variables
Section titled “Declaring Variables”In JavaScript, there are three ways to declare a variable but first let’s look at the syntax.
variable_keyword variable_name = some_value
In above syntax:
- variable_keyword refers to
var,constandlet. - variable_name refers to user defined name for the variable.
- some_value refers to the value that variable contains.
Here’s an example:
const username = "thatsmanmeet";
In above example:
- const is variable_keyword.
- username is variable_name.
- thatsmanmeet is some_value.
Variable_Keywords
Section titled “Variable_Keywords”const: It is used to define a variable whose value never changes.let: It is used to define a variable whose value can be changed over time.var: It works just likeletbut is an old way in JS.
Example:
// Declaring variables in JavaScript:
const birthYear = 2002;
let age = 23;
var weight = "75Kg";
// Changing a let variable:
age = age + 1; // now age is 24
// Attempting to change a const:
birthYear = 2003; // ❌ TypeError: Assignment to constant variable.
In above code example, const is used to define the birthYear as 2002 as the birth year of a person will remain constant and will not be changed. Now the age is 23, but it will be changed next year to 24, so let will be appropriate here. In case of var, I have used weight, which can also change over time. Hence this shows how these variables can be used in context of their immutability.
We can also see how assigning the value again to birthYear will cause Error in our code and will crash our code. We will learn about errors in the Error Handling article.
🧵 Wrapping It Up
Section titled “🧵 Wrapping It Up”Variables are the building blocks of every program: they let us store, update, and work with data as our code runs. You’ve now seen:
- What a variable is – a named container for a value
- How to declare one – using
const,let, orvar - When to pick each keyword – immutability with
const, re-assignable withlet, and the legacy behavior ofvar
So that’s all about introduction to the variables. let’s continue to next section to learn more about the variables.