TYPE


type( a )
type "variable type"
a any type of value

This function will return a string with the name of the type of variable that is provided as a.


For example:

--types of variables
str = "hello world"
num = 1
bool = true
tbl = {}
function funct() end

? type( str )    --prints: string
? type( num )    --prints: number
? type( bool )   --prints: boolean
? type( tbl )    --prints: table
? type( funct )  --prints: function

( The ? is the shorthand for the print() function. )


You may want to ensure that a variable being passed to a function is the correct type, and if it is not, then you would want to convert it to the correct type. For example:

function double_it( a )
	if type(a) ~= "number" then
		a = tonum(a)
	end
	
	return a + a
end

( The ~= is the not equal operator. )



Images in this Guide by NerdyTeachers is licensed under CC BY-SA 4.0

684

14 May 2023

Font