Or operator: Difference between revisions
>JulienDethurens No edit summary |
>JulienDethurens No edit summary |
(No difference)
|
Latest revision as of 22:51, 29 January 2012
The 'or' operator operates on two values. If the first value is neither false nor nil, the 'or' operator returns the first value. If the first value is false or nil, then it will return the second value, regardless of what it is.
If Statement
The 'or' operator comes in handy when you want to check if one of the listed values is a certain value.
Choice of Value
The 'or' operator can also be used to choose an existent value of a value that is nil or false. Here are some examples:
This printed '1' because x doesn't exist and is therefore nil. So the or operator allowed us to choose 1 over nil.
This also printed '1' because although x exists, it is false. If x had been true, then y would be true, because the 'or' operator would choose x, as it is neither false, neither nil.