function check_alert (pcheck) {
window.alert("You make $"+pcheck);
}
var paycheck = 1200;
check_alert(paycheck);
How can the function make an alert with the variable pcheck when the global variable is named paycheck, I'm confused ?
function check_alert (pcheck) {
window.alert("You make $"+pcheck);
}
var paycheck = 1200;
check_alert(paycheck);
How can the function make an alert with the variable pcheck when the global variable is named paycheck, I'm confused ?
Are you saying the code doesn't work, or that it works but you don't understand why?
You are passing by value to this function. So the value (1200) gets passed into the function, not the actual variable. The function does not perform any manipulations on the variable and, even if it did, the effects would not be visible outside the function.
All that is happening is the global variable value is being passed to the function, correct ?
The function check_alert has a parameter called (pcheck), it is a little confusing, but I'll get this figured out :)
function paper (log) {
window.alert("you like "+log);
}
var arnold = going;
paper(arnold);
Shouldn't this make a window box called "you like going" ?
You are missing quotes.
var arnold = "going";
It will display an alert message with the string of characters.
Ah, I rushed typing the code :(
The Log parameter acts like a proxy for the function.
How can the function make an alert with the variable pcheck when the global variable is named paycheck, I'm confused ?
Well, argument names of the function would be useless if they wouldn't be able to receive value from other pointers or variables.
It works because you've just passed the value of 'paycheck' varible to the 'pcheck' argument name which will now pass that *value to the *function using it.
It's the same as if you've used a straitforward input to the named argument of the function, like:check_alert(1200);
Troy could you explain that again ? :)
Troy could you explain that again ? :)
I just did! :)
function check_alert (**pcheck**) {
window.alert("You make $"+**pcheck**);
}
var **paycheck** = 1200;
check_alert(**paycheck**);
// alert val: "You make $1200"
you have passed the paycheck value to the function argument named pcheck which will now carry this (1200) value to your alert message as it aleready did.
p.s.: You are not passing 'names' but 'values' of variables. The variable name is just a handle to its value. 'paycheck' and 'pcheck' are now handling the same value.
In this case, 'paycheck' is indeed 'global' and can be acessed from inside a function. Try this:
var paycheck = 1200;
function check_alert () {
window.alert("You make $"+paycheck);
}
check_alert();
Think about it...
Now try this
var paycheck = 1200;
function check_alert () {
window.alert("You make $"+paycheck);
paycheck = paycheck - 10;
}
check_alert();
check_alert();
check_alert();
check_alert();
Ooops, someone is stealing from me!
To avoid the danger of a function corrupting the global variable, it's safer to use a different variable inside the function (pcheck in the original example) by passing it as an 'argument' which turns its value into a local variable within the function.
Hey thanks, I did more tests and I understand it better then I originally did, not completely, but better :)
nautical - what is this line 'paycheck = paycheck - 10;', a loop ?
It's there to demonstrate that if you use a variable defined outside the function instead of passing it in as a parameter, then your variable can be changed from inside the function. Many other languages would not be able to access the variable paycheck from inside the function. Compare with the following:
var paycheck = 1200;
function check_alert (pc) {
window.alert("You make $"+pc);
pc = pc - 10;
}
check_alert(paycheck);
check_alert(paycheck);
check_alert(paycheck);
check_alert(paycheck);
In this case pc = pc - 10 has no effect on the global variable so the alert keeps showing 1200.
I took a quick look for an article to help. Try this:
http://coding.smashingmagazine.com/2009/08/01/what-you-need-to-know-about-javascript-scope/
If the answer as to why you used pc = pc - 10 is on that page, you lost me, I couldn't find it.
Don't worry about it, read the article. In the last example, it was only there to show that changing pc inside the function has no effect on the value which is passed in the next time the function is used. Do you understand the answer to your original question now?
I understand my question. I also understand what you meant in your code, the only thing I don't get is the pc = pc -10 syntax, I won't fret over it, yet :)
OK, that syntax just says subtract 10 from pc.
Now I understand. In your first example when the function was being called it would subtract 10 from the global variable since the function was not calling any parameters.
I have to begin to take some of this knowledge and finish my site up, time is moving :(
he was off the topic for being busy rediscovering the tap water, as if we never heard of the existing difference between global and local variables, since like forever.
but by "pc = pc - 10;" he meant: "pc -= 10;"! completely off topic of course.
No I didn't
I meant what I said. It exposed that Siberian doesn't (yet) understand the assign operator vs algebra
He probably didn't, but his question was on a separate subject: how does or can the argument variable named pcheck carry or receive the value of a variable named paycheck when a given function is alerting the value by using pcheck keyword, whereas in the function argument we are inserting a variable with a different name, namely paycheck.
The answer is that JavaScript variables are not carriers or presentations of an object being passed by but simple tokens pointing or referring to a given object; and that paycheck token is simply passing its reference of an existing object (same object: number literal 12000 ) to another token which will be, now also pointing to that same object.
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.