Everyone knows the code for class assignments as follows:
<script language="javascript">
<!--
Code
//-->
</script>
Here is a little more content and you can also tell readers that this is javascript code, which is not supported by this browser.
<noscript>
Show this sentence when the browser does not support it
</noscript>
Today we mainly learn about the following content:
1. JavaScript variables
2. JavaScript expressions and operators
Var can be used to declare variables with the name specified for the variable, and the variable type can be determined by assigning values to the variables. Since JavaScript adopts a weak type style, the data types are not strictly required, and it will automatically convert as needed during the execution of the program.
For string variables, you can use "variable name.length" to get the length of the string in the variable, such as
var name;
name="javascript";
Then the value is 10.
If you create multiple variables in a row, remember to use commas to separate the variable names. Each statement is separated by a semicolon. (It is a good habit to use semicolons. Everyone should try to develop the habit of adding semicolons when studying)
Type conversion: JavaScript allows changing the type of variables in a program, the two most common type conversion characters Number and String.
Number(x) is a character type value -> numerical value type. String is the opposite. Compared to automatic type conversion in javascript, this conversion can be converted into a cast. (Casting type conversion needs to be used in javascript 1.2 and above)
Naming of variables:
1. It must start with a letter or an underscore, with alphanumeric and or underscore in the middle. No other symbols such as spaces, +, and - can be used.
As a hyphen, variable names cannot have spaces, (+), (-), (), or other symbols.
2. You cannot use keywords in JavaScript as variables.
(Javascript variable names are case sensitive, name and Name are different.)
There is another important thing for variables - that is, the scope of variables. There are also global and local variables in JavaScript. Global variables are defined outside all function bodies, and their scope of action is the entire function; while local variables are defined within the function body, only for this function, but for other functions, they are invisible.
example:
<script>
var myStr = "I am a global variable";
function getStr1(){
var myStr = "I am a local variable";
alert (myStr);
}
function getStr2(){
alert (myStr);
}
getStr1();
getStr2();
//The test will pop up two prompt boxes. The first prompt "I am a local variable" and the second prompt "I am a global variable"
</script>
Expressions: After defining variables, they can be assigned, changed, and calculated. This process is usually completed by expressions. It can be said that it is a collection of variables, constants, Booleans and operators. Therefore, expressions can be divided into arithmetic expressions, string expressions, assignment expressions, and Boolean expressions.
Operator:
1. Arithmetic operators: + (add) , - (subtraction), * (multiple), / (division), % (modulo) - (inverse),
++ (increment 1), -- (increment 1).
Example: 11%2=1; If x=2++x+4=7 x++4=6
(++x is to execute the statement and add 1 first, x++ is to add 1 by itself after executing the statement.
example:
<script>
var i=0, j=0;
alert(i++ + " " + ++j + " " + i);
// Output "0 1 1", it can be seen that i++ first outputs i and then performs operations, while ++j first performs self-add operations on j and then outputs the value of j
</script>
)
2. Comparison operators: <(less than), >(greater than), <=(less than or equal to), >=(greater than or equal to), ==(equivalent), !=(not equal to)
(The basic operation process is to first compare its operands, and then return a true or False value.)
3. Logical operators: ! (inverse), &= (assigned afterwards), & (logical and), = (or assigned afterwards), (logical or),
^= (assigned after XOR), ^ (logical XOR), ?: (three-point operator), (or), && (and)
==(equal to), =(not equal to).
4. String operator: only + ("my"+"javascript" result is equal to "my javascript")
5. Assignment operator: that is, =, assign the value on the right to the variable on the left.
6. Conditional operator: (?:)
Example: status=(age>=18)?"adult":"child"; If it is greater than 18, the value of the expression is adult.
() operator: the type used to return variable or data.
<script language="javascript">
<!--
Code
//-->
</script>
Here is a little more content and you can also tell readers that this is javascript code, which is not supported by this browser.
<noscript>
Show this sentence when the browser does not support it
</noscript>
Today we mainly learn about the following content:
1. JavaScript variables
2. JavaScript expressions and operators
Var can be used to declare variables with the name specified for the variable, and the variable type can be determined by assigning values to the variables. Since JavaScript adopts a weak type style, the data types are not strictly required, and it will automatically convert as needed during the execution of the program.
For string variables, you can use "variable name.length" to get the length of the string in the variable, such as
var name;
name="javascript";
Then the value is 10.
If you create multiple variables in a row, remember to use commas to separate the variable names. Each statement is separated by a semicolon. (It is a good habit to use semicolons. Everyone should try to develop the habit of adding semicolons when studying)
Type conversion: JavaScript allows changing the type of variables in a program, the two most common type conversion characters Number and String.
Number(x) is a character type value -> numerical value type. String is the opposite. Compared to automatic type conversion in javascript, this conversion can be converted into a cast. (Casting type conversion needs to be used in javascript 1.2 and above)
Naming of variables:
1. It must start with a letter or an underscore, with alphanumeric and or underscore in the middle. No other symbols such as spaces, +, and - can be used.
As a hyphen, variable names cannot have spaces, (+), (-), (), or other symbols.
2. You cannot use keywords in JavaScript as variables.
(Javascript variable names are case sensitive, name and Name are different.)
There is another important thing for variables - that is, the scope of variables. There are also global and local variables in JavaScript. Global variables are defined outside all function bodies, and their scope of action is the entire function; while local variables are defined within the function body, only for this function, but for other functions, they are invisible.
example:
<script>
var myStr = "I am a global variable";
function getStr1(){
var myStr = "I am a local variable";
alert (myStr);
}
function getStr2(){
alert (myStr);
}
getStr1();
getStr2();
//The test will pop up two prompt boxes. The first prompt "I am a local variable" and the second prompt "I am a global variable"
</script>
Expressions: After defining variables, they can be assigned, changed, and calculated. This process is usually completed by expressions. It can be said that it is a collection of variables, constants, Booleans and operators. Therefore, expressions can be divided into arithmetic expressions, string expressions, assignment expressions, and Boolean expressions.
Operator:
1. Arithmetic operators: + (add) , - (subtraction), * (multiple), / (division), % (modulo) - (inverse),
++ (increment 1), -- (increment 1).
Example: 11%2=1; If x=2++x+4=7 x++4=6
(++x is to execute the statement and add 1 first, x++ is to add 1 by itself after executing the statement.
example:
<script>
var i=0, j=0;
alert(i++ + " " + ++j + " " + i);
// Output "0 1 1", it can be seen that i++ first outputs i and then performs operations, while ++j first performs self-add operations on j and then outputs the value of j
</script>
)
2. Comparison operators: <(less than), >(greater than), <=(less than or equal to), >=(greater than or equal to), ==(equivalent), !=(not equal to)
(The basic operation process is to first compare its operands, and then return a true or False value.)
3. Logical operators: ! (inverse), &= (assigned afterwards), & (logical and), = (or assigned afterwards), (logical or),
^= (assigned after XOR), ^ (logical XOR), ?: (three-point operator), (or), && (and)
==(equal to), =(not equal to).
4. String operator: only + ("my"+"javascript" result is equal to "my javascript")
5. Assignment operator: that is, =, assign the value on the right to the variable on the left.
6. Conditional operator: (?:)
Example: status=(age>=18)?"adult":"child"; If it is greater than 18, the value of the expression is adult.
() operator: the type used to return variable or data.