So, you've just started your programming journey and it has been a pretty mixed feeling - sometimes feeling extremely excited when your code runs, and the other times , extremely frustrated when you have no idea what's wrong . Well, either ways, I'm happy to welcome you to the brotherhood.
In this post, I'll be talking about a concept in programming (and consequently in JavaScript) which is very important and this is the concept of variables.
Variables are very important and essential in programming and all programming languages, and I should I have talked about it alongside another related important concept (Data Types), but I'll do that in a separate post.
What are Variables?
A variable is a name in memory that stores a value.
Let me take you back to your basic mathematics algebra class:
Let x = (22/5 + 2)
Assuming (22/5 + 2) is a long expression, we have just saved this expression in a variable x, such that, anytime we use x in our subsequent calculations, we are actually referring to (22/5 + 2). You dig?
Now, coming back to JavaScript, this is how variables are used:
i = 1000;
In the case above, i is the variable name and 1000 is the value stored in the variable.
From the above example, whenever we use the name i in other parts of the same program, the value 1000 will be substituted.
I want to think it's making sense now.
In JavaScript, it is advisable to declare a variable first before using it, and you do this by using the var keyword. As in:
var name;
name = "Elusoji Sodeeq"
In the first line of code above, what we did is called "variable declaration". We are telling the JavaScript interpreter, :"hey, I want to register this variable name with you".
On line 2, what we did is called "variable assignment". We assigned value "Elusoji Sodeeq" to the variable we declared earlier. This is like saying, : "hey, JavaScript interpreter, remember the variable I registered with you earlier. Good. I want to give it a value now".
Now, that's it, we have just created and used a variable in JavaScript.
Variables can be used to store values of any type. I'll talk about Data Types in another post.
You can read further here
That will be all from me, for now.