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?
The main difference between the == and === operator in javascript is that the == operator does the type conversion of the operands before comparison, whereas the === operator compares the values as well as the data types of the operands.
In JavaScript, strings can be compared based on their “value”, “characters case”, “length”, or “alphabetically” order: To compare strings based on their values and characters case, use the “Strict Equality Operator (===)”.
The strict equality operator ( === ) checks whether its two operands are equal, returning a Boolean result. Unlike the equality operator, the strict equality operator always considers operands of different types to be different.
To compare two strings in JavaScript, use the localeCompare() method. The method returns 0 if both the strings are equal, -1 if string 1 is sorted before string 2 and 1 if string 2 is sorted before string 1.
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
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