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.

jpo
  • 3,959
  • 20
  • 59
  • 102
  • It's always good to start learning new language with [some tutorials](https://developer.mozilla.org/en-US/docs/JavaScript/Reference). – VisioN Jan 08 '13 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 – jpo Jan 08 '13 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. – Shmiddty Jan 08 '13 at 19:36
  • 1
    We'll need to see more code. – Shmiddty Jan 08 '13 at 19:39

2 Answers2

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!]

VisioN
  • 143,310
  • 32
  • 282
  • 281
David G
  • 94,763
  • 41
  • 167
  • 253
1

This should work

var y = "this is a string", x = "another";
javascriptFunction(y, x);
bedane
  • 1,129
  • 11
  • 13