What Are Variables In Mojo?

Mojo is a trending programming language in 2025 for AI development, high-performance computing, and Python interoperability. This language also supports you in writing flexible and fast programs.

A variable is an important concept because it stores and manipulates data in the Mojo program.

In this article, we will cover:

  • What are variables in Mojo
  • How to declare variables
  • Why Use let for Immutability?
  • Simple code examples
  • Shadowing in Mojo Variables
  • Your practice exercises

What is a variable in Mojo?

A variable in Mojo is a storage location that holds a value. For example, it is a container where we can store information, such as a number, a string, or a more complex object like a list and a matrix.

Mojo is a strongly typed variable, which means each variable has a specific type like Int, Float64, or String. And this type makes your program faster with fewer runtime errors.

How to Declare a Variable In Mojo?

There are two ways to declare a variable in Mojo, like:

  1. Immutable variable (default) – We can not change its value after assigning.
  2. Mutable variable – We can change its value if declared with the var keyword.

Here, the simple example of let and var keywords:

fn main():
let name: String = "Mojo" # Immutable variable
var counter: Int = 10 # Mutable variable

print(name)
counter += 5
print(counter)

Output:

Mojo
15

Both are declared as variables, but let is constant and var allows modification.

Why Use let for Immutability?

Let keyword provides us a several advantages like:

  • Immutable data can be optimised by the compiler.
  • It prevents accidental changes and modifications to important values.
  • Creates a functional programming style and makes them predictable.

Simple examples of Mojo Variables

Example 1: Tracking a Score in a Game

fn main():
var player_score: Int = 0
print("Game started! Score:", player_score)

# Player collects items
player_score += 10
player_score += 5

print("Final score:", player_score)

Output:

What Are Variables In Mojo

See this code carefully, we used a var because the score changes as the game progresses.

Example 2: Immutable Configuration

fn main():
let app_name: String = "MojoApp"
let version: Float64 = 1.0

print("Running", app_name, "version", version)

Output:

What Are Variables In Mojo

We used a let for safety, because the Application name and version do not change.

Example 3: Converting and Using Different Types

fn main():
var age = 21
print("Age as integer:", age)

let age_text: String = f"Age: {age}" # Shadowing with a new type
print(age_text)

Output:

What Are Variables In Mojo

This example shows how variables are transformed into different types with clean code.

Shadowing in Mojo Variables (Optional Feature)

Mojo supports the Shadowing method, where you can reuse the same variable name with a different type, similar to Rust Shadowing.

For example:

fn main():
let value: Int = 50
print("Original:", value)

let value: String = f"Value is {value}" # New variable, same name
print(value)

Output:

What Are Variables In Mojo

This example shows a Shadowing where the first int value was 50, then a new variable replaces it as a string.

Your Exercises Using Mojo Variables

Exercise 1: Temperature Tracker

Create a program with the following conditions:

  1. You declare today’s temperature (in Celsius) as an immutable variable using the let keyword.
  2. You create a mutable variable for tomorrow’s temperature (starting at 0).
  3. Update tomorrow’s temperature by adding 3 degrees to today’s value.
  4. Print both temperatures as an output.

Your output must be like this:

Today: 30°C, Tomorrow: 33°C

Answer to Exercise 1:

fn main():
# Step 1: Today's temperature (immutable)
let today_temp: Int = 30

# Step 2: Tomorrow's temperature (mutable, starting at 0)
var tomorrow_temp: Int = 0

# Step 3: Update tomorrow's temperature by adding 3 degrees
tomorrow_temp = today_temp + 3

# Step 4: Print both temperatures
print(f"Today: {today_temp}°C, Tomorrow: {tomorrow_temp}°C")

Output:

Today: 30°C, Tomorrow: 33°C

Exercise 2: Shopping Cart Calculator

  1. Use var to create a variable cart_total starting at 0.
  2. Add three different item prices to it, like 15, 40, 25.
  3. Shadow cart_total to store the total as a string with the message “Total bill is: X”.
  4. Print the string as an output.

Your output must be like this:

Total bill is: 80

Answer to Exercise 2:

fn main():
# Step 1: Start cart total as 0
var cart_total: Int = 0

# Step 2: Add three different items
cart_total += 15
cart_total += 40
cart_total += 25

# Step 3: Shadow cart_total as a string with message
let cart_total: String = f"Total bill is: {cart_total}"

# Step 4: Print the final message
print(cart_total)

Expected Output:

Total bill is: 80

Leave a Comment