For JavaScript, variables also have types. Let's look at the following small example:
This is the tested html
< P >
< Input Type = "Text" Name = "Txtval" />
< Input Type = "Button" Name = "Btntest" Value = "Test" Onclick = "Test ();" />
</ P >
< Span ID = "Lbltest" > Test Results </ Span >
Let's take a look at the test method.
Function Test ()
{
VaR Val;
VaR Result;
VaR Str = '';
Val = Document.all.txt Val. value;
Str = 'Test result < BR > ';
Str + = ' 1 + Val = ';
Result = 1 + Val;
Str + = Result;
Str + = ' < BR > 1 + Parseint (VAL) = ';
Result = 1 + Parseint (VAL );
Str + = Result;
Document. All. lbltest. innerhtml = STR;
}
Result Display
Test Results
1 + val = 11
1 + parseint (VAL) = 2
Obviously, Val is a string. Isn't JavaScript automatically converted? Yes! This is for sure. In the above example, if you add Val ++ before all addition operations, the results will be different. New Test Method
Function Test ()
{
VaR Val;
VaR Result;
VaR Str = '';
Val = Document.all.txt Val. value;
Str = 'Test result ';
Val ++ ;
Str + = ' < BR > Val ++ Post Val = '
Str + = Val;
Str + = ' < BR > 1 + Val = ';
Result = 1 + Val;
Str + = Result;
Str + = ' < BR > 1 + Parseint (VAL) = ';
Result = 1 + Parseint (VAL );
Str + = Result;
Document. All. lbltest. innerhtml = STR;
}
New results
Test Results
Val ++ post val = 2
1 + val = 3
1 + parseint (VAL) = 3
sometimes the type conversion is obviously executed to prevent errors.