Difference between using `=` and `===`?

Hi everyone!
I don’t understand why this function only responds to the first if statement:

const calculateWeight = (earthWeight, planet) => {

if (planet = “Mercury”) {
return earthWeight * 0.378
else if (planet = “Venus”) {
return earthWeight * 0.907
else if (planet = “Mars”) {
return earthWeight * 0.377
else if (planet = “Jupiter”) {
return earthWeight * 2.36
else if (planet = “Saturn”) {
return earthWeight * 0.916
else {return “Invalid Planet Entry. Try: Mercury, Venus, Mars, Jupiter or Saturn.”}



In this example I try to log the planet weight of Jupiter if the earth weight was 100. But the function responds with the first if statement, in this case Mercury and gives me this: 37.8

Anyone who can help?


please see this topic:

FAQ: Code Challenges: JavaScript Fundamentals - tipCalculator()

different exercise, but same issue.


You just made my day. Thank You!


I’m also getting an error like megacoder73951 but I don’t get it (even after reading the linked FAQ for tipCalculator). Can someone please explain how I can solve this with if/else and not switches?

Here’s my code:

const calculateWeight = (earthWeight, planet) => {
if (planet = ‘Mercury’) {
return (earthWeight * 0.378);
} else if (planet = ‘Venus’) {
return (earthWeight * 0.907);
} else if (planet = ‘Mars’) {
return (earthWeight * 0.377);
} else if (planet = ‘Jupiter’) {
return (earthWeight * 2.36);
} else if (planet = ‘Saturn’) {
return (earthWeight * 0.916);
} else {
return ‘Invalid Planet Entry. Try: Mercury, Venus, Mars, Jupiter, or Saturn.’;

console.log(calculateWeight(100, ‘Jupiter’));


if (planet = 'Mercury') 

a single equal sign means assign, is that really what you want to do in an if condition?

1 Like

Since I wasn’t the only one making this simple mistake, I’ll be literal:

if (planet == ‘Mercury’)

This is the way to type it.

The double equal is the coercive comparison that will do its best to compare two unmatched data types, such as, 1 == '1', which is truthy when one operand is coerced to the same type as the other. Best practice would be to avoid this and use only strict type matching, ===

1 === '1'  => false

In JS we never compare with a single equals sign. That is for assignment, only.

Oh, thank you for explaining that thoroughly!

// Terese

1 Like