Set type for function parameters?

dmr picture dmr · Dec 6, 2011 · Viewed 171.3k times · Source

Is there a way to let a javascript function know that a certain parameter is of a certain type?

Being able to do something like this would be perfect:

function myFunction(Date myDate, String myString)
{
    //do stuff
}

Thank you!

Update: Being that the answer is a resounding "no," if I want myDate to be treated as a date (in order to call date functions on it), I have to cast it as a date inside the function or set a new variable of type Date to it?

Answer

pronvit picture pronvit · Dec 6, 2011

No, JavaScript is not a statically typed language. Sometimes you may need to manually check types of parameters in your function body.