#Doubt between == and === in a lesson

43 messages · Page 1 of 1 (latest)

naive gale
#

Hi, in this lesson https://scrimba.com/learn/learnreact/forms-in-react-checkbox-co6da4637a27cca33be7cb1d7 on line 19 (please drag the slider to the end) , why does he use === instead of ==? type is of type string right? And if we compare same types of data, we can use == right?

Scrimba

Learn to code with interactive screencasts. Our courses and tutorials will teach you React, Vue, Angular, JavaScript, HTML, CSS, and more. Scrimba is the fun and easy way to learn web development.

south shale
#

Was an addition to Js after problems with ==.

naive gale
south shale
#

That doesn't make sense

#

It did type coercion which wasn't wanted

#

Also true == 1 was also true

#

It was just unwanted behavior

#

This is ==

#

This is ===

naive gale
#

i understand
but in the specific example, type which is equal to event.target.type is already of type string right? so there wont be event coercion

azure gorge
#

Using == is perfectly fine.

south shale
#

No... You're asking for problems in the future.

#

Just always use ===

#

If you get the funky type coercions then sure use ==, if you don't then don't use it.

azure gorge
#

For example:

"I don’t think we need to “fix” types in JS (like TypeScript or Flow), I think we just need to understand them better and more clearly communicate our types in JS. For example, completely contrary to most common belief, the == operator is actually more powerful and more useful in our programs than ===, and in fact, usage of == should be preferred and === should only be a last option. But to understand why I make that claim, you have to actually learn types, and the way they’re converted: coercion. I really wish developers wouldn’t just keep parroting back “JS types are evil” like they’ve heard for years. They’re great, and your programs suffer if you don’t learn and use them to their full potential." (from https://levelup.gitconnected.com/kyle-simpson-ive-forgotten-more-javascript-than-most-people-ever-learn-3bddc6c13e93)

or if you read https://notlaura.com/notes-kyle-simpson-javascript-spirit-guide/ .

south shale
naive gale
#

ok i see what you mean

south shale
#

also typescript ftw

azure gorge
south shale
#

[1] == true is not funky?

#

false == [[]] is not funky?

naive gale
#

as someone with a c++ background, it definitely caught me offgaurd

however if someone wasnt used to dealing with strict types, he might not find it weird

naive gale
south shale
#

someone on so made it pepe_shrug

azure gorge
# south shale `[1] == true` is not funky?

No, when does it happens? In your example written here... as I said, you have to know the tool you are using (JS) and case scenarios you are developing your JS algorithm for.

#

Coercion has its quirks, but then again, you have to know the language you are using.

south shale
naive gale
#

i think it's a matter of perspective

south shale
#

But it all goes back to, if you know what's happening and what you're doing then don't care what I say, if you're a JS beginner rather just not run into this unexpected behavior.

naive gale
#

👍 i'll go back to my lesson
thanks for the advices 😃

azure gorge
south shale
#

So your bottom line is ignore JS sucks and is totally random from time to time and we just have to see that coming instead of doing better.

azure gorge
silent prism
#

== produce ambiguity sometimes, for fine results always use === for correct typeset. But i think sometimes it is best to have string number comparisons. You not need to parse data to same data type.

mild thunder
#

@azure gorge JS has good and bad parts. Sadly it mainly has "bad" parts and the language is getter over-bloated with loads of crap from other languages. Most of those feature are not needed at all and has very rare use cases.

You quoted somebody who stated we shouldn't try to fix types in JS.
JS had been written in 2 weeks, it has a very poor design.
Having to work and THINK in types are 2 very powerful tools you can get, it will make your life so much easier.

Since JS coerce almost everything most of the time into something "unusual" it makes the language looks unpredictable when actually it is not, when you understand what is going on under the hood.
BUT the problem is understanding this weird behaviour which is not a pleasant road and I would recommend everybody to ready through the docs once, then forget about it and just use strict equals everywhere.

azure gorge
azure gorge
mild thunder
azure gorge
mild thunder
# azure gorge Sure, I just share it in the sense of more information, and also as a resource t...

I love how much you contribute to this community. You always have some unique questions which are very valuable to me and I am sure to others as well! ❤️
I am learning a lot from you and I love that both of us has different perspective on certain things and this makes this forum very vivid (in the best way possible) and I love hearing from your experiences and views, so thank you for being awesome! 💪