Monday, May 10, 2010

How to declare JQuery variable & What is JQuery $ sign symbol

In my first article i have discussed on how to start JQuery in your asp.net application. Here in this article i will explain how one can declare simple JQuery variable as well as object type Jquery variable also i will explain what does it mean by Jquery $ sign.

At first What is variable:
In programming, a variable is a value that can change, depending on conditions or on information passed to the program. Typically, a program consists of instructions that tell the computer what to do and data that the program uses when it is running. The data consists of constants or fixed values that never change and variable values (which are usually initialized to "0" or some default value because the actual values will be supplied by a program's user).

Now declaring a variable in JQuery:
Declarinag a variable in JQuery almost like javascript declaration. See the below example which will use simple variables:
<script language="javascript">
            $(document).ready(function() {          
            var a=10;
            var b=20;
            var c=a+b;
            alert(c);
            });
    </script>    


Now what is Jquery $ sign:
To initialize an object JQuery uses $ symbol. You can use this symbol to pass selector, function, string etc to execute or implement some functionality. You can replace the symbol $ by jquery word which will give you same result. Basically its a notation you have to use to pass a selector.

So the best way of declaring a variable in JQuery based on my first example is:
<script language="javascript">
            $(document).ready(function() {          
            var $a=10;
            var $b=20;
            var $c=$a+$b;
            alert($c);
            });
    </script>    

Object Declaration:
JavaScript supports Object concept very well. We can declare object variable same as javascript. You can create an object using the object literal in JQuery as follows:
<script language="javascript">
        $(document).ready(function() {          
            var Developer = {
               $Name: "Shawpnendu",
               $Experience: 6
            };
            
            //You can write and read properties of an object using the dot notation as follows:
            
            // Read Properties
            
            alert(Developer.$Name)
            
            // Write or set properties
            Developer.$Name="Bikash"
            alert(Developer.$Name)            
        });
    </script>    

Hope this will help you.

No comments:

Post a Comment

Write your Comment: