You have already learned how to use variables to store numbers and text, and how to combine them to create meaningful messages. Now, let’s take the next step and explore the different types of data you can work with in Lua. Understanding data types is a key part of programming because it helps you know what kind of information your variables are holding and how you can use them.
In this lesson, you will discover the main data types in Lua and how to identify them. Lua is a flexible language, and it supports several basic types, including:
- String: Text, like a city name or a message.
- Number: Any numeric value, such as the number of days for a trip.
- Boolean: A true or false value, often used for yes/no questions.
- Table: A collection of values (we’ll learn more about these soon).
- Nil: Represents the absence of a value.
Here’s a quick example to show how you might use these types in a travel-themed program:
In the code above, you can see how to create variables of different types and use the type() function to check what type each variable is.
Expected output:
Knowing about data types helps you write better, safer code. For example, you wouldn’t want to add a number to a string by mistake or try to use a value that doesn’t exist. By understanding the types of data you are working with, you can avoid errors and make your programs more reliable.
Data types are the building blocks of every program. Once you know how to use them, you’ll be able to store, check, and process all kinds of information — whether you’re planning a trip, building a game, or solving real-world problems.
Let’s dive in and see how you can use data types to make your Lua programs smarter and more powerful!
