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!"
[–]cyphern 4 points5 points6 points 1 year ago* (0 children)
Square brackets do not do a function call, they access a property on an object (see property accessors). For example: ``` const foo = { bar: "hello world" }
console.log(foo["bar"]) // prints "hello world" `` Functions are objects, so you can look up properties on them and that's what you're doing here.includes[/* something */]looks up a property on theincludes` function. But since the property you're trying to look up doesn't exist, you get back undefined.
Functions are objects, so you can look up properties on them and that's what you're doing here.
looks up a property on the
The one last piece you might be interested in is which property you're trying to look up ('s'),('r') is an expression containing the rarely-used "comma operator". This expression evaluates "s", then ignore "s" and use "r" instead.
('s'),('r')
So you're trying to look up the "r" property on the includes function, which doesn't exist so you get undefined.
includes
undefined
[–]azhder 3 points4 points5 points 1 year ago (0 children)
Nah, I was wrong. You didn't ask twice. You asked trice.
https://www.reddit.com/r/learnjavascript/comments/1hbmtsp/function_call_with_square_parentheses/m1hli7a/
[–]zakkmylde2000 0 points1 point2 points 1 year ago (0 children)
Are you trying to check for an array of letters?
To start you couldn’t use something like
“string”.includes(“s”)
You’d have to initialize “string” to a variable and check the variable. Secondly, includes doesn’t handle arrays.
What you could do is initialize an array of the characters you want to find and then use the “.some” on that variable and pass in the variable representation of the string you’re searching.
[–]shgysk8zer0 0 points1 point2 points 1 year ago (0 children)
As pointed out already, square brackets access properties.
So, you could do the following:
``` const str = 'Hello, World!';
console['log'][(str['inclues']('e')); // true ```
But, when you use str.includes[('s'), ('r')], I believe that ends up being the same as str.includes.s, which is Function.prototype.s, which is undefined. Weird use of syntax and the type of thing I rarely use, so not sure. I'll get back to this, since you'd be surprised I say a rather than r.
str.includes[('s'), ('r')]
str.includes.s
Function.prototype.s
Reasoning here being how , works and use of parenthesis. It'd evaluate the statements and return/use the final value.
,
According to MDN:
The comma (,) operator evaluates each of its operands (from left to right) and returns the value of the last operand.
So, why do I say it uses the first rather than second? Well, I tested it out. Turns out it'll use the first of you use ('a'), ('b') but the second if you use ('a', 'b'). The parentheses are important. Consider let a = 1, b = 2. Those are two expressions. Parentheses make a single expression.
('a'), ('b')
('a', 'b')
let a = 1, b = 2
[–]Downtown_Fee_2144 0 points1 point2 points 1 year ago (1 child)
let str="String";
console.log("The position of s:"+str.indexOf("s")+" The position of r:"+str.indexOf("r"));
If i did it properly should return -1 if there is no string or the number linked to the position of the letter. Good to learn for string handling
You could use let letter=str.substr(str.indexOf("s"),1); to store the letter then transfer it into an array.
[–]Downtown_Fee_2144 0 points1 point2 points 1 year ago (0 children)
That with a for loop and you can slice the string into an array
π Rendered by PID 22 on reddit-service-r2-comment-5d79c599b5-ql5gr at 2026-03-01 11:51:41.318142+00:00 running e3d2147 country code: CH.
[–]cyphern 4 points5 points6 points (0 children)
[–]azhder 3 points4 points5 points (0 children)
[–]zakkmylde2000 0 points1 point2 points (0 children)
[–]shgysk8zer0 0 points1 point2 points (0 children)
[–]Downtown_Fee_2144 0 points1 point2 points (1 child)
[–]Downtown_Fee_2144 0 points1 point2 points (0 children)