Javascript Variables Declaration

Declaring Variables in Javascript

Writing JavaScript variables are very flexible, and not too complicated and strict, so we do not receive error messages too frequently when running the program. For example in JavaScript variable declarations we can do in two ways:

  • Explicit: by writing the keyword var followed by a variable name and value of the variable:
var test = "hello"
  • Implicit: by writing directly followed by the name of the variable and the value of the variable:
test = "hello"

Navigator will automatically treat the statement as a declaration of a variable. In older navigator versions may occur a case where the navigator does not recognize the implicit variable declaration, it is advisable to use the explicit way of writing JavaScript programs.
The following are examples of variable declaration with both ways.
<script Language="JavaScript">
<! -
var MyVariable;
var MyVariable2 = 3;
MyVariable = 2;
document.write (MyVariable * MyVariable2);
// ->


Post a Comment