# Difference between += and .+=

**URL:** <https://discourse.julialang.org/t/difference-between-and/88289>\
**Category:** New to Julia\
**Created:** [October 5, 2022, 1:22pm UTC](https://discourse.julialang.org/t/difference-between-and/88289 "2022-10-05T13:22:11Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Fourier](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fourier/32/38176_2.png) [@Fourier](https://discourse.julialang.org/u/Fourier)\
**Post date:** [October 5, 2022, 1:22pm UTC](https://discourse.julialang.org/t/difference-between-and/88289/1 "2022-10-05T13:22:11Z")

</div>

What is the difference between the `+=` and the `.+=` operator in julia when adding two arrays, eg. `x = [1,2,3]; y = [4,5,6]``x+=y` vs `x.+=y`?

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [October 5, 2022, 1:23pm UTC](https://discourse.julialang.org/t/difference-between-and/88289/2 "2022-10-05T13:23:06Z")

</div>

`.` make it element wise

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [October 5, 2022, 1:41pm UTC](https://discourse.julialang.org/t/difference-between-and/88289/3 "2022-10-05T13:41:04Z")

</div>

Specifically, `x += y` is `x = x + y` which will allocate a new vector for `x+y` and assign that to `x`. `x .+= y` is `x .= x .+ y` which won’t allocate a vector, and will modify `x`.

---

<div class="post-metadata">

**Author:** ![lrnv](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lrnv/32/19373_2.png) [@lrnv](https://discourse.julialang.org/u/lrnv)\
**Post date:** [October 6, 2022, 8:40am UTC](https://discourse.julialang.org/t/difference-between-and/88289/4 "2022-10-06T08:40:23Z")

</div>

The answer you got are already true and describe the problem enough, I’ll just add something : You might encounter cases where the `+=` operator is not overloaded for the type you are considering, while the `.+=` statement basically does a loop and only requires the objects to be iterable (and their iterates to be summable, of course).

This is why I recommend using `.+=` when possible. There is also the benefit of loop fusion (see the docs). Anyway, the best way (the fastest and easiest for the compiler to understand) is always to write the loop yourself.

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [October 6, 2022, 8:58am UTC](https://discourse.julialang.org/t/difference-between-and/88289/5 "2022-10-06T08:58:33Z")

</div>

> [@lrnv](#):
>
> You might encounter cases where the `+=` operator is not overloaded for the type you are considering

You can not actually do this, `a += b` is just another way of typing `a = a + b`, not a standalone operator, as mentioned above.

---

<div class="post-metadata">

**Author:** ![lrnv](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lrnv/32/19373_2.png) [@lrnv](https://discourse.julialang.org/u/lrnv)\
**Post date:** [October 6, 2022, 9:28am UTC](https://discourse.julialang.org/t/difference-between-and/88289/6 "2022-10-06T09:28:28Z")

</div>

Yes, i meant that the `+` operator might not be overloaded, while the `.+` one is available to all iterables.
