JS Variables
Variables are like containers that hold values that can change. "Break" the word variable into two meaningful words and you have vary-able.
A variable holds value that can be used either in an arithmetic computation or in concatenation with other data-types at a later time.
JavaScript variable definition rule does not insist on defining the type of values that can be passed to the variable when the variable is created. It is regarded a weakly typed language for this reason. A single JavaScript variable can hold a string, float, double or integer value.
The keyword var in the variable declaration above is not mandatory. JS variables can be defined with or without it. It is advised though to have it included in your variable declaration as some JS app developers have complained of inconsistencies in their application as a result of its exclusion.
Java on the other hand is a strongly typed language. This means that upon variable declaration, the specific data-type that the variable is allowed to store is defined. Take a look at the example below.
The variable name value is defined in the example above to hold only String values. Passing integer, float, or double to this variable will trigger an error. "count" in the variable declaration above stores only integer values. any attempt to store a value of a data-type other than integer will produce an error.
Local VS Global JS variable declaration
<script type="text/javascript" language="javascript">
var website = "www.dissertation.com.ng";
function whatis(){
var website2;
}
function almagamate(){
document.write(website);
//outputs www.dissertation.com.ng
document.write(website2); //This prints nothing to the screen as variable website2 is not within it's scope.
}
</script>
The script above clearly shows the difference between a local or procedural variable and a global variable. website is a global variable which can be called from anywhere in the script.
website2 on the other hand is a local variable. Local in the sense that it is available for use only within the function block whatis().
Initializing JS variables
Initialization is the passing of initial values to a variable. JS variables do not have to be initialized upon variable declaration. They can be created and stored either as procedural variables or global variables.
The variable "website" in the example directly above is initialized with the value "www.dissertation.com.ng" upon declaration. But you really do not have to initialize a variable upon definition. I can also create the variable in the fashion below.
The above is a perfectly defined JS variable as well. Only that this variable does not have an initial value. We will look at scenerios where you do not want to initialize your variable upon declaration in future posts.
My next blog will be on data-types as it goes hand in hand with variables.
Enjoy!
Joseph Kayode Agbede
Variables are like containers that hold values that can change. "Break" the word variable into two meaningful words and you have vary-able.
A variable holds value that can be used either in an arithmetic computation or in concatenation with other data-types at a later time.
var name = "James";
Name = "Kayode";
The keyword var in the variable declaration above is not mandatory. JS variables can be defined with or without it. It is advised though to have it included in your variable declaration as some JS app developers have complained of inconsistencies in their application as a result of its exclusion.
Java on the other hand is a strongly typed language. This means that upon variable declaration, the specific data-type that the variable is allowed to store is defined. Take a look at the example below.
String value;
int count;
The variable name value is defined in the example above to hold only String values. Passing integer, float, or double to this variable will trigger an error. "count" in the variable declaration above stores only integer values. any attempt to store a value of a data-type other than integer will produce an error.
Local VS Global JS variable declaration
<script type="text/javascript" language="javascript">
var website = "www.dissertation.com.ng";
function whatis(){
var website2;
}
function almagamate(){
document.write(website);
//outputs www.dissertation.com.ng
document.write(website2); //This prints nothing to the screen as variable website2 is not within it's scope.
}
</script>
The script above clearly shows the difference between a local or procedural variable and a global variable. website is a global variable which can be called from anywhere in the script.
website2 on the other hand is a local variable. Local in the sense that it is available for use only within the function block whatis().
Initializing JS variables
Initialization is the passing of initial values to a variable. JS variables do not have to be initialized upon variable declaration. They can be created and stored either as procedural variables or global variables.
var website = "www.dissertation.com.ng";
The variable "website" in the example directly above is initialized with the value "www.dissertation.com.ng" upon declaration. But you really do not have to initialize a variable upon definition. I can also create the variable in the fashion below.
var website;
The above is a perfectly defined JS variable as well. Only that this variable does not have an initial value. We will look at scenerios where you do not want to initialize your variable upon declaration in future posts.
My next blog will be on data-types as it goes hand in hand with variables.
Enjoy!
Joseph Kayode Agbede
This comment has been removed by the author.
ReplyDelete