Saturday, 26 August 2017

'in' operator in JavaScript. String comparison




Hi I'm new in JavaScript and i find a basic problem:



When I use that piece of code in Python:



'a' in 'aaa' 


I get True




When I do the same in JavaScript I get Error:



TypeError: Cannot use 'in' operator to search for 'a' in aaa


How to get similar result as in Python?


Answer



I think one way is to use String.indexOf()



'aaa' .indexOf('a') > -1



In javascript the in operator is used to check whether an object has a property


No comments:

Post a Comment

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...