gazman99
gazman99

Reputation: 41

Javascript 101 - typecast variables passed into function

Hopefully a basic question, but is it all possible to typecast varibles when you define a function in JS? this would be useful when constructing a library for use by other developers.

functionName:function(var1:string, var2:int)

instead of

functionName:function(var1, var2)

Upvotes: 1

Views: 53

Answers (1)

Philipp
Philipp

Reputation: 69663

No, this is not possible in normal Javascript. Javascript is a completely dynamically-typed language. Types of variables are only known at runtime. So all you can do is throw an exception when you get a wrong type.

There are, however, Javascript dialects like TypeScript or Dart which do have types and compile to normal Javascript.

Upvotes: 2

Related Questions