# Slalom--what happened to the trusty var keyword?

**URL:** <https://discourse.codecombat.com/t/slalom-what-happened-to-the-trusty-var-keyword/4004>\
**Category:** Uncategorized\
**Created:** [May 22, 2015, 2:01pm UTC](https://discourse.codecombat.com/t/slalom-what-happened-to-the-trusty-var-keyword/4004 "2015-05-22T14:01:29Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![PixelAA](https://avatars.discourse-cdn.com/v4/letter/p/a4c791/32.png) [@PixelAA](https://discourse.codecombat.com/u/PixelAA)\
**Post date:** [May 22, 2015, 2:01pm UTC](https://discourse.codecombat.com/t/slalom-what-happened-to-the-trusty-var-keyword/4004/1 "2015-05-22T14:01:29Z")

</div>

Hi all, just a curious question (code passed fine).

In part of the suggested code when entering the Slalom level I see that the **var** keyword is not used to define the first gem for the first time. Even though the code worked fine, I was wondering why **var** was not used, is this because it’s within the scope of a **while** loop? Or the **var** keyword is just to make code easier to read and JS doesn’t care either way?

```python
gems = this.findItems();

while (this.pos.x < 20) {
	// move() takes objects with x and y properties, not just numbers.
	this.move({'x': 20, 'y': 35});
}

while (this.pos.x < 25) {
	// A gem's position is an object with x and y properties.
	gem0 = gems[0];
	this.move(gem0.pos);
}

```

Thanks for any insight!

---

<div class="post-metadata">

**Author:** ![trotod](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/trotod/32/3263_2.png) [@trotod](https://discourse.codecombat.com/u/trotod)\
**Post date:** [May 23, 2015, 12:08am UTC](https://discourse.codecombat.com/t/slalom-what-happened-to-the-trusty-var-keyword/4004/2 "2015-05-23T00:08:43Z")

</div>

It’s probably because the level designer forgot or decided not to use the `var` keyword.

Anyways, according to [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Grammar_and_types#Declaring_variables):

> You can declare a variable in three ways:
> 
> - With the keyword `var`. For example, `var x = 42`. This syntax can be used to declare both local and global variables.
> - By simply assigning it a value. For example, `x = 42`. This always declares a global variable and cannot be changed at the local level. It generates a strict JavaScript warning. You shouldn’t use this variant.
> - With the keyword `let`. For example, `let y = 13`. This syntax can be used to declare a block scope local variable. [Note: `let` doesn’t yet work in CodeCombat, I think.]

The main point is that declaring a variable without the `var` keyword in JS is discouraged.

If points 1 and 2 are unclear, here’s an example:

```js
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/var
function x() {
  y = 1; // global, can be seen throughout the code
  var z = 2; // local to the function, can only be seen inside x()
}

x();

console.log(y); // logs "1" 
console.log(z); // Throws a ReferenceError: z is not defined outside x

```

---

<div class="post-metadata">

**Author:** ![Feinty](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/feinty/32/1869_2.png) [@Feinty](https://discourse.codecombat.com/u/Feinty)\
**Post date:** [May 23, 2015, 3:14am UTC](https://discourse.codecombat.com/t/slalom-what-happened-to-the-trusty-var-keyword/4004/3 "2015-05-23T03:14:03Z")

</div>

defining a variable without using var basically declares it one level above

---

<div class="post-metadata">

**Author:** ![codeholic](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/codeholic/32/2618_2.png) [@codeholic](https://discourse.codecombat.com/u/codeholic)\
**Post date:** [May 23, 2015, 6:56am UTC](https://discourse.codecombat.com/t/slalom-what-happened-to-the-trusty-var-keyword/4004/4 "2015-05-23T06:56:56Z")

</div>

Not exactly. See [http://stackoverflow.com/questions/1470488/what-is-the-function-of-the-var-keyword-and-when-to-use-it-or-omit-it#answer-1470494](http://stackoverflow.com/questions/1470488/what-is-the-function-of-the-var-keyword-and-when-to-use-it-or-omit-it#answer-1470494)
