Welcome to Booleans in Lua

Now that you have explored the main data types in Lua, including strings and numbers, let’s focus on one of the simplest yet most powerful types: the boolean. In the previous lesson, you saw that booleans can hold only two values — true or false. These are perfect for answering yes/no questions or tracking the status of something in your program.

What You'll Learn

In this lesson, you will learn how to use booleans to represent real-world information in your Lua programs. For example, you might want to keep track of whether you have visited certain destinations on your travel list. Here’s a quick look at how you can do this:

In this code, we use boolean variables (has_visited_A and has_visited_B) to store whether a traveler has visited Paris or Tokyo. The print statements then display this information clearly. Notice how we use tostring() to convert the boolean value to text so it can be printed—this is necessary because the concatenation operator (..) in Lua expects strings or numbers, so booleans (and nil) must be converted to strings to avoid type errors.

Expected output:

Why Booleans Matter

Booleans are essential for making decisions in your code. They help you answer questions like: Has a task been completed? Is a user logged in? Should a message be shown? By mastering booleans, you will be able to control the flow of your programs and make them respond to different situations.

Ready to see how booleans can make your Lua programs smarter and more interactive? Let’s move on to the practice section and try it out for yourself!

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal