0

How can I call a javascript function and pass some variables to be used as parameters for instance

Say the function javascriptFunction uses 2 parameter. I will usually call it as javascriptFunction("param1", "param2") (with single quotes around the strings). But now I want to pass it some variables.

string y = "this is a string"
string x = "another"
javascriptFunction(y, x)

I have tried javascriptFunction(@y, @x), javascriptFunction("@y", "@x") (with single and then double quotes around the strings) but this does not work

EDIT I am actually making the call through a view (cshtml file). So my variables are strings.

4
  • It's always good to start learning new language with some tutorials. Commented Jan 8, 2013 at 19:33
  • The issue is that I do not create or initialize my variables in the javascript file but an cshtml page that uses strings Commented Jan 8, 2013 at 19:36
  • Part of me wants to close this question for being so basic, but I know this is exactly the type of question that SO is made to answer. There must be a duplicate somewhere, but I can't find it. Commented Jan 8, 2013 at 19:36
  • 1
    We'll need to see more code. Commented Jan 8, 2013 at 19:39

2 Answers 2

2

JavaScript has a weak type system. There's no need to specify the types. All variables' types are determined by their values. They are created with the keyword var:

var y = "this is a string";
var x = "another";

[And don't forget to use semicolons!]

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

3 Comments

Semicolons are not necessary though.
Not to mention that you could use a comma after the first string (in place of a semi-colon) and omit the second var.
@VisioN why did you make it so small now??
1

This should work

var y = "this is a string", x = "another";
javascriptFunction(y, x);

Comments

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.