JavaScript – Beginner Tutorial 04 [Variables]

Javascript Tutorials - Variables

Hello guys, welcome to my 4th javascript tutorial. I think you got understand the lessons I taught you before. If you didn’t check them, please just go back and come here to be master of javascript. Because it’s good to know about commenting and how to print something. However, today I wanna teach you a huge concept of the programming world. Variables: yeah it is essential to everything you’re going to do next.

You know what is a variable, don’t you? We’ve learned it from our primary classes also. We have solved some problem using this variable concept. So let’s remember it.

X = laptop (I created a variable as X, so X is equal to Laptop)

I can live without my X (Yeah, I just assigned X variable to a sentence. It means “I can’t live without my laptop“)

We can assign anything to easy and fast up our work with this concept. Maybe it’s a text, numerous thing, image, audio or whatever it is, we can make a variable and use the variable everywhere we need to place.

So, in javascript, we are using variables a whole bunch and we use them kind of in the same way. But, there’s a little bit difference. Now in order to tell javascript, this is a variable, we use the keyword ‘VAR’. By defining it, javascript will understand this is a variable and you can name it and assign anything to the variable. Let’s clear it by doing a sample. As before, open your notepad and test it.

var x = 21;

OK, you made a variable, here you need to remember the semicolon is needed when you’re making a variable. Just remember it. Now let’s print the variable we created in the browser.


After typing the second line you will get your output as 21.

OK, Now let’s add some text to the variable.

document.write(‘I am ‘ + x + ‘ years old’);

I think you made it, However, your output should be like “I am 21 years old”. In case, take a look at apostrophes. I have added a space at the end of “I am” and start of the “years old”. If I didn’t do it, javascript will add all 3words together like this “I am21years old”.

The benefit of making variables is, if you change a variable value one time, all the values assigned by the variable will be changed one time. and another most important thing is variable are case sensitive, it means capital X is not equal to simple x. if you do it you will get nothing. And another thing is you need to start your variable name with a letter or an underscore (_). you cannot use & or percentage signs or anything like that. In addition, other letters can be only letters, numbers, and underscore. Just remember it and never do this mistake. and lastly, your variables can’t start with a number. Finally, if you follow these simple rules, you’re good to go.

Now you can understand basically what a variable is and how it is useful in computer programming and how to use it in javascript. Check our website for more tutorials and come back again another day to check next lesson. Thank you guys and have a nice day.

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: