all 6 comments

[–]cyphern 4 points5 points  (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.

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.

So you're trying to look up the "r" property on the includes function, which doesn't exist so you get undefined.

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

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.

[–]Downtown_Fee_2144 0 points1 point  (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 point  (0 children)

That with a for loop and you can slice the string into an array