# JavaScript - this vs self

**URL:** <https://discourse.codecombat.com/t/javascript-this-vs-self/3143>\
**Category:** Uncategorized\
**Created:** [March 18, 2015, 4:30pm UTC](https://discourse.codecombat.com/t/javascript-this-vs-self/3143 "2015-03-18T16:30:16Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![J\_F\_B\_M](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/j_f_b_m/32/2756_2.png) [@J\_F\_B\_M](https://discourse.codecombat.com/u/J_F_B_M)\
**Post date:** [March 19, 2015, 1:11am UTC](https://discourse.codecombat.com/t/javascript-this-vs-self/3143/2 "2015-03-19T01:11:46Z")

</div>

Short version:  
You never use `self` in JavaScript. This is simply not a thing in JS. `self` is Pythons equivalent of `this`.

* * *

Long version ([based on this post](http://discourse.codecombat.com/t/javascript-array-sort-method-problem/2624/6?u=j_f_b_m)):  
Should you come to the point of defining functions in JavaScript, you will soon find that `this` points to the functions context rather than the global context. A common trick is to assign (the global) `this` to another variable. As `self` is almost the same as `this`, it’s one of the most common names for this helper-variable. So unless you assign anything to `self` it is just nonexistent.

```python
var self = this;

function getGoldThis(){
    return this.gold; //this would fail, because the current context doesn't know this.gold
}

function getGoldSelf(){
    return self.gold; //this would work, as it uses the this.gold from the outer context
}

var thisEquals = (this.gold == getGoldThis()); //false
var selfEquals = (this.gold == getGoldSelf()); //true

```

---

_[View the full topic](https://discourse.codecombat.com/t/javascript-this-vs-self/3143)._
