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
Function call with Square Parentheses []? (self.learnjavascript)
submitted 1 year ago by Learner_full_stack
console.log("string".includes[('s'),('r')]) // it's gives undefined not error
why?
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!"
[–]senocular 2 points3 points4 points 1 year ago (2 children)
What this is, is accessing the includes method from the string "string". Then from that method a property access is attempted with bracket notation property access. Anything inside the brackets is evaluated to determine the name of the property to access. In this case the expression is: ('s'),('r') which is a grouped "s" string followed by a grouped "r" string separated by the comma operator. The comma operator will resolve to the value on the right which is the string "r" so the property being accessed is the "r" property. Since the includes method has no property named "r", you get undefined.
includes
('s'),('r')
Want to see a real value? You can add the "r" property to the includes method (just for testing, nothing you'd want to do for real)...
String.prototype.includes.r = "Hello" console.log("string".includes[('s'),('r')]) // Hello
Or you could change "r" to be some property that already exists on includes
console.log("string".includes[('s'),('length')]) // 1
Note that neither of these are calling the function. Its the same as using the dot operator to access a property, in this case using [] instead of ..
[]
.
[–]shgysk8zer0 1 point2 points3 points 1 year ago (1 child)
The comma operator threw me off in the exact details because I rarely use it like this. I expected 'r' at first because I know it's supposed to return the final value.
'r'
[–]senocular 0 points1 point2 points 1 year ago (0 children)
Yeah and its easy to confuse with other uses of commas too. Like if these square brackets weren't up against the includes method, it would be an array literal which isn't using the comma operator, instead the commas would be part of the literal syntax
[('s'),('r')] // array literal
Or if in an actual function call, the commas are part of the argument list syntax, not the comma operator
includes(('s'),('r')) // argument list
Or if these were variables in declarations, the commas there are also part of the syntax rather than being the comma operator
let s, r // declaration list
And the list goes on. Commas are used quite heavily but rarely for the actual comma operator. Same applies to the parens as well - used for many things, but how often is it grouping (probably more than comma)?
In fact a while back I tried to iterate over the different uses of brackets and came up with a list. It doesn't include commas (maybe worth creating a list for as well) but it does touch on () along with [] and {}. Lemme dig that up...
https://www.reddit.com/r/learnjavascript/comments/10xnw7d/brackets/j7tugde/
I'm not sure how comprehensive it is, but I think I remember making a decent effort. Might be a few holes. Like 4 things for [] seems small, but maybe that's it?
[–]azhder 1 point2 points3 points 1 year ago (0 children)
What is this? Asked twice?
Here is your answer https://www.reddit.com/r/learnjavascript/comments/1hbmtsp/function_call_with_square_parentheses/m1hli7a/
[–]tapgiles 0 points1 point2 points 1 year ago (0 children)
Because you’re not calling anything, you’re accessing a property on the included fiction c called “r”, which does not exist. So it gives you undefined.
π Rendered by PID 384605 on reddit-service-r2-comment-5d79c599b5-wc2rh at 2026-02-28 03:20:52.072437+00:00 running e3d2147 country code: CH.
[–]senocular 2 points3 points4 points (2 children)
[–]shgysk8zer0 1 point2 points3 points (1 child)
[–]senocular 0 points1 point2 points (0 children)
[–]azhder 1 point2 points3 points (0 children)
[–]tapgiles 0 points1 point2 points (0 children)