Learn Programming Fundamentals
1 Programming Fundamentals
3 Conditional Structures
4 Strings
5 Arrays
6 Objects
8 Functions
Think of your brain: everything you learn is kept there. It's like a storage unit for all your knowledge. Then, based on what's stored in your brain, you make decisions or take actions.
Similarly, in computers, we need to save information somewhere so we can work with it and make decisions or produce results. Variables serve as placeholders for storing and manipulating data in the computer's memory. They enable us to retain and manipulate information throughout the execution of a program. Just as the brain stores and processes knowledge, variables store and facilitate the manipulation of data within a computer program.
Computers store data in specific locations within their memory. These locations are known as memory blocks, and they cannot be directly accessed or manipulated by programmers. To interact with the data stored in these memory blocks, we use variables.
Variables serve as symbolic names or identifiers that represent storage locations in computer memory. They allow us to store and manipulate data within a program. When we assign a value to a variable, the value is stored in a specific memory block, and we can refer to that value using the variable's name.
For example, if we store the value 9
in a variable called age
, the value 9
is stored in a memory block, and the variable age
serves as a reference to that memory block. We can then retrieve the value stored in age
by referencing the variable in our program.
The syntax of defining a variable varies in each programming language, but the concept is the same.
In JavaScript, we can declare a variable using var
, let
, or const
keyword. When you use var
, let
, or const
to declare a variable, you are creating a new variable with the specified name and assigning it a value.
var age = 90;
A variable's value can change during the program execution:
var age = 90; age = 30; age = 20; // The latest value of age is 20
However, in many languages, like JavaScript, const
is for making a constant variable. Once you set a value to a constant variable, you can't change it.
const PI = 3.14159; PI = 10; // This will cause an error!
In this example, const PI = 3.14159;
makes a constant variable named PI
. After setting it, you can't change its value. If you try, the program will give an error when you run it.