Organize JavaScript basic grammar learn notes _javascript tips

Source: Internet
Author: User
Tags arithmetic arithmetic operators logical operators ming numeric value ticket

1, what is a variable
The variable is a variable amount;
Programming Angle: A variable is a memory that is used to store some type of value. We can think of a variable as a box, a box for storing items, items can be clothes, toys, fruits ... Wait

2. Express your thoughts (expressions)
Expressions are similar to definitions in mathematics, and expressions refer to algebraic expressions that have certain values and are connected by operators to constants and variables. An expression can contain constants or variables.

string expression: " I "+" Love "+" You "+ MyChar//write string expression, value is string.

numeric expression:num + 5*32 + 2.5//write a numeric expression with a numeric value.

Boolean expression:2>3 num==5 num<60//write Boolean true or flase expression

Xiaoming has 10 Yuan, bought a Ben, spent 5 yuan, Xiao Hong said: "Your remaining money plus my 6 yuan, you can buy a pencil case."

 <script type= "Text/javascript" >
  var num1 = 10-5;//Calculate how much Xiao Ming left the money
  var num2 = num1+6;//small red flowers how much money to buy a pencil box
 document.write ("Xiao Ming has left:" +num1+ "Yuan" + "<br>");
 document.write ("Small red Flowers:" +num2+ "Yuan buy a pencil case");
 </script>

3, + number operator
An operator is a symbol used to specify a certain action in JavaScript.
(1) operator
Sun = numa + numb;
where "=" and "+" are all operators.
Arithmetic operators (+ 、-、 *,/etc.)
Comparison operators (<, >, >=, <=, etc.)
logical operators (&&, | |,!) )。
Note: the "=" operator is assigned, not equal to.
(2) "+" operator
In JavaScript, "+" is not just for addition, it can also connect two strings.

Copy Code code as follows:
mystring = "Java" + "Script";//mystring value "JavaScript" this string

4, from add one, self minus one (+ + and--)
In addition to the arithmetic operator (+ 、-、 *,/), there are two very common operators, since add a "+ +", since minus one "--". First, let's look at an example:

Mynum = ten;
mynum++; The value of the mynum becomes one
mynum--, and the//mynum value is changed back to 10.

In the example above, mynum++ makes the Mynum value on the original basis of the 1,mynum--so that mynum on the original basis minus 1, but also can be written as:

 Mynum = mynum + 1;//equal to mynum++
 

5. Comparison operator
We first to do math problems, math test results, xiaoming Test 90 points, small red Test 95 points, ask who test scores high?
A: Because of "> 90", the results of the small red test are high.
Which is greater than the ">" is the comparison operator, small red test results and xiaoming test results is the operand, and is two operands.
That is, two operands are compared by comparison operators, and the values are true (TRUE) and False (false).
The meaning of the operator:
< less than
> Greater than
<= less than or equal to
>= is greater than or equal to
= = equals
!= is not equal to

 var a = 5;//defines a variable, assigns a value of 5
 var b = 9;//defines a B variable, assigns a value of 9
 document.write (a<b),//a a value less than B? The result is true (true)
 document.write (A>=B); is a value greater than or equal to B? The result is False (false)
 document.write (a!=b); Is//a not equal to B? The result is true (true)
 document.write (a==b)//a is equal to B's value? The result is False (false)

Equality operator = = does not mean strict equality. For example: What would be the result of comparing false to an empty string?

 var a = false;
 var B = "";
 if (a = = b) {
  alert ("a equals B");
 }
 The evaluated result of this conditional statement is true. Because equality operator = = = NULL string is considered to be the same meaning of false. To make a rigorous comparison, use another equal sign (= = =). This congruent operator performs a rigorous comparison, comparing not only the value, but also the type of the variable:
 var a = false;
 var B = "";
 if (A = = = B) {
  alert ("a equals B");
 }

This time, the conditional expression evaluates to False. Boolean and string are not a type, even if you think false has the same meaning as an empty string.

The same is true for unequal operator!=. If you want to compare strict inequality, you should use!==.

6. Logic and operator
In mathematics "A>b", JavaScript also means "a>b"; in mathematics, "B is greater than a,b is less than C" is "A<b<c", which is expressed in && in JavaScript.
b>a && b<c//"&&" is and means, read "B is greater than a" and "B is less than C"
as we participate in the college entrance examination, before entering the examination room, must show the admission ticket and ID card, both indispensable, otherwise can not take the examination, said as follows:

 If (have a ticket && have ID card) 
 {
 Conduct examination exam
 }

"&&" is the logic and operator, only "&&" both sides of the value is satisfied (also true), the entire expression value is true.
Table of logical AND operator values:

Note: If A is false, a && B is false and will not execute B; Conversely, if a is true, the value of a && B is determined by the value of B.

7, I or you can (logic or operator)
"||" Logic or operator, equivalent to "or" in life, when any one of two conditions is satisfied, "logic or" the result of the operation is "true"
Table of logical OR operator values:

Note: If A is true, a | | B is true, not in the execution of B; Conversely, if a is false, the value of B should be determined by a | | Value of B

<script type= "Text/javascript" >
 var numa,numb,jq1;
 numa=50;
 numb=55;
 jq1= numa>numb| | Numa==numb;
 document.write ("JQ1 value is:" +jq1+ "<br>")
</script>

8, topsy-turvy (logical non-operator)
"!" Is the logic of the non-operator, that is, "not" meaning, not true or false, not false is true.
Logical non-operator value table:

Cases:

 var a=3;
var b=5;
var C;
c=! (B>a); B>a value is true,! (B>a) value is False
c=! ( B<A); The B<a value is FALSE! (b<a) value is True
<script type= "Text/javascript" >
  var numa,numb,jq1;
  numa=60;
  numb=70;
  jq1=! (NUMA<NUMB);
  document.write ("JQ1 value is:" +jq1+ "<br>")//output value JQ1 value is: false

Keep order (operator priority)
Example One:

var numa=3;

 var numb=6
jq= Numa + 30/2-numb * 3;//Result 0

Case TWO:

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

Precedence between operators (high to Low):
arithmetic operator → comparison operator → logical operator → "=" assignment symbol
If the operation of the sibling is in Left-to-right order, the multiple brackets are from the inside out.

 var numa=3;
 var numb=6; jq= Numa + >10 && numb * 3<2;
  The result is false <script type= "Text/javascript" > var numa,numb,jq1;
  numa=5;
  numb=2;
  Jq1=numa + >10 && numb * 3<20; Jq2= (Numa +)/(7-NUMB) * 3 document.write ("JQ1 value is:" +jq1+ "<br>");//jq1 value is: true document.write ("Jq2 value is:" +jq2
  ); The value of//JQ2 is: </script> <script type= "Text/javascript" > var a,b,sum;
  var a = 5; 
  var B = 100%7;
  sum = a > B && a*b > 0;
  document.write ("I think the value of a is:" + 5 + "B" value is: "+ 2 +" The value of sum is: "+ true+" <br/> ");
 
  document.write (The answer is, after the first round of calculation, A is: "+ A +"; B is: "+b +"; The first calculation sum is: "+ sum + <br/>"); 
  sum = ((++a) + 3)/(2-(--b)) * 3; 
  document.write ("Once again, I think the value of a is:" + 6 + "B" value is: "+ 1 +" The value of sum is: "+ +" <br/> "); document.write (The answer is, after the second round of calculation, A is: "+ A +"; B is: "+ B +"; The second calculation sum is: "+ sum +", the type of sum is also changed.)
"); </script> 

  Above is all about JavaScript's basic grammar, and I hope it will help you in your study.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.