User:NecroBumpist/Tutorials/Ternary Operator
From Legacy Roblox Wiki
Jump to navigationJump to search
Ternary operators are a quick and useful way to conditionally controll expressions without creating an entire if statement.
Expressions
Expressions are pieces of code that evaluate to one final value.
local x = 5 + 3 -- '5 + 3' is an expression
Basic Usage
It's easiest to show the usage of ternary operators by example.
NOTE: nil will evaluate to false logically in ternary operators, but will be returned as nil for future use.
CONT. Other values like numbers, strings, tables, etc will evaluate true logically, but will be returned as their original value for future use.
print(true and 3); --> 3 (because the 1st value was true, it returns the 2nd) print(false and 3); --> false (because the 1st value was false, it is returned) print(true or 'cool'); --> true (because the 1st value was true, it is returned) print(false or 'cool'); --> 'cool' (because the 1st value was false, the second is returned)
Practical Uses
Standard Function Inputs
function test(input) input = input or 'Default'; print(input); end test(); --> 'Default' test('Not Default'); --> 'Not Default'