Comparison strict vs... weak?


#1

Hi!

I’m learning Javascript from a book that strongly recommends using the strict comparison (eg ‘===’) rather than just good ol’ comparison (eg ‘==’) for run-of-the-mill control flow.

I’ve spent about 1.5 years learning python. Only in a few cases have I encountered a situation where I needed to use strict comparison. Maybe this is because I tend to be deliberate about how I use certain datatypes, or maybe it’s just because I have limited experience.

So the question: Are strict comparisons always preferred in javascript? Does this also apply to python?


#2

Hi @austen Python don’t makes the distincion between strict and type-converting comparisons like JavaScript (https://docs.python.org/3/library/stdtypes.html#comparisons) because it doesn’t convert different types automatically. Python will not convert a string to a number unless you tell Python to do it and even then it’s hard.

In JavaScript this is easy, so the distinction between strict and type-converting comparisons is important (because JavaScript will automatically convert different object types). So has the string “1” the same value as the number 1, because “1” and 1 will have the same value of 1. This means “1” == 1 will be true, but “1” === 1 will be false because in the later case JavaScript is forced to recognise the object-type and will see that they are not the same (the first is a object of type string the second a object of type number).

From MDM:

The more commonly-used abstract comparison (e.g. == ) converts the operands to the same type before making the comparison.

And here the whole link):

I hope I could help.


#3

Thanks for the link! I misunderstood the strict comparison. I was thinking that “===” was something like the “is” keyword from Python. Now I see they’re totally different. is checks to see if the evaluated object is identical to the instance being compared.

Wow. Automatic type conversion… that might make debugging a little crazy. No wonder they use === for comparison! Is there a way to enforce/coerce the type to which an expression evaluates? Kind of like int(), str(), bool() or float()?

Anyhow… I’ll make sure to use the strict comparison in JS.

Thanks again!


#4

Yes you can do that. On W3School is a very good explanation about this topic:
https://www.w3schools.com/js/js_type_conversion.asp

(Note: W3School had a bad reputation amongst “professional programmers” a while ago. I didn’t found any reason why. W3School helped me a lot. The easy but not simplistic explanations explain often heavy topics in a very understandable way. And in fact because of this the other ressources had to keep pace with it. So I found that MDM (Mozilla) worked hard in the last years to make their docs readable. W3School has also a very good intro to Python. You can read something on Python.org and then go to W3School and understand it :slight_smile: )


#5

MDN is the most reliable and complete resource you will find on the internet when it comes to JS.
W3School is good enough but it doesn’t cover all the details. It’s heavy on examples, easy to understand, I agree. It is very good for a beginner, but as you grow as a dev you will find it insufficient.
So for advanced techniques and info always go to MDN.
I think MDN did a huge amount of hard work to gather it all in one place: JS, JQuery, the DOM, accessibility guidelines, anything web development plus Firefox dev tools, which are the best ( much better than Chrome ones)


#6

Corrected some false wording of mine. A string and a number are not of type object in JavaScript.


#7

Yes thats true. But I like it to have different ressources, because I don’t always understand the first explanation in one place. It is sad enough that the WebPlatfform.org had to die https://webplatform.github.io/ it was a very ambitious project from Google, Apple, Microsoft, Facebook, etc. and had a very interesting start. But I think it’s dead strenghtened the MDM Platform.

And by the way, I’m a beginner in JavaScript and programming though :slight_smile: so W3School is perfectly for me :grin:


#8

I like Firefox much better too…


#9

I like FF better too but I am addicted to some features of Chrome, I know it’s stupid :smiley:


#10

One thing I recognised over the year is, don’t be to much attached to a tool. I was always on the search for the best super tool. But I found that it doesn’t exist (by the way Vim is very close :slight_smile: ). There is always some feature a other tool has that is better for some kind of stuff. I also think that the Chrome dev tools does some things better than Firefox so I use it in the particular case :sunny: the only thing is that you always have to deal with other commands and stuff and don’t get the work done as quickly as you like … Even Edge and Safari has sometimes good stuff :laughing:


#11

Hey, @DidierCH the best way to learn it, like Zed thought us is code code code and repeat.
After completing the JS book, all the exercises, all the vids , go in the wild and do small things.
Here’s an example of a small thing: world countries by continent using a REST API:
https://codepen.io/ioiomano/pen/mMGwZa.
It was an exercise I received while doing that internship and I did quite a few of those.
It really helps.
Spoiler alert: my codepen sucks, it’s full of unfinished drafts, lol.


#12

Hell yes! Thanks for the advice. And by the way: cool stuff! :+1:


#13

I think everyone here has you covered @austen, but I think of == as “probably equal” and === as “definitely equal”. There becomes a big problem when you get into inequality like < and > since there isn’t an equivalent too (AFAIK). That makes all the safety savings you get from === only slightly good since there’s plenty of other ways you can mess up < or > comparisons too.