Menu
Coddy logo textTech

Exponentiation Operator

Part of the Fundamentals section of Coddy's Lua journey — lesson 14 of 90.

The exponentiation operator represented by the caret symbol ^. This operator allows you to raise a number to a power, which means multiplying a number by itself a certain number of times.

Here's how the exponentiation operator works:

result = 2 ^ 3    -- result is 8 (2 × 2 × 2)
result = 5 ^ 2    -- result is 25 (5 × 5)
result = 10 ^ 1   -- result is 10 (10 to the power of 1)
result = 4 ^ 0    -- result is 1 (any number to the power of 0 is 1)
challenge icon

Challenge

Easy

Calculate the area of a square garden plot using the exponentiation operator. Create a variable called sideLength and assign it the value 7. Then use the exponentiation operator to calculate the area by raising the side length to the power of 2, and store this result in a variable called area. Print the calculated area.

Cheat sheet

The exponentiation operator is represented by the caret symbol ^ and allows you to raise a number to a power:

result = 2 ^ 3    -- result is 8 (2 × 2 × 2)
result = 5 ^ 2    -- result is 25 (5 × 5)
result = 10 ^ 1   -- result is 10 (10 to the power of 1)
result = 4 ^ 0    -- result is 1 (any number to the power of 0 is 1)

Try it yourself

-- Create variables for the garden plot calculation
local sideLength = 7

-- TODO: Write your code here to calculate the area using exponentiation

-- Print the calculated area
print(area)
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals