Variables and Statements
Now a days a JavaScript is most used language in browser. This is the most loved client side programming language. Especially for those who coming from the PHP, ASP.NET or any server side language.
Code structure
Semicolon
JavaScript code consists of multiple statements. Statements are separated by a semicolon.
Example:
<script type="text/javascript"> alert("Hello"); alert("World"); </script>
In JavaScript we have to put the semicolon to separate the statements If we are writing the code in single line. Otherwise there is no need to put the semicolon at the end of the every statements to separate the statements.
Statements with the semicolon:
<script type="text/javascript"> alert("Hello");alert("World"); </script>
Above code will be run and display the two times popup windows with the message Hello and World respectively. Above code are written in a single line so we need to separate that lines with the semicolon.
Statements without the semicolon:
<script type="text/javascript"> alert("Hello") alert("World") </script>
In above code we does not put the semicolon at the end of the line because each and every line have the single line of code so it works fine and display the two times popup windows with the message Hello and World respectively.
<script type="text/javascript"> alert("Hello") alert("World") </script>
Above code is written in single line without the semicolon. There is two JavaScript statements which are written in single line without the semicolon. So it will not run and display the error in the browser’s console.
Two thoughts on the Semicolon:
In JavaScript there are two rules about the writing statements and we have to follow the one of it.
- Either put the semicolon at the end of the each and every statements.
- Write the only one statements per line without the semicolon. (It means each and every line must and must be have single JavaScript statement)
It was the good practice to put the semicolon at the end of the each and every statement of the JavaScript and write only one JavaScript statement per line.
There is two benefits of it:
- It increases the readability of the code.
- It is also helpful when we need to minified our JavaScript file for the SEO and faster loading purposes.
Variables:
Variables are helpful to store the some kind of the value, which are constantly changing or not. In simple language, A variable is a named “box”, where we can store or put the value.
Definition
First, We should declare or define the variable. It can be done in any place of code using the var directive.
var a;
We can also declare or define multiple variable at the same time. Like this:
var a, b, c;
After the we can operate the variable in any way like assigning the value to it.
var a; a = 50;
It is also possible to define the variable and assigning the value to the same variable at the same time.
var a = 50, b = 60, c = 70;
So in JavaScript we can use the var directive to declare the variable, but there is no thumb rule to use the var to declare the variable. We are also able to declare the variable without the use of the var directive, yes it is possible. But it is not recommend to do that.
x = 50; y = 60; z = x + y; alert(z);
Above code will be run successfully and display the popup window with the output of the addition of the x and y. In above code three variables are used without the declaring it that is x, y and z.
To understand the variable, you must be have the knowledge of the scope. If you did not know about the Scope please click here and read it first.
In JavaScript every variable is declared under any scope.
If you use the variable without the declaring it so it is available globally. In simple we can say that if you use the variable without declaring it so it become the property of the window object, Yes it is right variables which are not declared using var directive so it become the property of the window object and available globally.
We can use this variable anywhere in the JavaScript code. It’s scope become the global.
Scope Example: Variables Without the var directive
function WithOutVar() { a = "I am global variable"; } WithOutVar(); // Call the function to declare the variable globally. alert(a); // We can use the variable "a" here because // it is declared globally.
In above code I declare the variable “a” inside the function called WithOutVar(), But I declared the variable without the var directive so it become the global and We can use it from anywhere. And I use that variable outside of the function and I alert that variable.
Yes, as per scope definition a variable declare inside the function can not be accessed outside the function.
But JavaScript allow us to use the variable without declared it.
Because Internally JavaScript convert the undefined variables to the property of the window object. And window object is global and we can access it anywhere from the JavaScript code so that’s why we can access the undefined variables from anywhere in JavaScript code.
But it is not recommend to use the variables without declaring it. Because it can become the reason for the most dangerous errors like. Memory leakage, Security.
Scope Example: Variables With the var directive
function WithVar() { var a = "I am private variable"; } WithVar(); alert(a); // Above statement will not be execute. // due to unaccessiable variable a. // because the variable a declares private.
In above example, I declare the variable with the var directive inside the function, So now variable “a” become the private and we can not access it outside the function.
Variable names
A variable name first character must be a letter, $ or _. The second char and other characters are allowed to be digits.
Below is some valid variables name.
var $this; var _a; var a; var $; var _; var $1; var user1;
JavaScript is case sensitive.
So user1 and USER1 are two different variables.
<script type="text/javascript"> var user1 = "user1"; var USER1 = "USER1"; alert(user1); alert(USER1); </script>
Reserved words
We are not able to use the some reserved words as the variable name. Here are some reserved words which we can not use: var, function, return, class and implements and other words. Because all this words are used by the language itself.
Variables types
JavaScript have the following basic data types:
number
We can store the any number in number data type, It can be integer and non-integer like: 1, 2, 2.50 etc.
string
We also store the string or character in string data type. Like: ‘cat’, ‘dog’, ‘apple’, ‘a’, ‘b’ and ‘c’.
boolean
We can also store the two possible true and false boolean value to the variable.
object
In object we can store the any type of value in object, Like: JSON object, function or anything else.
special values
There are special values in JavaScript like: null or undefined.
Weak typing
Variables in JavaScript are weakly typed. That means two things:
- Every value has a type
- We can put a value of any type into any variable
For Example:
var name = "ABC"; // string type variable var userID = 1; // number type variable
After that we can free to reassign the variable to a value of another type.
name = false; userID = undefined;
Before assigning any value to the variable, it has undefined value.
var a; var b = undefined;
In above code two variables are define. One is “a” and another is “b”. Both the variables are declared differently but both variables have the same value undefined.
Comments:
Ever you noticed in above code “//”. “//” used for the single line comment.
In JavaScript we give the comments in two way that is single line comment (//) and another is multiple line comments (/* */).
Single Line comment example:
// This is the single line comment
Multi line comment example:
/* This is the multiple line comment: Yup, JavaScript is coolest language That are mostly used in the client side. */
Summary:
- Variables are define by the var directive anywhere in the code.
- We can also define the variable without the var directive. But it is not recommend.
- A variable can contain a value of any type.
- JavaScript provides two types of the comment that is: single line comment (//) and multiple line comment (/* */)
- Statements can be separated by the semicolon. It is not necessary to do that. If we want to write the all JavaScript statement in single line that we have to use the semicolon to separate the statement. Or We have to write the each and every statement in single line it means each line have only the one JavaScript statement.