Servage Magazine

Information about YOUR hosting company – where we give you a clear picture of what we think and do!

JavaScript Variables

Thursday, September 11th, 2014 by Servage

JavaScript VariablesLike many other programming languages, JavaScript has variables. Variables can be imagined as named containers. Means you are giving a name to the container and that container has some sort of data. In commonsense, we are packing a big data bundle and giving that packing a name that is meaningful and indicating what you have stored in that package. By giving a name to that package, we are saving time and repeated lengthy tasks to put all data in multiple places and multiple times.

Therefore, JavaScript variable has important place in the programming and let you offer ease and comfort of coding. JavaScript variable has many peculiarities and we will explore them in this post in comprehensive manner.

You can declare multiple/many variable in one sentence only by just using comma in between them. Therefore, you need to declare variable once along with its values or data or without it and use them again-and-again in the same document.

Code Example

For instance:

var FirstName, age, job;
Or
var FirstName = "Shoaib", age = 50, job = "content writing";
Or in multiple lines
var FirstName = "Shoaib", 
age = 50, 
job = "content writing";

Second thing you may have observed the use of comma and semicolon in the above code. We have used comma to separate variable names only, not sentences whether they are in a single line or in multiple lines.

Third observation is value definition using = sign. However, we can leave variable without giving them a value at the time of declaration. We can assign them some values afterwards.

One important thing I would like to mention that we can re-declare JavaScript variables, even though we have declared its values before, and this re-declaration may not affect the actual value of the variable that you have declared before. However, you have to keep in mind that during re-declaration you won’t give any value again, just let them be intact.

Do Arithmetic

This is another one peculiarity of JavaScript variable that you can perform many arithmetic functions using simple arithmetic operators such as + (plus), -(minus), ==(is equal to), *(multiplication), /(divide), etc. Of course you have to take care when use = sign because if you use it once only it will become the assignment operator so write twice or thrice.

Data Type

In fact, JavaScript variable has no limit of data types so you will find Boolean, function, array, objects, numbers, simple strings or text strings, but numeric and text data are in abundance in practice. However, syntax of data type may change according to the data. Therefore, you should take precaution when you make simple strings or text strings.

Text strings are always placed in between single of double quotes while numeric data won’t have any quotation mark at all. Therefore, if you place a numeric data in quote, it would immediately be treated as text string, not numeric data only.

Variable Names and Scopes

There are some reserved keywords in JavaScript that set of keywords have forbidden using, as variable names, and reasons are obvious that we should avoid conflict in commands. Another important rule for naming variable is to avoid numbers at the first or initial character of the name. However, you can use number just after _ (underscore). Moreover, variables are case-sensitive so you can differentiate them simply by changing upper case to lower and vice verso.

Scopes of variables are depending on the regions and we have only two regions in programming. The first is global and another is local. We all web programmers know that global scopes are applicable everywhere and we can’t repeat declaration of global variable. Whereas function or object-oriented programming are of local type so you can declare same variable again in another object or function with same or different values.

One of the most interesting thing in scope is that with local variable declaration you can override the global variables when you are using same variables. Therefore, there are some more advantages in object-oriented programming.

References & More Reading
JavaScript Variables
JavaScript Variables and DataTypes
How to Declare a Variable in Javascript

JavaScript Variables, 3.7 out of 5 based on 3 ratings
You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

No comments yet (leave a comment)

You are welcome to initiate a conversation about this blog entry.

Leave a comment

You must be logged in to post a comment.