3

I wanna pass in some variables to my functions like int, string, and etc. I'm a C# developer and is very new in JS.

In C# we normally call a function with:

function computeGrade(int grade)
{
...
}

computeGrade(90);

I understand that js uses var that can either be a string, int, etc. However when I try:

function ComputeGrade(var grade)
{
...
}

it gives me an error that it failed to initialize the function.
Forgive me for being too naive with js. Thanks a ton!

Dran Dev
  • 529
  • 3
  • 20
  • In JavaScript, you don't specify the type of the parameter. Try this -> function ComputeGrade(grade) { ... }. This enables you to pass anything as the 'grade' parameter (strings, numbers, functions, etc). – thiag0 Aug 27 '19 at 02:27
  • @John edited it :) thanks a lot. And sorry for those unnecessary tags... – Dran Dev Aug 27 '19 at 02:32
  • Thank you for your edit :-) On a related topic, it might be of interest to you that as of ECMAScript6 you can use [let](https://stackoverflow.com/questions/762011/whats-the-difference-between-using-let-and-var). There is a slight difference between `var` and `let`, but `let` behaves more like C# variables do with regards to scoping. Personally, I'd argue that `let` is a better way to go these days. – ProgrammingLlama Aug 27 '19 at 02:34
  • @John I've heard `let` too but was quite unsure on whether what's the main difference between the two. Thanks for the heads up! :) I'll try utilizing `let` – Dran Dev Aug 27 '19 at 02:39

2 Answers2

3

In js variables are just untyped name bindings. So you don't need to tell what a variable is capable of storing. Actually It just refers to any type of value.

function ComputeGrade(grade)
{
...
}  

So here, grade is just a name binding. It does not matter what kind of value you pass this function. It will bind to grade variable.

himank
  • 439
  • 3
  • 5
1

Use var for declaring local variables. For function arguments just use the param name without var.

karmakaze
  • 34,689
  • 1
  • 30
  • 32