Whats the reasoning behind why this practice works?

I recently came across this really handy practice for toggling between two values, however I don’t fully understand which quirk of Luau’s allows me to do something like this:

function ToggleText(bool:boolean)
	TextLabel.Text = bool and "OFF" or "ON"
end

Where I would rather have to be doing this instead:

function ToggleText(bool:boolean)
	if bool then
		TextLabel.Text = 'ON'
	else
		TextLabel.Text = 'OFF'
end

Does someone know what is happening here?

1 Like

How logical operators work in Lua and operator precedence.

Note that you have the option of using Luau if-then-else expressions which kinda replace the old idiom.

1 Like

Wow thank you, that is really interesting.

So the Luau way of writing it would be like this?

function TextToggle(bool:boolean)
	TextLabel.Text = if bool then 'ON' else 'OFF'
end
2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.