NAnuradha
NAnuradha

Reputation: 29

do we need to include return types for javascript methods

I have a doubt with return types of Javascript methods. I have seen some static methods which doesn't contain any clue about return types but return some texts as strings. Please explain:

      <script>  
        class Test{  
          static display(){  //This doesn't include return type 
           return "static method is invoked"  
          }  
        }  
        document.writeln(Test.display());  
      </script> 

Upvotes: 1

Views: 48

Answers (1)

Jack Bashford
Jack Bashford

Reputation: 44125

No you don't - and I'm pretty sure it's impossible in pure JS. You can do it in TypeScript, and some other languages support it natively (Swift), but you don't (and can't IIRC) in JavaScript.

If you wish to use TypeScript it would look like:

static display(): string {...}

Upvotes: 3

Related Questions