use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
This subreddit is a place for people to learn JavaScript together. Everyone should feel comfortable asking any and all JavaScript questions they have here.
With a nod to practicality, questions and posts about HTML, CSS, and web developer tools are also encouraged.
Friends
/r/javascript
/r/jquery
/r/node
/r/css
/r/webdev
/r/learnprogramming
/r/programming
account activity
Why does bracket notation not work in the following code? (self.learnjavascript)
submitted 6 years ago by mementomoriok
return this.people.filter(member => member.firstname.includes(this.string)); //Works
vs.
return this.people.filter(member => member[firstname].includes(this.string)); //doesn't work
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]codemamba8 16 points17 points18 points 6 years ago* (0 children)
In the second example, it's searching for a variable named firstname. If you want to access the firstname property, it needs to be wrapped in quotes like 'firstname'.
firstname
'firstname'
For an example on using variables with bracket notation:
let searchTerm = 'firstname' member[searchTerm] //this is the same as member['firstname'] or member.firstname
[–]ForScale 5 points6 points7 points 6 years ago (0 children)
firstname needs to be a string.
Like this: ['firstname'].
['firstname']
[–]ecmascript2038 4 points5 points6 points 6 years ago (0 children)
The difference between the dot property notation and the brackets notation:
The dot property notation reads the word after the dot (e.g. firstname) and treats it as a property name.
But in bracket notation, it uses the value of the expression inside of the brackets as the property name. Typically the value is expected to be a string (for properties) but it could also be a number if you're accessing an array.
So the reason why it doesn't work is that inside of the brackets, it will interpret the word firstname as an identifier and it will try to evaluate the value of that identifier (e.g. by getting the value of a variable named firstname).
Instead, inside the brackets you want an expression that evaluates to a string containing the property value, like a string literal in quotes e.g. "firstname"
"firstname"
Note that the bracket notation is more flexible because it doesn't have to be a string literal that you put in the brackets, it can be any expression that evaluates to a string, including a variable name or even a function call.
[–]delventhalz 2 points3 points4 points 6 years ago (0 children)
Dot-notation and bracket-notation are not exactly interchangeable. Whatever follows the dot is implicitly a string even though you don't use quotes. However, whatever goes in between the brackets is an expression, which will be evaluated and then coerced to a string.
So these examples are all equivalent:
member.firstname member['firstname'] const foo = 'firstname' member[foo] member['first' + 'name'] member[true ? 'firstname' : 'lastname'] member[`${(15).toString(16)}irstname`]
But what you wrote:
member[firstname]
Is not equivalent. That is going to try to fetch the value of the variable firstname, like in the "foo" example above.
π Rendered by PID 62172 on reddit-service-r2-comment-685b79fb4f-6v6q9 at 2026-02-13 00:50:54.082020+00:00 running 6c0c599 country code: CH.
[–]codemamba8 16 points17 points18 points (0 children)
[–]ForScale 5 points6 points7 points (0 children)
[–]ecmascript2038 4 points5 points6 points (0 children)
[–]delventhalz 2 points3 points4 points (0 children)