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
EasyCalculate 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)This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Operators 2 Relational & Logic
Equality OperatorsRelational OperatorsThe 'and' OperatorThe 'or' OperatorThe 'not' OperatorShort-Circuit EvaluationTruthy and Falsy ValuesRecap - Simple Logic7Basic Conditional Logic
The if-then StatementThe if-then-else StatementThe elseif StatementRecap - Treasure Chest2Variables and Data Types
What is a Variable?NumbersStringsBooleansThe Value 'nil'The type() FunctionNaming ConventionsRecap - Character Profile5Basic Output
Printing LiteralsPrinting VariablesPrinting Multiple ValuesCombining Strings & VariablesThe tostring() FunctionInputCastRecap - Status ReportRecap - Till 1208String Manipulation Basics
string.len()string.upper & string.lowerstring.sub()string.rep()string.find()Recap - Format Username3Operators 1 Arithmetic & Conc
Arithmetic OperatorsModulo OperatorExponentiation OperatorString ConcatenationOperator PrecedenceRecap - Simple Calculations6Project: Character Stats Disp
Welcome MessageDeclare Character Stats9Functions Basics
Declaring a FunctionCalling a FunctionFunctions with ParametersFunctions with Multiple ParamsThe 'return' StatementRecap - Area Calculator