<p>Generally speaking, no. They are just two ways of saying the same thing.</p>

<p>Dot syntax is necessary when you are referring to a <strong>method</strong> within an object. However, if you are only trying to access the <strong>property</strong> of an object (or a value within an array), either technique will work.</p>

CC BY-SA 3.0
2014-06-14T17:35:30.257Z
2014-06-14T17:35:30.257Z
Lindsey D
<p>They're both the same for the most part.</p>

<p>The only time I'd use the second way you listed is when the property is a variable.</p>

<p>For instance:</p>

<pre><code>{% set foo = 'name' %}
{{ variable[foo] }}
</code></pre>

<p>This will give you <code>{{ variable.name }}</code></p>

CC BY-SA 3.0
2014-06-14T17:37:54.743Z
Yes, using property as a variable is definitely important thing! +1
nicael
CC BY-SA 3.0
2014-06-14T17:36:20.490Z
2014-06-14T17:41:45.213Z
2014-06-14T17:41:45.213Z
Bill Criswell
<p>When to use</p>

<pre><code>{{ variable.property }}
</code></pre>

<p>and when to use</p>

<pre><code>{{ variable['property'] }}
</code></pre>

<p>Is there a difference between these methods of accessing attributes of a variable or advantages of using one of them?</p>

<variable><difference><attribute>
Difference between using subscript syntax and "dot" syntax when accessing attributes of a variable or advantages of usage one of them
# Difference between using subscript syntax and "dot" syntax when accessing attributes of a variable or advantages of usage one of them

(Asked by: username_0 on 2014-06-14)

When to use
{{ variable.property }}

and when to use
{{ variable['property'] }}

Is there a difference between these methods of accessing attributes of a variable or advantages of using one of them?

**Tags:** variable, difference, attribute


---

## 2 answers

### Answer by username_2 on 2014-06-14. Score: 7 | Accepted answer

They're both the same for the most part.
The only time I'd use the second way you listed is when the property is a variable.
For instance:
{% set foo = 'name' %}
{{ variable[foo] }}

This will give you {{ variable.name }}

> **Comments:**
> **username_0**: Yes, using property as a variable is definitely important thing! +1  

---

### Answer by username_1 on 2014-06-14. Score: 4

Generally speaking, no. They are just two ways of saying the same thing.

Dot syntax is necessary when you are referring to a **method** within an object. However, if you are only trying to access the **property** of an object (or a value within an array), either technique will work.


---

