Purpose of the | symbol

Hi, what’s the purpose of the | symbol in Luau? I’ve seen it be used before as a separator between the class of a function’s variables, but I’d like to know what advantages doing that has and what other uses the | symbol may have.

Example of how I’ve seen it used in the past:

local function test(v1:number | string)
 print(v1)
end

Additionally, I have tried searching for this, however Google (and the dev forum) don’t seem to actually read the | symbol when searching. Either that or nobody has ever asked this before.

2 Likes

‘|’ defines multiple accepted types for a parameter. In your example, v1 can be either the number type or the string type. You can think of it as an “or” but for parameter types.

2 Likes

They’re called unions if you want to dig further! There’s also intersections (&), which are the natural counterpart Syntax - Luau

4 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.