How do you use Variables and for what?

Hi so I am pretty new to coding like in java as I used blocks (cuz it’s easy and fun) but now that I am starting to learn a bit of javascript I really want to know what I should be using variables for because I honestly don’t really understand them. In one of the lessons or activities it says something about using variables and doing stuff like for example

let myName = 'Aram' let myCity = 'LosAngeles' console.log(`My name is ${myName} My favorite city is ${myCity}`);

I didn’t really understand why we need the dollar sign or why we need the {} also what do they do in this and why do we need the things if you could answer my questions it would be greatly appreciated

At the start of the school year we generally organize our courses into binder form with tabs upon which we write the name of each course. Those tabs are variables and denote the section of our binder where all the material relating to it are kept.

Objects occupy memory. Variables tell us where in memory that an object is stored (for the most part). They are labels.

Maybe your dad has a bolt bin in the garage or shop. That’s memory. The slots are where we keep bolts of a certain size. To each bin, or slot, is affixed a label describing the size. That is a variable.

_3x9mm24tpi

Variables describe the object they refer to, or can at least be deciphered to a description. Above we have described a three inch bolt with a shaft of 9 millimeters and twenty-four threads per inch.

1 Like

So basically what you are saying is that variables are like folders for keeping information such as strings and values?

1 Like

Not folders, as such, but labels on the folders. Memory is the folder. We are often told that we store things in variables. To me that is a misnomer. We store things in memory (JS and the OS look after how that is done), and we refer to (as in point to) them with variables. A value in memory that has a variable bound to it is said to have a reference. If we change the value a variable points to, the reference to the old value is lost and the memory it used is freed up for future use (called, garbage collection).

3 Likes
let a = 12
console.log(a)    //  12
a = 25
console.log(a)    //  25

Above we declared a mutable variable (meaning we can re-assign a new value to it). Once declared, the variable is a static label in memory and known as a property of its execution context which you’ll learn about later as a scope reference. We are able to assign new values to this variable at any point in our program. The value 12 occupies a different slot in memory than the value 25. Once it loses a reference from a, its memory is freed up.

You’ll soon learn about data types. Since a variable is just a name/label we can assign data of any type for it to reference. Our example above assigns primitive values which will then be evaluated as ‘number’ type. This will be coming up soon, so I won’t spoil it.

1 Like

This helps a lot because i’m still trying to understand like all of it but… its a start Thak you

1 Like

Every program uses memory. How that is managed is not our concern. Variables give us a means of identifying stored values. When we wish to store something, we immediately give it a referrer so it has a binding. Only with const do we have a static binding, else it is a dynamic one, meaning where our variable points can be shifted around.

const cat = "Tabby";

We now have a string literal in memory, with a static binding to the variable, ‘cat’. Static binding means it remains in memory for the entire session and is immutable (cannot be re-assigned or deleted).

1 Like

so I could use the const and some type of variable to have information stored that can not be changed?

1 Like

Yes, exactly.

cat = "Manx"
    ^

TypeError: Assignment to constant variable.

The above is a run time error that was thrown as soon as I ran the code with that line added.

Oh so that happened because it is a variable that can not be changed and so if you try to change it there will be a error

1 Like

I have 2 questions. 1 what do the brackets do {} I don’t understand what they do or why we need them. 2 what does ; do in code because sometimes I need to put it after code such as if I do

console.log(Math.ceil(43.8)); let snakeEye = 12 if(snakeEye > 10){ console.log( 'wow its 10+ snake eyes')} console.log('If the health of the snakes is good then they will not be sick, but this is common kowledge');

Hello @mtf, I read by chance this thread and I’ve also a question regarding variables, values and memory allocation. We have variables that in the case of primitive types point to an memory-addresses in the call stack that hold the value, we assign to a variable. My question is: the variable (the identifier) should be also allocated somewhere in the memory. Isn’t it? And if yes, where and how is it stored? Thank you!

1 Like

Curly braces do many things in JS, and it depends on the context which role they play.

  1. They are used to signalize the beginning and the end of a code block like in a function
function myFunction() {
// do something // 
};
  1. They are used in template literals to ‘inject’ expression in your string declaration: Template literals (Template strings) - JavaScript | MDN, with a dollar sign before them.
let a = 5;
let b = 10;
console.log(`Fifteen is ${a + b} and
not ${2 * a + b}.`);
  1. They are used for the literal notation of an object: Object initializer - JavaScript | MDN.

const object1 = { a: 'foo', b: 42, c: {} };

  1. and maybe other things … :slight_smile:
2 Likes

Values (data) are stored in memory in their raw form, instructions are pushed to the call stack once they are compiled. The variables reside in the instructions but reference their respective values.

2 Likes

so they are just used for many things. If one of you guys could answer this what are ; used for because usually if I don’t put them after a new string when creating variables or after a console log the code breaks

The semicolon (;) tells the interpreter it has reached the end of statement and can quit parsing. The statement is then interpreted and compiled then pushed to the call stack to await execution.

1 Like

Oh ok thanks for the clarification it really helps because I’m trynna understand things more like what they do and why we need them

1 Like

One of the first things we have to contend with in learning any language is the particular syntax that is expected of well formed instructions (statements) and expressions (values). We learn that JS statements normally signal their end with a semi-colon, and then it gets confusing when we learn that not all statements require them.

a = 42;    //  assignment statement

console.log(a);    //  output statement

f = function () {

};        //  function expression (note the assignment)

The exceptions to the semi-colon rule are,

if (condition) {

} else {

}    // no semi-colon

while (condition) {

}    //  no semi-colon

switch (expression) {
case 42: // action statement;
}    //  no semi-colon

(case clauses are statements so get a semi-colon)

function foo() {

}    //  no semi-colon on function declaration

Bottom line, JS has a fairly high tolerance for missing semi-colons and will automatically insert them when it is deemed necessary. This is a concern that will become more apparent with time, so don’t be too hung up on it.

1 Like

ok thank you for this it helps a lot

2 Likes

Hi umm I wanted to share something I made its not that complicated but I mean I like it I also have a question. My question is how could I put this code into like a game of some sorts like with a refresh button I am just asking as I am curious

et noMatch = (Math.floor(Math.random() * 20 )); console.log(noMatch) switch(noMatch) { case 1 : console.log("thats pretty low is that lucky maybe, maybe not"); break; case 4: console.log("you had a 1 in 20 chance to get this but still not luCky also eat because I know that Eating is good.also CreaM is good And Rodents are ok i guEss"); break; case 8 : console.log("double what we got or you didnt get 4 but still pretty good I guess...........3.....") break; case 18 : console.log("If this is how old you are congrats because you can buy a house or a car if you want to pretty cool I\'d say"); break; case 13 : console.log("hello teenager either you just turned 13 you are way past that or you are still 13 but maybe turning 14 I don\'t know but it does not matter...............5..................6") break; case 2 : console.log("A magical number I would say 2 is just perfect but that\'s just my opinion") break; case 20 : console.log("th highest value you can get I think. But you the user will never know unless............2...............1") break; /* hello to the user if you see this then hi I will be explaining why we need things such as the case the break; and the "" inside the console.log the reason you need the case is because it is a short version of the if else command called a ternary operator which is jsut the if else or the if. the break is telling the computer to stop the readig of the code because if the computer did not stop it would be a big mess. the reason you need the qutation marks is because it is telling the computer this is a string and not code as if we did not have the qutation marks the code would break because it would think that the string is code and so it wold result in a eror as it is not correct code. Anyways this is it im not going to explain any other details as it would take too long (ps. there is a riddle but you are probably going to cheat because I was very vague i will give one clue the variable has to equal true.) */ case 35621 : console.log("If you have reached this point then you are probably a coder or a hacker because to get to this you would need to manuall assign the variable noMatch to the value 35621 which might be confusing for many people but congrats if you got here, now I know this is a run on sentence but I don\'t really care anyways congrats and bye you wasted your time getting this value ........4") break; default : console.log("you got unlucky this is the default setting so, you are unlucky") } let iceCream = (0) if(iceCream === 1){ console.log (' If you scrolled down shame on you but those that solved the riddle which was extremely hard since I gave no details such as what the variable had to equal or its exact name but if you did solve it then lol cuz its another waste of time ') }