How do you check if a variable is assigned to a certain string in a conditional statement?

Disclaimer: I’ve searched as hard as I could for an exact answer to my question but I can’t find it, so this question very well exist already since it seems pretty simple, but I just couldn’t see it.

I’m trying to check if a variable is equal to a certain string, and then log a different output depending on whether this is true or not. I am very much a beginner and am really only trying to do this to make sure I understand how strings and conditionals actually work, which I clearly do not understand as of yet.

This is an example of what I am trying to do:

var grocery = [
    'apple',
    'orange',
    'grapes',
    'carrots',
    'nothing'
];
var grocery = grocery[Math.floor(Math.random()*grocery.length)];
console.log(`You obtained ${grocery}!`); // here I'm trying to assign a random string out of the set to the variable grocery. It will then log the sentence. This part has been working fine.
if(grocery == 'nothing') {
let food = false; 
 } else {
  let food = true; // this part, I'm sure you can see, is completely incorrect. But I can not find the correct format for something like this. 
};
food ? console.log('I have food') : console.log('I have no food') 

Right now, the first half of the code runs perfectly and logs to the console, but this last part won’t log due to the error: “cannot read property ‘equals’ of undefined”. I’m assuming that the variable food hasn’t been defined, but I don’t understand why, or how to fix it so that it is.

From what info I have been able to find, I might need an entirely new function rather than the == comparison, which makes sense I guess since you can’t really compare a variable with its own value (?). But I’m completely stumped as to how to check the assigned string of the variable for the conditional statement.

Again, I’m a complete beginner. Started this after completing only the conditional statements review (https://www.codecademy.com/courses/introduction-to-javascript/lessons/control-flow/exercises/review-control-flow) so I’m probably in over my head but I’m just so curious as to how/if this is done. Also I apologise if all the terminology I’m using is wrong, I really have no idea what I’m doing.

Thank you.

Hi @chandlereade96676480
that is a matter of scope: You declare the variable inside the if statement. Outside of that, the variable cannot be accessed. If you move the declaration out and just do the assignment of the boolean inside the if statement, it works:

let food;
if(grocery == 'nothing') {
food = false; 
 } else {
food = true; 
};

You can also use the ternary operator for that as you did with the consoles:

let food = grocery == 'nothing' ? false :  true;

Hey!

As Mirja mentioned, this is an issue of scope. Programs like Visual studio code are useful for things like this as it would grey out food, indicating that it’s value isn’t being read anywhere. I’m also a super beginner but find writing in visual studio and then copying into codecademy significantly easier.

Good luck!