Variables (JavaScript)
InJavaScript, as in any programming language, a piece of data is used to quantify a concept.
In JavaScript, a variable is the name you give that concept; it represents the value at a given instant. When you use the variable, you really mean the data it represents. Here is an example:
NumberOfDaysLeft = EndDate – TodaysDate;
In a mechanical sense, you use variables to store, retrieve, and manipulate all the different values that appear in your scripts. Always create a meaningful variable name; that makes it easy for humans to understand what your scripts do.
The first time a variable appears in your script is its declaration. This first mention of the variable sets it up in memory so you can refer to it later on in your script. Always declare variables before using them. You do this using the var keyword.
// 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 JavaScript value undefined. Although it is unsafe to do so, it is legalJavaScript syntax to omit the var keyword from your declaration statement. When you do, theJavaScript interpreter gives the variable global scope visibility. When you declare a variable at the procedure level though, you do not want it to be visible at the global scope.
A variable name is an identifier. In JavaScript, identifiers are used to:
name variables,
name functions,
provide labels for loops.
JavaScript is a case-sensitive language. This means a variable name such as myCounter is different than the variable name MYCounter. Variable names can be of any length. The rules for creating legal variable names are as follows:
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 theJavaScript 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 exists, but has theJavaScript value undefined. Here is an example.
var currentCount; // finalCount has the value NaN because currentCount is undefined. var finalCount = 1 * currentCount;
Note that the main difference between null and undefined in JavaScript is that null behaves like the number 0, while undefined behaves like the special value NaN (Not a Number). A null value and an undefined value will always compare to be 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;
JavaScript is a loosely typed language. This means its variables have no predetermined type (as opposed to strongly typed languages like C++). Instead, JavaScript variables have a type that corresponds to the type of value they contain. A benefit of this behavior is that it provides you with the flexibility to treat a value as if it were of another type.
In JavaScript, you can perform operations on values of differing types without fear that the JavaScript interpreter will raise an exception. Instead, theJavaScript interpreter automatically changes (coerces) one of the data types to that of the other, then performs the operation. For example:
Operation | Result |
---|---|
Add a number and a string | The number is coerced into a string. |
Add a Boolean and a string | The Boolean is coerced into a string. |
Add a number and a Boolean | The Boolean is coerced into a number. |
Consider the following example.
// A number. var x = 2000; // A string. var y = "Hello"; // The number is coerced into a string. x = x + y; // Outputs 2000Hello. document.write(x);
Strings are automatically converted to equivalent numbers for comparison purposes, but are left as strings for addition (concatenation).
To explicitly convert a string to an integer, use the parseInt function. To explicitly convert a string to a number, use the parseFloat function.
Tidak ada komentar:
Posting Komentar