Coding

Data Types in Programming Explained (With Examples)

A data type tells the computer what kind of value a piece of data is (a whole number, a decimal, text, true or false, a collection) and therefore what you can do with it and how much memory it needs. Data types in programming fall into two broad groups: primitive types that hold one value, and compound types that group several.

The examples below use Python, JavaScript and Rust, because their official documentation spells out the rules clearly and they sit at different points on the typing spectrum.

What is a data type?

A data type is a classification that determines which values are allowed and which operations make sense. You can add two numbers, but adding a number to a list is either an error or a conversion, depending on the language. The type also decides how the value is stored. A 32-bit integer takes 4 bytes. A Unicode character in Rust takes 4 bytes. A string can take any number of bytes.

Every language has a built-in set of types, and most let you define new ones (classes, structs, enums). The built-in ones are what you meet first.

Primitive (scalar) data types

Primitive types hold a single value. The Rust book calls them scalar types and lists four: integers, floating-point numbers, booleans and characters. Other languages use the same ideas with different names and rules.

KindHoldsPythonJavaScriptRust
IntegerWhole numbersintNumber, BigInti8 to i128, u8 to u128, isize, usize
Floating pointDecimal numbersfloatNumberf32, f64
BooleanTrue or falseboolBooleanbool
TextCharactersstrStringchar (one character), String

JavaScript's documentation lists seven primitive types: Null, Undefined, Boolean, Number, BigInt, String and Symbol. Python's documentation lists three numeric types: int, float and complex.

Integers and their limits

In Rust you choose the size. A signed type with n bits covers -(2^(n-1)) to 2^(n-1) - 1, and an unsigned type covers 0 to 2^n - 1. So i8 covers -128 to 127, and u8 covers 0 to 255. If you don't say, Rust uses i32.

JavaScript has no separate integer type for ordinary numbers. A Number can only safely store integers from -(2^53 - 1) to 2^53 - 1, which are Number.MIN_SAFE_INTEGER and Number.MAX_SAFE_INTEGER. Outside that range the value is a double-precision floating-point approximation. For larger whole numbers, MDN points to BigInt, which holds integers of arbitrary size. The MDN example shows the difference:

Number.MAX_SAFE_INTEGER + 1 === Number.MAX_SAFE_INTEGER + 2; // true
const x = BigInt(Number.MAX_SAFE_INTEGER);
x + 1n === x + 2n; // false

Mixing a BigInt and a Number in arithmetic throws a TypeError, so you convert explicitly.

Overflow

Overflow happens when a result does not fit in the type. Rust handles it differently by build mode. In a debug build it checks and panics at runtime. In a release build (--release) it does not check, and the value wraps using two's complement: for a u8, 256 becomes 0 and 257 becomes 1. The Rust book says to treat relying on that wrapping as an error, and offers explicit methods instead: wrapping_*, checked_* (returns None on overflow), overflowing_* and saturating_*.

Booleans are numbers in Python

Python's bool is a subclass of int, and True and False behave like 1 and 0 in many numeric contexts. The documentation discourages relying on that and recommends converting explicitly with int(). In practice it means True + 1 evaluates to 2, which surprises people who expect a type error.

Characters and strings

Python strings are immutable sequences of Unicode code points, and Python has no mutable string type. Rust's char is 4 bytes and represents a Unicode scalar value, written with single quotes ('z'), while strings use double quotes. JavaScript strings are also immutable primitives.

Compound data types

Compound types group values. The common ones are arrays or lists (an ordered sequence), tuples (a fixed group, often of mixed types), and objects or dictionaries (named fields or key-value pairs).

Rust has two primitive compound types. A tuple has a fixed length and can mix types. An array has a fixed length and one element type:

let tup: (i32, f64, u8) = (500, 6.4, 1);
let (x, y, z) = tup;      // destructure
let a: [i32; 5] = [1, 2, 3, 4, 5];

Python's list is a mutable sequence, while tuple is an immutable one. In JavaScript, all types except Object define immutable values, so objects (including arrays) are the only mutable values. Whether a type is mutable decides if a function that receives it can change it behind your back, which is why this property matters when you pass data around.

Static vs dynamic typing

The other axis is when types are checked. Rust is statically typed: it must know every variable's type at compile time. It can usually infer the type, but when several are possible you must annotate. This line needs the u32, and without it the compiler reports error E0284: type annotations needed:

let guess: u32 = "42".parse().expect("Not a number!");

JavaScript is dynamically typed. MDN says variables are not directly associated with any particular value type, and any variable can be assigned and re-assigned values of all types. So this is legal:

let value = 42;
value = "forty-two";

Static typing catches mismatches before the program runs. Dynamic typing is more flexible when writing, but type mistakes show up only when that line executes.

Checking a type at runtime

In JavaScript, typeof returns a string naming the type. One quirk to know: typeof null returns "object", so MDN says to test for null with === null. Null is a primitive despite what typeof reports.

How to choose a type

  • Counts and indexes: an integer. In Rust, usize is mainly used for indexing collections, and it is 64 bits on a 64-bit architecture and 32 bits on a 32-bit one.
  • Anything that must exceed the type's range: a wider integer type, or BigInt in JavaScript.
  • Measurements: a float. Rust defaults to f64.
  • Two states: a boolean, not the numbers 0 and 1.
  • Fixed groups of mixed values: a tuple or a struct. Same-type collections that grow: a list.

Frequently asked questions

What are the basic data types in programming?
Most languages provide integers, floating-point numbers, booleans, characters or strings, and some way to group values, such as arrays, lists, tuples or objects. The exact names and sizes differ by language, so check the language's own documentation.
What is the difference between a primitive and a compound data type?
A primitive (or scalar) type holds a single value, such as a number or a boolean. A compound type groups several values into one, such as a tuple, array, list or object. Rust's book, for example, calls integers, floats, booleans and chars scalar types, and tuples and arrays compound types.
What is the difference between static and dynamic typing?
In a statically typed language like Rust, every variable's type must be known at compile time, so type errors are reported before the program runs. In a dynamically typed language like JavaScript, variables are not tied to a value type, and any variable can hold values of any type.
Why does the size of an integer type matter?
A fixed-size integer can only hold a limited range. An 8-bit unsigned integer holds 0 to 255. Going past the limit is overflow, which some languages treat as an error and others silently wrap around.

Sources