Where to assigned values come from?

I am brand new to coding and I am having trouble understanding so basic concepts. I am reading Jon Ducketts “javascript and Jquery” book while simultaneosly taking the fCC course. In the book I am given the following example:

var username;
var message;
username = ‘Molly’;
message = ’ See our upcoming range’;

when the book is assigning values, where are the getting that info from? Who is " molly " and why is the message “see our upcoming range”?

When I assign a value to a variable, do I just arbitrarily put whatever I want because it will get updated in later code?

I know this is a stupid question but I’m clearly missing some very basic but important concept in javascript and it seems like every tutorial just assumes I know.

They just chose arbitrary values for demonstration. If I was doing a variable called “favoriteFood”, then I might have:

var favoriteFood;
favoriteFood = "pizza";

Or on another day I might do:

var favoriteFood;
favoriteFood = "lasagna";

or

var favoriteFood;
favoriteFood = "pad thai";

It’s whatever string I choose to store in that variable.

When I assign a value to a variable, do I just arbitrarily put whatever I want because it will get updated in later code?

Well, if you don’t have a value yet, you can give it a default value, if there is one, or you can leave it undefined. You don’t have to initialize it on the next line.

I know this is a stupid question

No, not at all. This is confusing stuff. That’s why it pays well. Keep asking questions - that means you’re thinking about this stuff.

Thanks for your reply.

Would you possibly be able to give a simple , real life situation or something that I would commonly use javascript to do on a website in which I would use that?

You will encounter them as you learn. You will use A LOT of variables. Pretty much everything you do with a real programming language will involve variables. Any “real world” example is going to contain a lot more JS things that you won’t understand yet. But anytime you are using any kind of number, string, boolean value, data structures, or whatever - any kind of data - you’re going to have to store it somewhere for the computer program. In the old days it was memory addresses but with modern computer languages you can give it a name that makes sense to you to make it easier to remember.

If you want examples, just keep moving forward - you’ll get a whole bunch. For know the important thing is to understand that you can use variables to store values. You have to declare them and you can assign them values. If you use var to declare them, then you can change the values as many times as you want:

var favoriteFood;

favoriteFood = "pizza";

// wait, changed my mind

favoriteFood = "hamburger";

// wait, wife wants me to eat healthy

favoriteFood = "tofu stirfry";

(There are other ways to declare variables besides var; no rush, you’ll learn those later.)

Thanks. Maybe I’m being to impatient. It just feels strange to pass all the challenges but now know what I just did. I’ll just keep on doing it until it makes sense. I appreciate all your help and feedback,.

I wouldn’t say impatient, I’d just say curious. Yeah, the feeling of “Why am I learning this? Am I learning this?” is very normal. There is a certain irreducible complexity to this. So the only way to learn it is in tiny chunks. And we may not see how they fit together at first, but they will. You’ll forget some things and have to adjust your understanding as you go, but that’s OK, that’s normal. Just keep pushing forward. Just understand the lesson in front of you for what it is.

If you understood that you can store data in variables, that you can declare a variable with var and that you can assign it a value with =, then you’ve understood the lesson.

1 Like