Metatables

hasmetatable

Returns whether the target value has a metatable.

Syntax

hasmetatable(target: any): boolean

Arguments

NameTypeDescription
targetanyValue to check for a metatable.

Returns

NameTypeDescription
hasMetatablebooleantrue when the target has a metatable; otherwise false.

Description

Returns whether the target value has a metatable. The function returns one boolean and does not return or change the metatable itself.

Call it with 1 parameter(s): target. The argument table explains which values are required and which ones only refine the behavior.

It returns hasMetatable (boolean). Use the returns table to separate successful values from nil results and recoverable errors.

Example

Check whether a value currently has a metatable.

local target = {}

print(hasmetatable(target))

setrawmetatable(target, {})

print(hasmetatable(target))