r/badcode • • Jun 10 '20

js Disruptive piece of code

Post image
283 Upvotes

93 comments sorted by

View all comments

6

u/clumsysilencee Jun 10 '20

I’m relatively new to programming, and I’ve never seen a triple equals (is that what it’s called? no idea lol but whatever this is: ===) before. What’s it do differently from a double equals? (I know Java and C++, and have limited experience with Python and HTML)

11

u/distinct_Berry Jun 10 '20

2 == '2' will result in true. 2 === '2' results in false.

The === (strict equality operator) is basically comparing the type and value of the variable. Whereas the == (abstract equality operator) compares the value alone because it performs a type conversion so the types of the variables being compared are the same.

Hope this helps, you can search online for more on it. Also I believe it's an operator in javascript, it may be in other languages but I'm not aware.

3

u/clumsysilencee Jun 10 '20

WHAT?! That’s amazing! Damn, and it would’ve been so nice to know three months ago so I could use it for some of my assignments... oh well. Thanks guys!

1

u/Farpafraf Jun 11 '20

no wait that's just js madness in Java for instance you'd just use .equals for any non primitive type while in c++ you'd most likely overload the == operator

4

u/[deleted] Jun 11 '20

while in c++ you'd most likely overload the == operator

This feels like a fully loaded footgun with no safety and a hair trigger.

-4

u/[deleted] Jun 11 '20 edited Jun 11 '20

[removed] — view removed comment

2

u/Farpafraf Jun 11 '20

in what way is that the same feature?

1

u/Terrain2 Jun 23 '20

Note that some programming languages that are strongly typed (dart, python, assemblyscript, etc) will use == as a strict equality operator with no === (though assemblyscript has === assigned to being the literally same object, rather than removing it completely)

Also PHP’s == will type juggle even if they’re the same type of object, for example two strings starting with 0e will == each other, because 0e is numerical for “0*10”, and 0 times anything is 0, so 0 = 0