Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Comments on What's the difference between =, == and === operators in JavaScript?

Parent

What's the difference between =, == and === operators in JavaScript?

+8
−0

While learning JavaScript, I started to see =, == and === operators. What's the difference between them?

History

0 comment threads

Post
+2
−0

These are the differences between these operators:

The Assignment Operator (=)

This isn't for comparing things; it’s for giving a value to a variable. It’s like putting a label on a box.

  • Example: let fruit = "Apple";

  • Translation: "Take the box named 'fruit' and put an 'Apple' inside it."

The Loose Equality Operator (==)

This checks if two things are roughly the same value. It’s like a friendly clerk who doesn't care if you pay with a 5 dollar bill or five 1 dollar coins, they both "equal" five dollars.

  • Example: 5 == "5" (True)

  • Translation: "Is the number 5 close enough to the word '5'?" JavaScript says yes because it converts the word into a number for you.

The Strict Equality Operator (===)

This is the "perfectionist." It checks that the value AND the type (like number vs. text) are exactly the same.

  • Example: 5 === "5" (False)

  • Translation: "Is the number 5 exactly the same as the word '5'?" JavaScript says no—one is a number, and the other is just text that looks like a number.

Almost all developers use === by default. It’s safer because it prevents JavaScript from making "helpful" guesses that might lead to weird bugs later on.

History

1 comment thread

How close do the types need to be? Are all numerics lumped together? Is `1 === 1.0` true or false? (1 comment)
How close do the types need to be? Are all numerics lumped together? Is `1 === 1.0` true or false?
Monica Cellio‭ wrote 5 months ago

How close do the types need to be? Are all numerics lumped together? Is 1 === 1.0 true or false?