Previous Dialogs |
ABC of JavaScript : A JavaScript Tutorial Variables |
Next Arrays |
JavaScript is a loose typing language. That means you do not have to specify the data type of a variable when you declare it, and data types are converted automatically as needed during script execution. You could use the 'var' keyword to define a variable. Using var outside a function is optional; you can declare a variable by simply assigning it a value. However, it is good style to use var, and it is necessary in functions if a global variable of the same name exists. But you can leave that out if you don't wish to work you fingers unnecessary.
var name="Binny" site="http://www.oocities.org/binnyva is my site" number=13 precise_number = 12.9904289 char = 'B'
Now the variable 'name' has a the value binny and the variable 'site' has the value 'http://www.oocities.org/binnyva'. Now we display it.
alert("My name is "+name+" and my site is "+site)
This will display a message box with this message in it
My name is Binny and my site is http://www.oocities.org/binnyva
Now try it out in our T-Box - just type the three blue lines given above into the below box and hit the 'Run' button to see your script in action.
And if you name is not Binny, change the name variable to whatever your name is. For example
var name="Clark Kent"
Variables can store numbers the same way it can store stings. For example
var a=5 var b=4 var c=a+b alert("The sum of "+a+" and "+b+" is "+c)
Copy it to the T-Box if you want to see it in action. Here two numbmers are added and their result is stored in a third variable.
Previous Dialogs |
Contents | Next Arrays |