PROGRAMMING-CONCEPTS

Data Types: Definition, Syntax, and Examples

A data type defines the kind of value that can be stored and manipulated in a program. It tells the computer what kind of data it’s dealing with—numbers, text, lists, or something else—and determines how that data can be used.

Every programming language has built-in data types, ensuring values are handled correctly during operations, comparisons, and storage.


Why Data Types Matter

Data types prevent mistakes by setting clear expectations for how information behaves. For instance, you can’t meaningfully multiply a sentence by another sentence, but you can multiply two numbers.

They also help programs run efficiently. Computers allocate different amounts of memory based on a value’s type, which keeps performance optimized and avoids unexpected results.


Common Data Types Across Languages

Most programming languages share similar fundamental data types, even if their names vary slightly.

1. Numbers

Numbers represent integers or decimal values used in calculations.

age = 30
price = 19.99

In JavaScript and TypeScript:

const count = 42;
const temperature = 23.5;

Swift distinguishes between integers and floating-point numbers:

let score: Int = 95
let pi: Double = 3.14159

2. Strings

Strings store sequences of characters like words, phrases, or symbols.

message = "Hello, world!"

In JavaScript:

const name = "Luna";
console.log("Hi, " + name);

Strings can also include escape characters (\n for new line, \t for tab) or use interpolation:

let user = "Milo"
print("Welcome, \(user)!")

3. Booleans

Booleans represent only two possible values: True or False. They’re essential for conditional logic and control flow.

is_active = True

JavaScript example:

const isLoggedIn = false;
if (!isLoggedIn) {
  console.log("Please log in.");
}

In SQL, booleans appear in WHERE clauses:

SELECT * FROM users WHERE active = TRUE;

4. Lists, Arrays, and Collections

These data types group multiple values in an ordered sequence.

colors = ["red", "green", "blue"]

JavaScript and TypeScript use arrays:

const scores = [90, 80, 100];

Swift also supports arrays with strict typing:

let fruits: [String] = ["apple", "banana", "cherry"]

5. Objects and Dictionaries

Objects (or dictionaries) store data as key-value pairs.

person = {"name": "Nina", "age": 28}
print(person["name"])

JavaScript uses a similar structure:

const user = { name: "Alex", age: 32 };
console.log(user.name);

Objects make it easy to organize related data together.


6. Null and Undefined

These represent the absence of a value.

In Python, the equivalent is None:

result = None

In JavaScript:

let data;
console.log(data); // undefined
data = null;       // now intentionally empty

In SQL, NULL means a field contains no data at all.


Static vs. Dynamic Typing

Languages handle data types differently.

  • Statically typed languages (like Swift or TypeScript) require you to declare types explicitly. Errors are caught before the program runs.
  • Dynamically typed languages (like Python or JavaScript) determine types at runtime. This makes code faster to write but easier to break if you’re careless.

Example (TypeScript):

let age: number = 30;
age = "thirty"; // Error: Type 'string' is not assignable to type 'number'

Example (Python):

age = 30
age = "thirty"  # Works, but can cause bugs later

Complex and Derived Data Types

Beyond primitives, most languages support complex or composite data types that store multiple values or behaviors.

Tuples (Python, Swift)

Tuples group values of different types together.

user = ("Nina", 28, True)
let info = ("Alex", 32, false)

Objects and Classes (OOP)

Objects can have both data (attributes) and actions (methods):

class Car:
    def __init__(self, brand):
        self.brand = brand
my_car = Car("Tesla")
print(my_car.brand)

TypeScript and Swift use similar object-oriented structures with explicit typing.

Functions as Data

In many modern languages, functions are first-class data types. They can be stored in variables or passed as arguments:

const greet = (name) => `Hello, ${name}`;
console.log(greet("Luna"));

Type Conversion

Sometimes you need to convert one data type into another, such as turning text into a number or a number into a string.

Example in Python:

num_str = "42"
num = int(num_str)

Example in JavaScript:

const num = Number("42");

Conversions must be intentional, since mixing incompatible types can lead to unexpected results.


Type Checking

Checking what kind of data you’re dealing with helps prevent bugs.

Python:

print(type(42))           # <class 'int'>
print(isinstance("hi", str))  # True

JavaScript:

console.log(typeof 42);        // number
console.log(Array.isArray([])); // true

TypeScript enforces this automatically at compile time, reducing runtime errors.


Common Mistakes

Developers often run into problems when they ignore or misuse data types. Typical mistakes include:

  • Mixing incompatible types ("5" + 2 results in "52" in JavaScript).
  • Forgetting to handle null or undefined values.
  • Comparing numbers and strings without conversion.
  • Assuming arrays or objects behave like primitive values.
  • Declaring a variable without initializing it properly.

Type errors are among the most common bugs in beginner codebases.


Best Practices

To write cleaner, safer code:

  • Use clear, consistent types throughout your program.
  • Initialize variables with meaningful defaults.
  • Convert types explicitly rather than relying on automatic coercion.
  • Use linters or IDEs to detect type inconsistencies early.
  • In TypeScript or Swift, favor strong typing—it prevents subtle logic errors.

Good type discipline makes your code more predictable, maintainable, and easier to debug.


Summary

Data types define what kind of information a program handles and how that information behaves. Understanding them helps prevent bugs, control memory usage, and write reliable, efficient code.

From numbers and strings to arrays, objects, and booleans, data types are the foundation of all programming. They give structure to logic, meaning to operations, and stability to complex systems.

Learn to Code for Free
Start learning now
button icon
To advance beyond this tutorial and learn to code by doing, try the interactive experience of Mimo. Whether you're starting from scratch or brushing up your coding skills, Mimo helps you take your coding journey above and beyond.

Sign up or download Mimo from the App Store or Google Play to enhance your programming skills and prepare for a career in tech.

Reach your coding goals faster