Back to Blog

Learn Lua in Neovim: Variables and Types — local, type() & Nil | Episode 2

Sandy LaneSandy Lane

Video: Learn Lua in Neovim: Variables and Types — local, type() & Nil | Episode 2 by Taught by Celeste AI - AI Coding Coach

Watch full page →

Learn Lua in Neovim: Variables and Types — local, type() & Nil

In this lesson, you'll learn how to declare variables in Lua using the local keyword and explore the four fundamental data types: string, number, boolean, and nil. You'll also see how to check a variable's type with the type() function and understand why uninitialized variables default to nil.

Code

-- Declare a local string variable
local name = "Alice"

-- Declare a local number variable
local age = 25

-- Declare a local boolean variable
local is_student = true

-- Declare a local variable with nil (means "no value")
local nickname = nil

-- Print variables with concatenation using ..
print("Name: " .. name)               -- Output: Name: Alice
print("Age: " .. age)                 -- Output: Age: 25
print("Is student? " .. tostring(is_student))  -- Output: Is student? true

-- Check the type of each variable
print("Type of name: " .. type(name))         -- string
print("Type of age: " .. type(age))           -- number
print("Type of is_student: " .. type(is_student)) -- boolean
print("Type of nickname: " .. type(nickname)) -- nil

-- Uninitialized local variables are nil by default
local uninitialized
print("Type of uninitialized: " .. type(uninitialized)) -- nil

Key Points

  • Use local to declare variables with limited scope inside functions or blocks.
  • Lua has four core data types shown here: string, number, boolean, and nil.
  • The type() function returns the data type of any value at runtime.
  • Variables that are declared but not initialized automatically hold the value nil.
  • The string concatenation operator .. joins strings and variables for printing.