Skip to Content
This is the Beta version of our new Learning Paths approach. Please email feedback.

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;

Commonly Used Data Types

KeywordData TypePurpose
byteIntegerStores an 8-bit signed number with 28 values (-127 to 127).
unsigned byteIntegerStores an 8-bit unsigned number with 28 values (0 to 255).
intIntegerStores a 16-bit unsigned number with 216 values (-32,768 to 32,767).
unsigned intIntegerStores a 16-bit unsigned number with 216 values (0 to 65,535).
longIntegerStores a 32-bit signed number, with 232 values.
unsigned longIntegerStores a 32-bit unsigned number, with 232 values.
floatNumberStores floating-point numbers (numbers with a decimal point) upto 4 bytes.
doubleNumberStores floating-point numbers upto 8 bytes.
charCharacterStores a single character in 8 bytes (internally as a number).
stringCharactersStores multiple characters (equivalent to an array of char ).
boolLogicalStores one of two values, true or false in one byte.