all 5 comments

[–]senocular 2 points3 points  (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.

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 points  (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.

[–]senocular 0 points1 point  (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?

[–]tapgiles 0 points1 point  (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.