I was just testing the in operator in Javascript. When I run something like console.log("cookie" in document), it logs true, but when I do console.log("cookie" in "cookiejar") or assign cookiejar to a variable a and then do console.log("cookie" in a), I get an error statement TypeError: invalid 'in' operand a. Can anyone tell me why is it behaving like this?
The in operator doesn't do what you think it does. a in b tells you if a given object b has the property named a.
You cannot use in to search for characters in a string. Use indexOf for that.
'cookiejar'.indexOf('cookie')
Note that the in operator is entirely separate from and completely unrelated to the for-in statement.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With