# Global and local variables
- It is not allowed to create new global variables, nor to modify _ENV, _G global variables. Only new local variables are allowed. The syntax is similar to local a = 123; local a, b = "name",'age'. If you create a local function, it can be similar to local function abc() return "abc"; end
- Local variable declarations can also declare variable types, such as local a: string = "hello"
- The value of the local variable needs to be consistent with the compile-time type declaration of the local variable at compile time, such as local a = 1; a = "hi" This will report an error at compile time.
- For security reasons, each function in the smart contract cannot exceed 128 local variables, and the symbol length of each local variable cannot exceed 128 characters (function parameters, including self, are also counted in the number of local variables)
- The symbols of variable names, parameter names, and event names do not allow the use of keyword names. Pay special attention to keywords such as do/end/then that are easily misused as variable names/parameter names/event names
- Local variables can be declared with the three keywords local, var, let
- The var keyword is equivalent to the local keyword, both are used to declare variable local variables, which can be used and modified in the following visible scope code
- The let keyword is used to declare an immutable local variable. The declared local variable can only be initialized and cannot be modified, but the immutable is just the variable itself. If the value pointed to by the variable is a table or record type, the content of the table can still be changed.
such as:
var a = 'hello'
local b = 'hello'
let c = 'hello'
var d: object = 'hello'
a = 'glua' -- Correct
a = 123 -- Error, type a cannot be changed
b = 'glua' -- Correct
b = 123 -- Error, type b cannot be changed
c = 'glua' -- Error
d = 123 -- Correct, the object type of the d variable compile-time type can be assigned with a numeric value