What does something: number mean in parameters?

I always see people using something like this in functions:

function add(name: string, amount: number)
    -- something here
end

I’m confused on why there is a name: string there instead of just name. Does it do anything, or is it just for looks and easier to read?

Thanks in advanced.

1 Like

A string is simply text(characters). So in other words the name is text.

1 Like

Type checking. Basically, you force the code to assume that name is a string, and amount is a number, so autocomplete gives you the correct members for that variable.

1 Like

This is called type checking, basically it tells the code that the variable name is supposed to be a string instead of the default any(meaning any possible type). This helps to catch common non-runtime coding errors early on without having to run the code, however it won’t change the behaviour of the code itself.

So type checking doesn’t prevent any errors from occurring or change code behaviour. It just warns the programmer about said type checking errors by redlining the code within the script editor and in a way “annoying” them till they fix the issue.

Type checking is highly useful for organizing code, helping other programmers decode what you did and for even helping your future self when revisiting your code.

2 Likes

Just for looks. It helps with autocompletion aka intellisense.

1 Like

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