Variables and Data Types
Computer programs generally operate on data in some form or manner. Data is stored in some form of memory, either transient RAM or persistent disk. The locations at which various data values are stored are referred to in machine language as memory addresses. In a high-level language, these are referred to by human-readable names, known as variables. Variables are called so because the values stored in the memory location they refer to can change as the program executes its instructions.
All languages define various rules on what you can use as variables; however, they are a combination of alphanumeric and some other characters, such as a hyphen (-) or an underscore (_), and generally cannot be only numbers or start with a number. While they can be any cryptic combination, most programmers would use words that clearly indicate the value stored in that variable in the context of the program.
Variables are usually first declared, which is an indication to the program that this will be an identifier used for the memory location. When declaring a variable it may also be initialized or given a value to begin with (could be 0 or an empty string). Initialization is not mandatory in all languages but is highly recommended. The reason is that the variable will point to any available memory location, which may have a random value stored by some other program earlier, and the new variable will start using that random value, possibly leading to errors in your program.
Variables are assigned values using the equal to (=) sign, with the variable on the left and the value to be assigned on the right.
This is how variables are declared and initialized in a program:
let number_var = 0;
let string_var = '';
Data Types
In some programming languages, you need to specify a data type when declaring the variable, which tells the program type of data stored in the variable. The most commonly used data types are strings (as a series of one or more characters), numbers (integers and decimals), and boolean (true or false). When a variable is declared with a certain data type, the program can only store values of that data type in the variable. The data type also defines the operations that can be done using the value in that variable. For example, an arithmetic operation such as addition or multiplication on a string value may not be meaningful.
Programming languages in which you must specify the data type are referred to as strongly typed languages. Some programming languages like JavaScript are loosely typed. It means that you can declare a variable without specifying its data type and assign any kind of value to it. The program will determine the data type based on the value assigned. It is convenient in some ways but also could lead to errors during execution (referred to as run-time errors) if an operation not possible on a particular data type is attempted. In strongly typed languages, such errors are flagged during the compilation time by itself.
When declaring a variable, it is also a good idea to initialize it by assigning a value. The initial value can be anything based on the objective of your program. Usually, string variables are initialized, to an empty or blank string, number variables are initialized to 0, and Boolean variables to either true or false, but that is a convention, not a rule.
let number_var = 0; // JavaScript is untyped
let boolean_var = true;
// C is a typed language
int number_var = 0;
bool boolean_var = true;