NumberOfDaysLeft = EndDate – TodaysDate.
You use variables to store, retrieve, and manipulate values that appear in your code. Try to give your variables meaningful names to make it easy for other people to understand what your code does.
The first time a variable appears in your script is its declaration. The first mention of the variable sets it up in memory, so you can refer to it later on in your script. You should declare variables before using them. You do this using the
// A single declaration. var count; // Multiple declarations with a single var keyword. var count, amount, level; // Variable declaration and initialization in one statement. var count = 0, amount = 100;
If you do not initialize your variable in the
var statement, it automatically takes on the value
The first character must be an ASCII letter (either uppercase or lowercase), or an underscore (_) character. Note that a number cannot be used as the first character.
Subsequent characters must be letters, numbers, or underscores (_).
The variable name must not be a reserved word.
Here are some examples of valid variable names:
_pagecount Part9 Number_Items
Here are some examples of invalid variable names:
// Cannot begin with a number. 99Balloons // The ampersand (&) character is not a valid character for variable names. Alpha&Beta
When you want to declare a variable and initialize it, but do not want to give it any particular value, assign it the value
null. Here is an example.
var bestAge = null; var muchTooOld = 3 * bestAge; // muchTooOld has the value 0.
If you declare a variable without assigning a value to it, it has the value
undefined. Here is an example.
var currentCount; // finalCount has the value NaN because currentCount is undefined. var finalCount = 1 * currentCount;
null value behaves like the number 0, while
undefined behaves like the special value
NaN (Not a Number). If you compare a
null value and an
undefined value, they are equal.
You can declare a variable without using the
var keyword in the declaration, and assign a value to it. This is an implicit declaration.
// The variable noStringAtAll is declared implicitly. noStringAtAll = "";
You cannot use a variable that has never been declared.
// Error. Length and width do not yet exist. var area = length * width;
If you add a number and a string, the number is coerced to a string.
If you add a Boolean and a string, the Boolean is coerced to a string.
If you add a number and a Boolean, the Boolean is coerced to a number.
In the following example, a number added to a string results in a string.
var x = 2000; var y = "Hello"; // The number is coerced to a string. x = x + y; document.write(x); // Output: // 2000Hello
Strings are automatically converted to equivalent numbers for comparison purposes. To explicitly convert a string to an integer, use the parseInt function. To explicitly convert a string to a number, use the parseFloat function.