is there any keyword like const or anything else which does the same job with it in lua?

Is there a const keyword in lua ? Or any other similar thing? Because i want to define my variables as const and prevent change of the value of the variables. Thanks in advance.


  • Update: November 2023

    Following the release of Lua 5.4 in June 2020, support for constant variables has been added, refer to this answer by Ainar-G for more more information.

    Original Answer

    Lua does not support constants automatically, but you can add that functionality. For example by putting your constants in a table, and making the table read-only using metatable.

    Here is how to do it:

    The complication is that the names of your constants will not be merely "A" and "B", but something like "CONSTANTS.A" and "CONSTANTS.B". You can decide to put all your constants in one table, or to group them logically into multiple tables; for example "MATH.E" and "MATH.PI" for mathematical constants, etc.