1. What is a variable
Literal meaning: A variable is a variable amount;
Programming perspective: A variable is a memory used to store some/certain values. We can think of a variable as a box, which is used to store items. The items can be clothes, toys, fruits, etc.
2. Express your thoughts (expression)
An expression is similar to the definition in mathematics. An expression refers to an algebraic expression that has a certain value and uses operators to connect constants and variables. An expression can contain constants or variables.
String expression: "I" "love" "you" mychar //Write a string expression, the value is a string.
Numerical expression: num 5*32 2.5 //Write a numeric expression, the value is a numeric value.
Boolean expression: 2>3 num==5 num<60 //Write an expression of Boolean value true or false
Xiao Ming had 10 yuan and bought a notebook for 5 yuan. Xiaohong said: "With your remaining money and my 6 yuan, you can buy a pencil case."
<script type="text/javascript"> var num1 = 10-5;//计算小明剩下多少钱 var num2 = num1+6;//小红花多少钱买铅笔盒 document.write("小明还剩:"+num1+"元"+"<br>"); document.write("小红花:"+num2+"元买个铅笔盒"); </script>
3. Sign operator
Operators are symbols used to specify certain actions in JavaScript.
(1) Operator
sun = numa numb;
Among them, "=" and " " are both operators.
Arithmetic operators ( , -, *, /, etc.)
Comparison operators (<, >, >=, <=, etc.)
Logical operators (&&, ||, !).
Note: The "=" operator is assignment, not equal.
(2) " " operator
In JavaScript, " " not only represents addition, but can also concatenate two strings.
4. Add one to yourself and decrement one to yourself (and - -)
In addition to the arithmetic operators (, -, *, /), there are two very commonly used operators, increment by " "; decrement by "--". First let’s look at an example:
mynum = 10; mynum++; //mynum的值变为11 mynum--; //mynum的值又变回10
In the above example, mynum increases the value of mynum by 1 based on the original value, and mynum-- causes mynum to decrease by 1 based on the original value. In fact, it can also be written as:
mynum = mynum + 1;//等同于mynum++ mynum = mynum - 1;//等同于mynum--
5. Comparison operators
Let’s do a math question first. Among the math test scores, Xiao Ming scored 90 points and Xiao Hong scored 95 points. Who got the higher score?
Answer: Because "95 > 90", Xiaohong's test score is high.
The greater than sign ">" is the comparison operator, and Xiaohong's test scores and Xiaoming's test scores are the operands, and they are two operands.
That is to say, the two operands are compared through the comparison operator, and the values obtained by are true (true) and false (false).
Operator meaning:
< less than
> Greater than
<= less than or equal to
>= greater than or equal to
== equals
!= is not equal to
var a = 5;//定义a变量,赋值为5 var b = 9; //定义b变量,赋值为9 document.write (a<b); //a小于b的值吗? 结果是真(true) document.write (a>=b); //a大于或等于b的值吗? 结果是假(false) document.write (a!=b); //a不等于b的值吗? 结果是真(true) document.write (a==b); //a等于b的值吗? 结果是假(false)
The equality operator == does not mean strict equality. For example: What will be the result of comparing false with an empty string?
var a = false; var b = ""; if(a == b){ alert("a equals b"); } //这个条件语句的求值结果是true。因为相等操作符==认为空字符串于false的含义相同。要进行严格比较,就要使用另一种等号(===)。这个全等操作符会执行严格的比较,不仅比较值,而且会比较变量的类型: var a = false; var b = ""; if (a === b){ alert("a equals b"); }
This time, the evaluation result of the conditional expression is false. Because even though false has the same meaning as the empty string, Boolean and String are not the same type.
The same is true for the inequality operator !=. If you want strict inequality, use !==.
6. Logical AND operator
In mathematics, "a>b" also means "a>b" in JavaScript; in mathematics, "b is greater than a, b is less than c" is "a
b>a && b
if(有准考证 &&有身份证) { 进行考场考试 }
“&&”是逻辑与操作符,只有“&&”两边值同时满足(同时为真),整个表达式值才为真。
逻辑与操作符值表:
注意: 如果A为假,A && B为假,不会在执行B; 反之,如果A为真,要由 B 的值来决定 A && B 的值。
7、我或你都可以 (逻辑或操作符)
"||"逻辑或操作符,相当于生活中的“或者”,当两个条件中有任一个条件满足,“逻辑或”的运算结果就为“真”
逻辑或操作符值表:
注意: 如果A为真,A || B为真,不会在执行B; 反之,如果A为假,要由 B 的值来决定 A || B 的值
<script type="text/javascript"> var numa,numb,jq1; numa=50; numb=55; jq1= numa>numb||numa==numb; document.write("jq1的值是:"+jq1+"<br>") </script>
8、是非颠倒(逻辑非操作符)
"!"是逻辑非操作符,也就是"不是"的意思,非真即假,非假即真。
逻辑非操作符值表:
例:
var a=3; var b=5; var c; c=!(b>a); // b>a值是true,! (b>a)值是false c=!(b<a); // b<a值是false, ! (b<a)值是true
<script type="text/javascript"> var numa,numb,jq1; numa=60; numb=70; jq1=!(numa<numb); document.write("jq1的值是:"+jq1+"<br>")//输出值jq1的值是:false </script>
保持先后顺序(操作符优先级)
例一:
var numa=3; var numb=6 jq= numa + 30 / 2 - numb * 3; // 结果为0
例二:
var numa=3; var numb=6 jq= ((numa + 30) / (2 - numb)) * 3; //结果是-24.75
操作符之间的优先级(高到低):
算术操作符 → 比较操作符 → 逻辑操作符 → "="赋值符号
如果同级的运算是按从左到右次序进行,多层括号由里向外。
var numa=3; var numb=6; jq= numa + 30 >10 && numb * 3<2; //结果为false <script type="text/javascript"> var numa,numb,jq1; numa=5; numb=2; jq1=numa + 30 >10 && numb * 3<20; jq2=((numa + 30) / (7-numb)) * 3 document.write("jq1的值是:"+jq1+"<br>");//jq1的值是:true document.write("jq2的值是:"+jq2);//jq2的值是:21 </script> <script type="text/javascript"> var a,b,sum; var a = 5; var b = 100%7; sum = a > b && a*b > 0 ; document.write( "我认为 a 的值是:" + 5 + " b的值是:" + 2 + "sum 的值是:" + true+"<br/>"); document.write( "答案是,第一轮计算后,a 为:"+ a +";b为:"+b +";第一次计算sum为:"+ sum +"<br/>"); sum = ( (++a) + 3 ) / (2 - (--b) ) * 3; document.write( "再一次计算后,我认为 a 的值是:" + 6 + " b的值是:" + 1 + "sum 的值是:" + 27 +"<br/>"); document.write( "答案是,第二轮计算后,a 为:" + a + ";b为:" + b +";第二次计算sum为:"+ sum +",sum的类型也发生变化了。"); </script>
以上就是关于Javascript基础语法的全部内容,希望对大家的学习有所帮助。