JavaScript运算符:算术运算,赋值运算符和比较运算都是基本的,像+和 – 这样的特殊字符可以用不同的组合来执行不同的操作。 让我们看看一些更基本的操作符,比如算术,赋值和比较操作符如何表现。
让我们看看一些更基本的运算符:
1、算数运算符(Arithmetic operators);
2、赋值运算符(Assignment operators);
3、比较运算符(Comparison operators)。
一、运算符
+号 + 添加两个值一起
例子: var addition = 1 + 1;//addition = 2
-号 – 从其中删除一个值
例子:var substraction = 1 – 1;//subtraction = 0
*字符 * 用于乘以两个值
例子:var multiplication = 5 * 2; multiplication = 10;
正斜杠 / 将一个数字除以另一个
例子:var division = 5/2.5;// division = 2
二、系数(Modulus)
%
例子:var remainder = 5 % 2;//remainder = 1;
模数运算符非常适合用于确定数字是奇数还是偶数,
偶数除以2余0时;
奇数除以2余1时;
余数分两个数字
例子:var remainder = 5 % 2;//remainder = 1;
递增(Increment) ++
递减(Decrement) —
比较复杂,后缀模式(postfix mode)
++exampleVariable
–exampleVariable
例子:
var postfix = 5;
var prefix = 5;
console.log
postfix++;//
postfix;
++prefix;
prefix;
让人混肴,用Assignment operators代替
三、赋值运算符(Assignment operators)
var additionAssignment = 1;
additionAssignment + = 1;//additionAssignment = 2;
var substractionAssignment – = 1;//substractionAssignment = 0;
var mulitplicationAssignment = 2;
var divisionAssignment = 5;
var modulusAssignment = 5;
multiplicationAssigment *= 2;
divisionAssignment /=2.5;
modulusAssignment %=2;
四、比较运算符(Comparison operators)
equality comparison;
greater-than comparison;
less-than comparison;
console测试:
1、1 == true;(regular Comparison),
true
2、1 === true;(strict Comparison)
false
当我们使用比较运算符是:
一个等于真;
一个等于true
不严格的两个等号,严格是三个等号。
第一次看到值为true,第二次看到值为false。
语言松散,有时表现出凝聚力。
equality without type coercion。无类型强制平等
By using = you assign a value to something.
x = 1 //x now equals 1
x = 2 //x now equals 2
By using == you check if something is equal to something else. This is not strict
x == 1 //is x equal to 1? (False)
x == 2 //is x equal to 2? (True)
true == 1 //does the boolean value of true equal 1? (True)
By using === you check if something is equal to something else. This is also strict.
x === 1 //is x equal to 1? (False)
x === 2 //is x equal to 2? (True)
true === 1 //does the boolean value of true equal 1? (False)
如果不清楚,那么严格的是它不仅检查两个值的相等性,而且还比较两个值的类型。 使用==将尝试将表达式的一侧转换为与另一侧相同的类型。 例如,布尔和整数。 true的布尔值是1,因此1等于1? 是的,没错。 但是,当使用strict时,它不会在进行比较之前尝试转换,它会检查true是否等于1,这不是因为它们是两种不同的数据类型,并返回false。
Hope this helps!
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comparison_Operators?v=test
3、1 !=true;
false
4、1 !== true;
true
5、1>1;
false
6、1>=1
true
7、1<1;
false
8、1<=1
true
9、1>‘1’
false
10、1>’hello’
false
avoid non-strict comparisons == !=
转载请注明:林雍岷 » 4. JavaScript基础知识:算术,赋值和比较运算符