Using Generics in Java allows you to return a different type, while still retaining static type safety. You simply specify the type that you want to return in the generic type parameter of the function call.
Whether you could use a similar approach in Javascript is, of course, an open question. Since Javascript is a dynamically typed language, returning an object
seems like the obvious choice.
If you want to know where a dynamic return scenario might work when you are used to working in statically-typed language, consider looking at the dynamic
keyword in C#. Rob Conery was able to successfully write an Object-Relational Mapper in 400 lines of code using the dynamic
keyword.
Of course, all dynamic
really does is wrap an object
variable with some runtime type safety.