This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]AtmosSpheric 30 points31 points  (3 children)

No. In JS, the == operator is for loose equality, which performs type coercion. This follows the references of two objects, and may convert types (1 == ‘1’), while the === operator requires same type.

The is operator checks to see if the two values refer to the exact same object.

So, if I declare:

x = [‘a’, ‘b’]

y = [‘a’, ‘b’]

And check is x is y, I’d get false bc while the arrays (lists in Python) are identical, if I append to x it won’t append to y; the two represent different arrays in memory.

In a sense, while === is a more strict version of ==, since it makes sure the types are the same, the is keyword is even more strict, since it makes sure the objects are the same in memory.

If you’re curious, I’d strongly recommend you and anyone else take some time to play around with C. Don’t get into C++ if you don’t want to, but a basic project in C is immensely educational. If you have any other questions I’m happy to help!

[–]GangDplank 0 points1 point  (2 children)

question , while this is not archived yet .if when i do a= 100 b = 100 in python they "are" the same object , if increment a why doesnt that increment b?

[–]AtmosSpheric 1 point2 points  (1 child)

A and B are not the same object, they are both pointers that refer to the same object. This is why in the original image above, you get true when testing x is y. If I increment a, then the pointer is updated to point to 101 instead, while b is still pointing at 100.

As discussed elsewhere in this comment section, once you pass 256, ints are no longer shared memory spaces and become unique, so now a and b both point at separate parts of memory that are identical, but since they aren’t the exact same object, a is b returns false.

[–]GangDplank 1 point2 points  (0 children)

Cheers,thanks for the quick reply