Difference between two codes

Can anyone tell me why ‘rose’, ‘lilly’ and ‘sunflower’ equals ‘peonies’, ‘daisies’ and ‘whatever’ when obviously it doesn’t? Why does the + make the difference?

1 Like

Hey @mrt_yonkova,

Great question — and love that you’re exploring how coding works in the gallery snippets!

Let’s start with the last example first, and what is actually being compared. In JavaScript, you can append strings together using the ‘+’ symbol. So, for example, the code below will output Marta says: Hello World.

let name = 'Marta'
let output = name + ' says: ' + 'Hello World'
console.log(output)

In your example, ‘peonies’ + ‘daisies’ + ‘whatever’ would append all the strings together creating:

'peoniesdaisieswhatever'

Which is not equal to the array ['rose', 'lily', 'sunflower']. Both the data type (string vs. array) and the contents (flower names) are different.

Now, if we look at the first example, things get a little interesting.

We know the + symbol appends strings together, but what does the ‘,’ symbol do?. We know that when used within an array [] it separates items, but the code you shared doesn’t have the flower names within an array. So what are the , symbols doing when not within an array?

It’s a little complicated (more information here and here), but the , symbols are basically telling the if statement: look at each item separated by ,, and if the last one is true, run the true code.

Here is simple example of what is happening, where the last item seperated by the comma is returned false, and the console.log() does not run:

if (true, false) {
console.log('This will not run')
}

Whereas this code will run, since true is the last item:

if (false, true) {
console.log('This will run')
}

Obviously, if statements aren’t very useful if we just put true or false in them, so some more examples:

if (1 === 2, 'hello') {
console.log('This will run')
}

In the above example, although 1 === 2 is definitely false, there are two items seperated by the comma symbol, so the if statement looks to the last item. In this case, 'hello', which is true (all non-empty strings are true — checkout how this works through truthyness here)

In your example:

if (flowers === 'peonies', 'daisies', 'whatever') {
console.log('I love roses, lillies and sunflowers');
} else {
    console.log('I need more flowers')
};

There are three items separated by two comma symbols. The first item (also called expression) is flowers === 'peonies', which is false. The second item is 'daisies' which is true. And the third item is 'whatever' which is true. Since the , symbol is telling the if statement to look at the last item (also called the last expression), true is returned.

Phew! That’s a lot of information, and might seem a bit confusing. Don’t worry too much about the comma operator/symbol, it’s a less common operator and can only be used outside of arrays, objects and most functions.

If you want to compare the contents of the array, you can simply add array brackets to your code, like this:

 if (flowers === ['peonies', 'daisies', 'whatever']) {
console.log('I love roses, lillies and sunflowers');
} else {
    console.log('I need more flowers')
};

Hopefully this was helpful, and thanks for posting / finding such a great question! I’m excited to see where your coding journey takes you.

H :slight_smile:

1 Like