Js|jscript| variable
In any programming language, a piece of data is used to quantify a concept.
How old are for you?
In Jscript, a variable is the name of the concept; it represents a value that gives an instant. When the variable is used, the data it represents is actually used. To show Examples:
Numberofdaysleft = enddate–todaysdate;
The mechanical understanding is to use variables to store, get, and manipulate all the different values that appear in the script. Create meaningful variable names, and make it easier for others to understand the script.
Variable declaration
The first occurrence of a variable in a script is in the declaration. Variables are set in memory the first time they are used, so that they can be referenced later in the script. Declare the variables before using them. You can use the VAR keyword to declare a variable.
var count; A single declaration.
var count, amount, level; Multiple declarations declared with a single var keyword.
var count = 0, amount = 100; Declaration and initialization of variables in a single statement.
If there are no initialization variables in the VAR statement, the variable automatically takes the JScript value undefined. Although it is not secure, the VAR keyword is a legitimate JScript syntax in declaring statements. At this point, the JScript interpreter gives the variable the visibility of the global scope. When declaring a variable at the procedure level, it cannot be used in a global scope, in which case the variable declaration must be in the var keyword.
Variable naming
The variable name is an identifier. In Jscript, with an identifier:
Named variables,
Named functions,
Give the label for the Loop.
JScript is a case-sensitive language. Therefore, variable name myCounter and variable name myCounter are not the same. The name of a variable can be any length. Creating a valid variable name should follow the following rules:
The first character must be an ASCII letter (either uppercase or lowercase), or an underscore (_). Note that the first character cannot be a number.
Subsequent characters must be letters, numbers, or underscores.
The variable name must not be a reserved word.
Some examples of the names of legal variables are given below:
_pagecount
Part9
Number_items
Some examples of invalid variable names are given below:
99Balloons//cannot begin with a number.
The Smith&wesson//ampersand (&) character is not valid for variable names.
When you want to declare a variable and initialize it, but do not want to specify any special values, you can assign a value of a JScript value of NULL. Examples are shown below.
var bestage = null;
var muchtooold = 3 * bestage; The value of Muchtooold is 0.
If a variable is declared without assigning a value to it, the variable exists with an undefined Jscript value. Examples are shown below.
var Currentcount;
var finalcount = 1 * currentcount; The value of the Finalcount is NaN because Currentcount is undefined.
Note that the main difference between null and undefined in JScript is null operations like the number 0, whereas undefined operates like a special value NaN (not a number). Comparisons of null values and undefined values are always equal.
You can declare a variable without the var keyword and assign a value. This is an implicit declaration.
Nostringatall = ""; Implicit declaration of variable Nostringatall.
You cannot use a variable that has not been declared.
var volume = length * width; Error-length and width do not exist.
Forced conversions
The JSCRIPT interpreter can evaluate the action item's data type in an expression when it is the same. If an expression attempts to perform an operation on two different data types (such as a number and another string) without casting, an error result is generated. In Jscript, however, the situation is different.
JScript is a free type of language. Its variables do not have a predetermined type (relative to a strongly typed language, such as C + +). In contrast, the types of JScript variables correspond to the types of the values they contain. The advantage of this operation is that the value can be treated as another type.
In JScript, you can perform operations on different types of values without worrying about the JSCRIPT interpreter producing an exception. Instead, the JScript interpreter automatically changes one of the data types (casts) to another data type, and then performs the operation. For example:
Operation result
Numeric values are coerced into strings by adding values to the string.
The Boolean value is added to the string to cast the Boolean value to a string.
The value is added to the Boolean value to cast the Boolean value to the numeric value.
Consider the following example.
var x = 2000; A number.
var y = "Hello"; A string.
x = x + y; Casts a number to a string.
document.write (x); Output 2000Hello.
To explicitly convert a string to an integer, use the parseint method. To explicitly convert a string to a number, use the Parsefloat method. Note that strings are automatically converted to equal numbers when the size is compared, but the addition (join) operation is reserved as a string.