corund
corund

Reputation: 177

Why type(nil)==nil is false?

I'm reading "Programming in Lua" book and I don't understand exercise 2.1:

What is the value of the expression

type(nil)==nil?

(You can use Lua to check your answer.) Can you explain this result?"

When I execute this code I get "false" as result. I could not explain this result, from my point of view correct result should be "true". I tried

type(some_undeclared_variable)==nil 

and it also gives me "false".

Upvotes: 8

Views: 1910

Answers (1)

Yu Hao
Yu Hao

Reputation: 122383

The function type() always returns a string, the value of type(nil) is the string "nil", which is not the same as nil, they have different type.

Upvotes: 11

Related Questions