JavaScript Variables
All programming languages has one common characteristics is variables. Variables are useful to hold the data value, which is constantly changing.
JavaScript allows only three primitive data types that are:-
- String: String is collection of characters. We can use the “” (double quote) or ” (single quote) to create the string in the JavaScript, In JavaScript double quote (“”) and single quote (”) both are same. If you want to declare the single character than we can also double quote (“”) or single quote (”) also.
- Number: JavaScript also provides the number datatype, so we can able to declare the number like integer, long, double, float.
- Boolean: JavaScript also supports the boolean datatype, So we can save the true and false in the variables as well.
JavaScript also supports the composite data type know as the object. JavaScript also have two unique data types that is the null and undefined.
How to define the variables in the JavaScript ?
With var
For declaring the variables in JavaScript we can use the var keyword. So before using any variables and declaring variables in JavaScript we have to declare the variables using the var keyword.
Example:
var a = 10; var b = 10; var addition, subtraction, multiplication, division; addition = a + b; //20 subtraction = a - b; //0 multiplication = a * b; //100 division = a / b; //1
Without var
JavaScript is loosely type language, So its not necessary to declare the variables before use it in JavaScript. We can freely use any variables with out the declaring it.
But If you want to use the variables without declaring it, than first you have to assign some value to the variables. Otherwise JavaScript throw the error say the “this variables is not defined.”
Example:
a = 10; b = 10; addition = a + b; //20 subtraction = a - b; //0 multiplication = a * b; //100 division = a / b; //1
Note: We strongly recommended you to use the var keyword to declare the variables before to use it. If you use the var keyword to declare the variables so the variables will be declared in the any function scope and it will be destroyed automatically when the execution of the function is over.
But when you use the variables without the declaring than it will be available to the global scope and also we can say the variables are indirectly declaring in the page scope and it is automatically become the part the of the page scope so it will be never destroyed from the memory till the page is displaying the browser, and it become the leads to the memory leakage.
Variables which is used without the declaring, it will automatically becomes the property of the window object, so that variables are accessible from anywhere in the page. So we request you to never use the variables without declaring it.
Loosely typed variables:
Loosely typed variables means the variables first declare as the string and holding the string, and after that it can also hold any type of the value like number, boolean or object also.
Example:
var name = 1; name = "jhon"; name = 99.9; name = false; name = null;
Happy coding!!!
🙂