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!

4
  • 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). Commented Aug 27, 2019 at 2:27
  • @John edited it :) thanks a lot. And sorry for those unnecessary tags... Commented Aug 27, 2019 at 2: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. 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. Commented Aug 27, 2019 at 2: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 Commented Aug 27, 2019 at 2:39

2 Answers 2

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.

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

Comments

1

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

1 Comment

Thanks so much! This actually clarified it :D

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.