Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialUnsubscribed User
866 PointsindexOf Syntax
In this video we're introduced to the indexOf method. While I understand that the method returns the number of the position of a given character found within the argument passed... I'm not sure I fully understand how the syntax was formulated in order to turn the returned result into a boolean. In this case we used (return email.indexOf("@") != -1). In this case could we have used (return email.indexOf("@") !== -1)?
2 Answers
Nicholas Mejia
23,800 PointsJen, when it comes to comparison operators think of it this way:
We can't use a single equal sign (=) to compare because that is how we assign a variable. The browser would interpret X = 1 as X should now be one. This can be a problem if you have your x variable set already, so instead we compare with a double equal sign to say CHECK if X is equal to 1. When it comes to not equals, we no longer have to use a double equal sign just to check if something is loosely not equal so we can just say X != 1.
If that doesn't make sense, think of it this way.
X == 1; //Roughly equal to 1 X === 1; //Strictly equal to 1
X != 1; //Roughly not equal to 1 X !== 1; //Strictly not equal to 1
Technically the email.indexOf("@") !== -1 would work because if we cant find "@", it's always going to be a false return, but be careful when using it when mixing various types of data.
Say X = 1.
If you check if(X !== "1"), it would return false because it is a string where as X is currently set to an integer.
Hope this makes sense!
Stone Preston
42,016 Points!==
compares the values AND the data types whereas !=
just compares the values. In this case you could use either one. The statement doesnt turn the returned result into a boolean, it returns a boolean.
!= is a comparison operator which means not equal. So what this statement evaluates to is return true
if the indexOf(@) function returns something other than -1, and return false
if the indexOf("@") returns -1.
this post explains the strict comparison operators. (!== and ===) the answer is below:
JavaScript has both strict and type-converting equality comparison. For strict equality the objects being compared must have the same type and:
Two strings are strictly equal when they have the same sequence of characters, same length, and same characters in corresponding positions. Two numbers are strictly equal when they are numerically equal (have the same number value). NaN is not equal to anything, including NaN. Positive and negative zeros are equal to one another. Two Boolean operands are strictly equal if both are true or both are false. Two objects are strictly equal if they refer to the same Object. Null and Undefined types are == (but not ===). [I.e. Null==Undefined (but not Null===Undefined)]
Unsubscribed User
866 PointsGot it. Thanks!
Unsubscribed User
866 PointsUnsubscribed User
866 PointsTotally makes sense. Thanks!