Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

lua
communitycreator

What is nil in Lua?

Dario Vincenzo Tarantini

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Introduction

In Lua, there is a single value type called nil. Lua uses this type as a non-value to represent the absence of a valid value. The main property of nil is that it is different from any other type in the language.

Usage

nil is assigned to every global variable we create by default. We can also set nil to a global variable to delete it.

Note: nil is also useful if you want to delete object instances and array elements.

Example

In this example, we use nil to delete a variable.

x = 1
print(x)
x = nil
print(x)
x = {
   zero = 0,
   one = 1
}
x.zero = nil
print(x.zero)

RELATED TAGS

lua
communitycreator

CONTRIBUTOR

Dario Vincenzo Tarantini

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring