Advanced JavaScript-JS basic syntax

Learn to share variable declarations, variable types, how to assign, change, and calculate a series of operations on them, master the use of different operators, and flexibly use arithmetic operators, comparison operators, and logical operators to manipulate variables and values.

What is a variable

What is a variable? Literally, a variable is a variable quantity; from a programming perspective, a variable is a memory used to store a certain value. We can think of variables as a box, which is used to store items. Items can be clothes, toys, fruits ... etc.

1 <script type="text/javascript"> 
2 var str='你好';
3 </script>

Give the variable a name (variable naming)

In order to distinguish boxes, we can use BOX1, BOX2 and other names to represent different boxes. BOX1 is the name of the box (that is, the name of the variable)

 

Variable naming rules:

1. Must start with a letter, underscore, or dollar sign, followed by letters, underscores, dollar signs, and numbers.

1  correct:           
 2      mysum            
 3      _mychar         
 4      $ numa1    
1  Error:
 2    6num   // No numbers at the beginning 
3    % sum // No special symbols except (_ $) at the beginning, such as (% + / etc.) 
4    sum + num // In addition to the beginning (_ $) Special symbols, such as (% + / etc.)

 2. Variable names are case sensitive, such as: A and a are two different variables.

3. It is not allowed to use JavaScript keywords and reserved words as variable names.

 Determine your existence (variable declaration)

If we want to use boxes to hold things, do we have to find the boxes first? Then in programming, this process is called declaring variables, the action of finding boxes, how to express:

1 variable declaration syntax: var variable name;    

var is equivalent to the action of finding a box. In JavaScript, it is a keyword (that is, a reserved word). The purpose of this keyword is to declare a variable and prepare a location (that is, memory) for the "variable".

1 var mynum; // Declare a variable mynum

Of course, we can find one box at a time or multiple boxes at a time, so Var can also declare multiple variables at once, separated by "," commas.

1 var num1, mun2; // Declare a variable num1

Variables can also be used without declaration, but for standardization, they need to be declared first and then used.

1 <script type="text/javascript">
2   var mya;
3   mya=5;
4   alert("mya的值是:"+mya);
5 </script>

Diversified me (variable assignment)

We can think of the variable as a box, and the box is used to store items. How to store content in the variable? We use "=" to store content in the variable, see the following statement:

1 var mynum = 5; // Declare the variable mynum and assign a value.

How to read this sentence? Assign a value to the variable mynum with a value of 5. We can also write like this:

1  var mynum; // Declare the variable mynum
 2 mynum = 5; // Assign the variable mynum

Note: The role of "=" here is to assign values ​​to variables, not equals.

The box can hold clothes, toys, fruits ... etc. In fact, variables are omnipotent containers. You can store anything in variables, such as numeric values, strings, Boolean values, etc. For example:

1  var num1 = 123; // 123 is a numeric value
 2  var num2 = "one two three"; // "one two three" is a string
 3 var num3 = true; // Boolean value true (true), false (false)

Among them, the content stored in the num1 variable is a numeric value; the content stored in the num2 variable is a character string, and the character string needs to be enclosed in a pair of quotation marks, and the content stored in the num3 variable is a Boolean value (true, false).

1 <script type="text/javascript">
2 var num1=10;
3 var num2="js";
4  document.write("num1的值是:"+num1);
5  document.write("num2的值是:"+num2);
6 </script>

Express your thoughts (expression)

Expressions are similar to the definitions in mathematics. Expressions refer to algebraic expressions that have certain values ​​and connect constants and variables with operators. An expression can contain constants or variables.

Mychar is a variable in the string expression

Num is a variable in a numeric expression

Num is a variable in Boolean expression

I have other uses (+ operator)

Operators are used to specify certain actions in JavaScript

(1) Operator / Look at the following JavaScript code.

1 sum = numa + numb;

The "=" and "+" are operators. There are many such operators in JavaScript, for example, arithmetic operators (+,-, *, /, etc.), comparison operators (<,>,> =, <=, etc.), logical operators (&&, || ,!). The "=" operator is assignment, not equal.

(2) "+" operator

Arithmetic operators are mainly used to complete tasks like addition, subtraction, multiplication and division. In JavaScript, "+" not only represents addition, but also concatenates two strings, for example:

1 mystring = "Java" + "Script"; // mystring value "JavaScript" this string

Add one and subtract one (++ and--)

In addition to the arithmetic operators (+,-, *, /), there are two very commonly used operators, adding one "++" and decrementing one "-". First look at an example:

mynum = 10; 
mynum ++; // mynum value becomes 11 
mynum--; // mynum value becomes 10 again

In the above example, mynum ++ increases the value of mynum by 1 on the original basis, and mynum-- causes mynum to subtract 1 from the original basis. In fact, it can also be written as:

1  mynum = mynum + 1; // equivalent to mynum ++
 2 mynum = mynum-1; // equivalent to mynum--

 

 Contest contest (comparison operator)

The comparison operator performs comparisons to obtain values ​​of true and false. In JavaScript, there are many such comparison operators. The meaning of these operators is as follows:

1  var a = 5; // Define the variable a and assign the value 5
 2  var b = 9; // Define the variable b and assign the value 9
 3 document.write (a < b ); // A is less than the value of b? Result True (true)
 4  document.write (a > = b); // is a greater than or equal to the value of b? The result is false (false)
 5  document.write (a! = B); // a is not equal to b Value? The result is true (true)
 6 document.write (a == b); // A is equal to the value of b? The result is false (false)

I am with you (logic and operator)

 "A> b" in mathematics is also expressed as a> b in JavaScript; "b is greater than a, b is less than c" in mathematics is "a <b <c", then you can use && in JavaScript, as follows :

1 b> a && b < c     // "&&" means "and", the pronunciation is "b is greater than a" and "b is less than c"

 "&&" is a logical AND operator. Only when the values ​​on both sides of "&&" are satisfied at the same time (at the same time is true), the entire expression value is true.

Logic and operator value table:

 Note: If A is false and A && B is false, B will not be executed; otherwise, if A is true, the value of B will determine the value of A && B.

I or you can (logical or operator)

The "||" logical OR operator is equivalent to the "or" in life. When either of the two conditions is met, the result of the "logical OR" operation is "true".

1  var a = 3;
 2  var b = 5;
 3  var c;
 4 c = b> a || a> b; // b> a is true, a> b is false, c is true

Table of logical or operator values:

Note: If A is true and A || B is true, B will not be executed; otherwise, if A is false, the value of B will determine the value of A || B.

Is not reversed (logical not operator)

"!" Is a logical non-operator, which means "not", either true or false, or false or true. It's like Xiaohua bought a cup today, Xiaoming said: "The cup is white", Xiaoliang said: "The cup is red", Xiaohua said: "Xiaoming is not telling the truth, Xiaoliang is not telling the truth" . Guess what color cup Xiaohua bought, the answer: red cup.

Logical non-operator value table:

1  var a = 3;
 2  var b = 5;
 3  var c;
 4  c =! (B> a); // b> a value is true, (b> a) value is false
 5 c =! (B < a ); // b <a value is false,! (b <a) value is true

Maintain order (operator priority)

 We all know that division, multiplication and other operators have higher priority than addition and subtraction, for example:

1  var numa = 3;
 2  var numb = 6
 3 jq = numa + 30/2-numb * 3; // the result is 0

 

 If we want to change the order of operations, we need to add parentheses to change the priority:

1  var numa = 3;
 2  var numb = 6
 3 jq = ((numa + 30) / (2-numb)) * 3; // The result is -24.75

 

Priority between operators (high to low): arithmetic operator → comparison operator → logical operator → "=" assignment symbol; if operations at the same level are performed in order from left to right, multiple brackets outward.

1  var numa = 3;
 2  var numb = 6;
 3 jq = numa + 30> 10 && numb * 3 < 2 ; // The result is false

Programming exercises

1  < script type = "text / javascript" > 
2    var a, b, sum;
 3    var   a   =  5 ;
 4    var   b   =  100 % 7 ;  
 5    sum = a > b && a * b >  0 ;
 6    document.write ( " I think the value of a is: "  +  5   +  " The value of b is: "  +  6  +  " The value of sum is: " +  false + " <br/> " );
 7    document.write ( "The answer is, after the first round of calculation, a is: " +  5  + " ; b is: " + 6  + " ; the first calculation of sum is : " +  False  + " <br/> " );
 8  
9    sum = (( ++ a) +  3 ) / ( 2  - ( - b)) *  3 ;  
 10    document.write ("After another calculation, I think the value of a is: "  +   6   +  " The value of b is: "  +   5  +  " The value of sum is: "  +  ' -9 '  + " <br/> " ); 
 11    document .write ( "The answer is, after the second round of calculation, a is: "  +  7  +  " ; b is: "  +  4  + " ; the second calculation of sum is: " +  ' -15 '  + " , the type of sum It has also changed. " );
12  
13  </script>

Guess you like

Origin www.cnblogs.com/dhnblog/p/12694170.html