0

Am not sure if functions in javascript are what we call methods in other programming languages.Methods in other programming languages can have their result specified just after the access-specifier like in C# for example i would do like

//method to return int
private int myinteger(){
     int a=0;
    return  a;
}
//method to return string
private string mystring(){
   string b="myreturn";
   return b;
}

I just don't know how to do that with javascript functions, you think you can help me with a sample?Thank You very much :)

4
  • 2
    please lookup the difference between statically typed (like c#) and dynamically typed languages (like javascript) Commented Mar 25, 2021 at 7:09
  • 2
    Long-story short: there is no such thing in Javascript. If you desire type safety, i strongly recommend to use Typescript instead. Just beware that this comes with some complexity in setup - depending on your use case. Commented Mar 25, 2021 at 7:10
  • Will sure do that, i just want to learn, in the mean time can you help me with code to specify the result? Commented Mar 25, 2021 at 7:11
  • Okay thats all thanks Commented Mar 25, 2021 at 7:11

3 Answers 3

1

You don't need to provide the data types in javascript. The functions are pretty similar you just have to start it with the function keyword.

Also, we need to start the variables with const or let.

I use the console.log(myinteger()); below to log the value of the myinteger() function in the browser console. (Similar to c++'s cout)

//method to return int
function myinteger() {
  const a = 0;
  return a;
}
//method to return string
function mystring() {
  const b = "myreturn";
  return b;
}

console.log(myinteger());
console.log(mystring());

If you are someone who wants to use javascript but still want to assign the data type and many more thing then you can use TypeScript By Microsoft.

Sign up to request clarification or add additional context in comments.

Comments

0

You cannot do this with javascript, but you still have two workarounds available:

  1. Use eslint and use the https://eslint.org/docs/rules/consistent-return rule
  2. Use typescript

Comments

0

Javascript has types for values (not variables)

so you can define a variable as

var name = "Hamond";

and to know it's type you have to use typeof

typeof name; // "string"

Side note: you can use let or const instead of var but let that be for another time.

so variables in javascript doesn't have types, values have. You can add static typing using typescript so

var name: string = "Hamond";

and at dev time if you wanted to edit name and incorrectly deal with it as a non string type you will get an error warning you immediately

name = 3; // error
name - 4; // error
// and so forth because `name` is of `string` type

so this type check is done at author or dev time and you don't have to wait until run time to get the error.


Why all the talk about variables and values?

Because Javascript function can return any value(even returning a variable is basically returning its value if it's scalar value or its reference if it's an object type)

so defining a function look like:

function doSomething(){

    return 33;
}

notes:

  • no return type
  • can have no return statement(by default will return undefined)

with typescript

function doSomething(): number{

    return 33;
}

typing problems solved at dev/write time

About function vs method: I think developers in many times use these terms interchangeably, but in javascript we just have function, even a function defined inside a class in javascript is just a function. People like the name method when its defined inside some class.

references:

JS syntax

The Nature Of Functions

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.