javascript 1 min read

Lesson 3 -- Variables

Hello guys! Variables is one of the most important concept of Programming and we will be learning today to use variables in JavaScript.

Hello guys! Variables is one of the most important concept of Programming and we will be learning today to use variables in JavaScript.

Let’s drift back to memories of our Elementary Math education where we first learnt about variables(remember dreadful x? Anyone?) in our Algebra class. If you guys cant remember, here is a little example for some help.

In variables we basically take something like letter ‘x’ and equate it to some value:

x = Jatin

So whenever we will say:

x is a good boy. //Statement 1

It will actually mean that:

Jatin is a good boy.

So, here we see the logical interpretation of Statement 1. “x” is replaced by it’s value which is “Jatin”.
That is all what a variable is, it is just a fancy name for a placeholder. I hope you got the point, even if not, then also you will be clear soon when we will try to create an example and execute and then you will get the concept.

So the syntax to use variables in JavaScript is:

var <variable_name> = <value>;

So, let’s go ahead and see a program to understand the usage of variables and then we will see the code and analyse it.

Code:

Now let’s Compile it. You know the shortcut Ctrl+Alt+Shift+R ;)

Output:

Perfect, so we can see in the output windows that our variable(x,y) are replaced by their values(23,17). So this was the basic exercise for you guys.

Syntax Explained:

  • var x = 23;
    here:
    x : name of the variable
    = : assignment operator
    23 : the value of our variable ‘x’
  • document.write(x);
    here it is used to print out the value of our variable x.

So, here we go, this was basic of Variables. In next lesson we will learn about types of variables, and how to implement them in a more advanced manner.

See you guys in the next lesson. Peace!

Leave a comment

Keep reading