Pass a var between functions JS

  • s15199d
  • Expert
  • Expert
  • User avatar
  • Posts: 524
  • Loc: NC, USA

Post 3+ Months Ago

I need to pass a variable from one javascript function to another. Any ideas?

I did do a search. No luck though. Kinda surprised to be honest!
  • Tannu4u
  • Proficient
  • Proficient
  • User avatar
  • Posts: 480
  • Loc: India

Post 3+ Months Ago

i think that i haved written it.A person has asked the same question i think that u have not searched the right post try it.Any way i will tell u..

<script>
function(argument)
{
//Now call the second function with this argument
function2(argument);
return true;
}

function2(arg)
{
//This is the variable from the first function u will get it
alert(arg);
}
</script>
  • s15199d
  • Expert
  • Expert
  • User avatar
  • Posts: 524
  • Loc: NC, USA

Post 3+ Months Ago

I was doing it right all along...I had something else bugged up. Once I fixed it my var passed fine. For me the easiest way is to make the var global. Which always works for me...but I had been told you shouldn't do that...which maybe you shouldn't...but IMO if it aint broke...don't touch it.
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13504
  • Loc: Florida

Post 3+ Months Ago

Global Variables, with theese in mind,

Quote:
Main Entry:communism
Function:noun
Etymology:French communisme, from commun common
Date:1840

1 a : a theory advocating elimination of private property b : a system in which goods are owned in common and are available to all as needed


:D
  • theak
  • Proficient
  • Proficient
  • User avatar
  • Posts: 438
  • Loc: Taipei, Taiwan

Post 3+ Months Ago

There's nothing wrong with communism, it's people that don't work.
  • Carnix
  • Guru
  • Guru
  • User avatar
  • Posts: 1098

Post 3+ Months Ago

Global variables require that the memory space allocated for those variables not be freed until a given application is unloaded completely. It's ok to have a few, but making all variables global will slow down your program. This applies to pretty much any language from C++ to JavaScript, although less sophisticated languages tend to blur the lines between global and local variables.

If you need to pass a variable from one function to the next, just make the function return the value. Functions should generally always return something anyway...

Code: [ Select ]

function part_one(arg1){
 arg1 += " there";
 return part_two(arg1);
}

function part_two(arg2){
 return arg2.length;
}

alert(part_one("hello"));
  1. function part_one(arg1){
  2.  arg1 += " there";
  3.  return part_two(arg1);
  4. }
  5. function part_two(arg2){
  6.  return arg2.length;
  7. }
  8. alert(part_one("hello"));



This will return the integer 11. (The string "hello there" is eleven character long). You pass the string "hello" to the first function, which then adds " there" to it, then passes that string to the second function, which returns it's length to the first function, which in turn retuns that number to the alert call.

if you add an alert below the one already there... you will see that arg1 and arg2 will give undefined errors... but if you put alery(arg1); inside the first function it will return the current value of arg1...

.c

Post Information

  • Total Posts in this topic: 6 posts
  • Users browsing this forum: No registered users and 55 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.