Comment in Lua

Get FREE domain for 1st year and build your brand new site

Free book on Graph Algorithms

In this article, we have explained how to comment a single line or multi-lines in Lua Programming Language. Comments are lines of code that are not compiled and executed. Comments are used to keep documentation within code and sections of reference code within a working code.

Table of contents:

  • Single Line Comment in Lua
  • Multi-line Comment in Lua
  • Concluding Note

Comment in Lua

In short, comment in Lua Programming Language is as follows:

-- This is a comment in Lua

--[[
This is a multi line comment in Lua
Line 1
Line 2
--]]

Single Line Comment in Lua

Single Line comment is a line of code that has no newline character and is not processed when the code is compiled and executed. Every production level Programming Language has the support of Single Line Comment.

In Lua, to comment a single line, the characters -- is used. Example:

-- Comment in Lua (opengenus)

Consider the following Lua code:

-- Comment in Lua (opengenus)
x = 2
x = x * 10
print(x)

In this, we have added a comment at the beginning of the code. The program compiles correctly. Output:

20

If we comment the line "x = x * 10", then the answer should be just 2. Consider the following code snippet:

-- Comment in Lua
x = 2
-- x = x * 10
print(x)

Output:

2

Multi-line Comment in Lua

Multi-Line comment is a set of lines of code that is not processed when the code is compiled and executed. Every production level Programming Language has the support of Multi-Line Comment.

Consider the following code:

-- Comment in Lua
x = 2
-- x = x + 3
-- x = x * 10
print(x)

Output:

2

The above code has 3 single line comments and two lines are contiguous. The 2 lines can be commented as multiple line comments. This is the modified code with multi-line comments:

-- Comment in Lua
x = 2
--[[ x = x + 3
 x = x * 10 --]]
print(x)

Output:

2

Concluding Note

Lua, like every other Programming Language, has the support of Single and Multi-line comments. In this article at OpenGenus, we have demonstrated how to use comments in Lua.