Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Can you give a Javascript variable two values? If so, can you do so like so? var pickles = 'green' && 'vegetable';

If you can do this, would you need to provide both the values in order for this to pass through an if else statement?

Seth

3 Answers

Jason Anders
MOD
Jason Anders
Treehouse Moderator 145,863 Points

Hey Seth,

No, variables as you are describing can only have one value... What you are looking for is an Array. Have a look at this StackOverflow Thread

Keep Coding! :)

Steven Parker
Steven Parker
243,656 Points

There are other types of variables, such as objects and arrays, that can hold multiple values. But a "scalar" variable such as this one holds a single value.

Now for example, you could put those to values into an array variable like this:

var pickles = ['green', 'vegetable'];

--- The rest of this is not directly related to your question. ---

I tried your assignment on codepen and it surprised me. I figured that since you were doing a boolean operation (&&) the result would be the logical AND of the "truthiness" of the two strings. I know that a string is usually considered "truthy" if it is not empty or null, so I was expecting the value to be true. Instead, codepen said the value was vegetable!

So I looked up the logical operators at MDN and discovered that "Logical AND (expr1 && expr2) Returns expr1 if it can be converted to false; otherwise, returns expr2." So it made sense, as "green" would evaulate as "true", your assignment gets "vegetable".

Anyway, by looking into your question, I learned something about JavaScript logical operators I didn't know!

thank you Jason Anders