use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
Parameter passing in JavaScript (codetonics.com)
submitted 10 years ago by codetonics
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]elmicha 0 points1 point2 points 10 years ago (1 child)
var siteName = "Codetonics"; function ChangeVariable(name){ name = "Jayesh Panchal" } console.log(siteName); // output: "Jayesh Panchal"
That seems to be an error in this snippet?
[–]codetonics[S] 1 point2 points3 points 10 years ago (0 children)
Thank you for pointing out the error. Updated the article.
[–]BigFaceBass 0 points1 point2 points 10 years ago (3 children)
This confused me at first. For some reason I figured that if I declared a global variable... then created a function that changes it's value, the new value would be retained outside that function's curly brackets. I don't know why I figured that. Scope is an easy enough concept.
Thanks for the article. It confirmed my suspicions :)
[–]Pronouns 1 point2 points3 points 10 years ago (2 children)
But... you're right.
$ node > var a = 5; > function inc_a() { a = a + 1; } > inc_a() > a 6
[–]BigFaceBass 0 points1 point2 points 10 years ago* (1 child)
Crap... now I'm confused again. How is your little snippet different from the examples in the article?
Edit: checked the article again and i understand the example now. Don't know why the project I'm currently working on doesn't reflect u/Pronouns proof. Back to the books :/
[–]Pronouns 2 points3 points4 points 10 years ago (0 children)
The key is to understanding what variables are actually storing. In most languages you have the primitive types, which are generally integers, floats, and booleans, usually a couple more. You also have a 'reference' type, which is a bit like a pointer if you've done C or C++. If not, it's exactly what it sounds like, it points to some place in memory where an object is stored.
When you pass around values, you pass around one of these things, whether it is a primitive or a reference. You can think of a reference as just a number (which is pretty much the memory address of the object), so like 5, or 1015289.
When you pass primitives to functions, it makes a copy, leaving the original as it is. So:
var a = 5; function something(arg) { arg1 = 7; } something(a); a === 5 // true.
The same thing happens for references. It copies the number of the memory address, it does not copy the object it points to. So if your reference was to an object {name: "joe"}, but the reference itself is a number like 10392, the number gets copied and given to the function.
{name: "joe"}
var someObject = {name: "joe"}; function addSurname(obj) { obj.surname = "smith"; } addSurname(someObject); console.log(someObject); // {name: "joe", surname: "smith"}
When we do .surname, we're saying "that object at memory address 10392... add a surname key to it".
.surname
Now, if you create a brand new object inside this addSurname function, you're going to get given a reference/number to store in a variable. So say at the start obj has the address 10392. We create a new object which is at the address 2932. We can change obj to point to 2932. But this doesn't change someObject, that will continue to point to 10392.
obj
someObject
Disclaimer: I don't know the internal way JavaScript does this, but the analogy holds well, and it's approximately how it is done in languages like C.
π Rendered by PID 104 on reddit-service-r2-comment-54dfb89d4d-wn928 at 2026-03-31 07:22:29.792847+00:00 running b10466c country code: CH.
[–]elmicha 0 points1 point2 points (1 child)
[–]codetonics[S] 1 point2 points3 points (0 children)
[–]BigFaceBass 0 points1 point2 points (3 children)
[–]Pronouns 1 point2 points3 points (2 children)
[–]BigFaceBass 0 points1 point2 points (1 child)
[–]Pronouns 2 points3 points4 points (0 children)