Learn HTML,CSS, Python etc from the foundation

Tuesday, February 19, 2019

UNDERSTANDING JAVASCRIPT VARIABLES |THINGS YOU WOULD LIKE TO KNOW

No comments :

I’m going to take this topic bit by bit for easy assimilation. So in this lesson, I’m going to give you a simple background of how variables work in Javascript. I will, in our subsequent lessons, slowly go deeper and deeper in a simplified manner. My aim is to help newbies or beginners understand every concept (if possible) in JavaScript programming. Having said this, let's move on.
JavaScript variables are often seen as the containers for storing data values.  The good thing about JavaScript variable is that it can hold many data types, numbers, strings, objects, to mention but a few. You don’t even need to declare the variable type you are using in JavaScript.
You might be wondering why variables are necessary for JavaScript? Well, variables are very necessary while programming in JavaScript because it will make a lot of work much easier and simpler for you. For instance, if you have 500 lines of codes that may include the variable “x”. By the time you want to effect a change in the code, you can simply modify the value of “x” once, and it will automatically reflect in all places where it is used. (Keep calm if you’re a bit confused, it will definitely become clearer as we proceed in subsequent lessons). However, it is worthy of note here that JavaScript variable names are case sensitive. For example, variable x is not the same as variable X. In addition, if you want to declare a variable, simply use var keyword.
For example
var x = 15;
In the above line, var is a shortened form of the word "variable". is our variable name. The equal sign (=) is called the “assignment” operator rather than an equal operator (please note this!).  The above means that variable x is assigned the value of 15.
If you now want to see the result, you have to use this function  “document.write ():
Inside the bracket is where you will input your variable name, followed by a semicolon.
Example,
Open your text editor and write the following;
var x = 15;
document.write(x):

IT WILL LOOK LIKE THIS

SAVE AND OPEN IT, IT WILL LOOK LIKE THIS (the output is very thin though)


 WRAPPING UP

In my next lesson, I will be taking a step further into the subject matter, make sure you acquaint yourself with this lesson.

No comments :

Post a Comment