Validator and Debugger!
Although not required, it is considered good practice to declare variables before using them. You do this using the var statement. The only time you must use the var statement is when declaring variables that are local to a function. Local variables are those that are only within the function. At all other times, using the var statement to declare variables before their use is a recommended practice.
The following code examples are of variable declaration:var mim = "A man, a plan, a canal, Panama!"; // The value stored in mim is of string type. // The sentence in quotes, the value of which is assigned to mim, is a string literal. var ror = 3; // The value stored in ror has numeric type. var nen = true; // The value stored in nen has Boolean type. var fif = 2.718281828 // The value stored in fif has numeric type.
Some examples of valid variable names:
- The first character must be a letter (either uppercase or lowercase) or an underscore (_), or a dollar sign ($).
- Subsequent characters can be letters, numbers, underscores, or dollar signs.
- The variable name can't be a reserved word.
Some invalid variable names:
In instances in which you want to declare a variable and initialize it, but without giving it any particular value, you may assign it a special value, null.
- 99Balloons // Starts with a number.
- Smith&Wesson // Ampersand (&) is not a valid character for variable names.If you declare a variable without assigning any value to it, it exists but is undefined.var zaz = null; var notalot = 3 * zaz; // At this point, notalot becomes 0.You can declare a variable implicitly (without using var) by assigning a value to it. You cannot, however, use a variable that has never been declared at all. To do so generates an error at runtime.var godot; var waitingFor = 1 * godot; // Places the value NaN in waitingFor as godot is undefined.lel = ""; // The variable lel is declared implicitly. var aMess = vyv + zez; // Generates an error because vyv and zez don't exist.
- Look at the types of 1 and "10". The "10" is a string, the 1 is a number, so the number is coerced into a string.
- As the values on either side of the + operator are both strings, do a string concatenation. This results in "110"
- Look at the types of the values on either side of the +=. nowWhat contains a number, and "110" is a string, so convert the number to a string.
- As there are now strings on either side of the += operator, do a string concatentation. This results in "0110".
- Store this result in nowWhat.After this code is executed, the nowThen variable contains the integer 11.var nowThen = 0; nowThen += 1 + parseInt("10"); // In this case, "+=" performs addition.
Validator and Debugger!