Does javascript assign by reference?

Is there any way I can be less verbose in JavaScript by pointing a local variable by to an objects property?

For instance in PHP I can do this:

$obj->subobject->property = 'Foo';
$property =& $obj->subobject->property;
$property =  'Bar';
echo $obj->subobject->property;
// output 'Bar'

It's not a very good example but you get the idea.

I want to copy this behaviour in Javascript. I'm quite often having to go quite deep into objects and it's getting quite annoying having to do:

if (please.stop.making.me[somevar].type.so.much.length) {
    please.stop.making.me[somevar].type.so.much[newSubObjectKey] = anObject;
}

// perform more operations on the object down here

It would be a lot easier to read and a lot easier to type:

var subObj = is.much.easier.to.type.once;
if (subObj.length) {
     subObj[newSubObjectKey] = anObject;
}

// now that's much better

I know I should really know this already, but I'm just advancing to "advanced novice" in JavaScript.

Does javascript assign by reference?

informatik01

15.8k10 gold badges73 silver badges102 bronze badges

asked Jul 18, 2011 at 12:02

3

In JavaScript, everything is passed by value, but the variable's type will determine whether it's a reference passed by value or not;

  • Objects are references
  • Primitives (numbers, strings etc) are passed by value.

In simple terms, if you pass a variable to a function that's an array, modifying it in the function will affect the parent.

However, passing it a value in the array will not. Naturally, there's absolutely nothing stopping you wrapping a primitive in an object to ensure it works like a "pointer".

Does javascript assign by reference?

informatik01

15.8k10 gold badges73 silver badges102 bronze badges

answered Jul 18, 2011 at 12:10

OliproOlipro

3,44918 silver badges24 bronze badges

2

You can assign a new variable to reference any depth in a chain of property keys, so long as the entry referred to isn't a primitive type.

This works because a bare object variable is actually a reference to that variable, so your new (shorter) variable can point to the same place.

However primitive number and string values are passed by value, so you can't create new references to those.

answered Jul 18, 2011 at 12:16

AlnitakAlnitak

328k70 gold badges400 silver badges485 bronze badges

1

It would be a lot easier to read and a lot easier to type:

var subObj = is.much.easier.to.type.once;
if (subObj.length) {
     subObj[newSubObjectKey] = anObject;
}

Have you even tried the above? Because it works.

As for the actual question. You cannot have references or pointers to values. Everything is passed by value in javascript (not reference).

Edit: I forgot to mention some values are references. You still can't get a pointer to those reference values.

answered Jul 18, 2011 at 12:06

RaynosRaynos

163k56 gold badges347 silver badges394 bronze badges

3

coffeeScript code

# result method
Object::r = (v) ->
    s=@
    st='global.'+v+'={'+'s'+'}'
    re=(eval st)
    str='global.'+v+'=re["s"]'
    eval(str)
    @


[1..10].r('a').
    map( (v) -> 'lorem_ipsum_'+v.toString() ).r('ar').
    join("__").r('s').
    split("__").reverse().r('arr')

console.log("\n")
console.log(a); console.log("\n")
console.log(ar); console.log("\n")
console.log(s); console.log("\n")
console.log(arr); console.log("\n")

screenShot

answered Feb 14, 2018 at 14:38

Not the answer you're looking for? Browse other questions tagged php javascript pointers or ask your own question.

Is JavaScript by reference or by value?

In JavaScript, all function arguments are always passed by value. It means that JavaScript copies the values of the variables into the function arguments. Any changes that you make to the arguments inside the function do not reflect the passing variables outside of the function.

Is JavaScript call by reference?

Function arguments are always passed by value. It copies the value of a variable passed in a function to a local variable. ... Javascript..

Are JavaScript objects by reference?

Objects in JavaScript are passed by reference. When more than one variable is set to store either an object , array or function , those variables will point to the same allocated space in the memory. Passed by reference.