Home>Article>Web Front-end> Let’s talk about JavaScript operators
This article brings you relevant knowledge about javascript, which mainly introduces related issues about operators. Operators are also called operators and are used to implement assignment and comparison. and symbols that perform arithmetic operations and other functions. Let’s take a look at them together. I hope it will be helpful to everyone.
[Related recommendations: javascript video tutorial, web front-end】
Operator (operator) also Known as operators, they are symbols used to implement functions such as assignment, comparison, and performing arithmetic operations.
Commonly used operators in JavaScript are:
Concept: Symbols used in arithmetic operations, used to perform two operations Arithmetic operations on variables or values.
Operator | Description | Instance |
---|---|---|
Add | 10 20=30 | |
- | minus | 20-10=10 |
* | Multiply | 10*20=200 |
/ | divide | 10/20=0.5 |
% | Get the remainder (modulo) | Return the remainder of the division 9%2=1 |
console.log(1 + 1); //2 console.log(1 - 1); //0 console.log(1 * 1); //1 console.log(1 / 1); //1 console.log(4 % 2); //0
Floating point numbers will have errors in arithmetic operations (avoid direct participation in operations):
console.log(0.1 + 0.2); //0.30000000000000004
Cannot directly determine whether two floating point numbers are equal.
var num = 0.1 + 0.2; console.log(num == 0.3); //false
Expressions and return values:
Formulas composed of numbers, operators, variables, etc. are called expressions.
The expression will eventually return a result to us, which we call the return value.
If you need to repeatedly add or subtract 1 to a numeric variable, you can use increment (
) and decrement (--
) operator to complete.
Complicated writing:
var num = 1; num = num + 1; num = num + 1; console.log(num); //3
Write in the variable The front
num
prefix increment is to add 1, similar to num=num 1
var age = 10; ++age; console.log(age);//11 类似于age = age + 1
Usage formula: Add self first, then return the value
console.log(age); var a = 10; console.log(++a + 10); //(10+1)+10=21
Write after the variable
num
Setting increment means adding 1, similar to num=num 1
var age = 10; age++; console.log(age);//11 类似于age = age + 1
Usage formula: Return to the original value first, then increment
var a = 10; console.log(a++ + 10); //10+10=20 console.log(a); //11
num ;
Exercise:
var e = 10; var f = e++ + ++e; //1.e++=10 e=11 2.++e=12 f=10+12 console.log(f); //22
Concept: Comparison operator (relational operator) is the operator used when comparing two data. After the comparison operation, a Boolean value (true/false) will be returned as the result of the comparison operation.
Description | Case | Result | |
---|---|---|---|
Less than number | 1>2 | true | |
Greater than sign | 1>2 | false | ##>= |
2>=2 | true | ##<= | |
3<=2 | false | == | |
17==17 | true | != | |
17!=17 | false | === !== | |
17==='17' | false | console.log(2 <= 5); //true console.log('岳泽以' = '个人博客'); //false console.log(17 == '17'); //true 默认转换数据类型,字符串型转换为数字型 console.log(17 = '17'); //false 数据类型不同,要求值和数据类型一致 |
Usage | |||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Give the right side to the left side | == | ||||||||||||||||||||||||||||||||||||||||||||||||||||
Determine whether the values on both sides are equal (there is implicit conversion) | === | ||||||||||||||||||||||||||||||||||||||||||||||||||||
Determine whether the values and data types of both sides are equal Identical | 逻辑运算符概念:逻辑运算符是用来进行布尔值运算的运算符,其返回值也是布尔值。后面开发中经常用于多个条件的判断。
逻辑与符号: 两侧都为 console.log(3 > 5 && 3 > 2); //false console.log(3 < 5 && 3 < 7); //true 逻辑或符号: 俩侧都为 console.log(3 > 5 && 3 > 2); //false console.log(3 < 5 && 3 < 7); //true 逻辑非符号: 逻辑非也叫作取反符,用来取一个布尔值相反的值。 console.log(!true); //false console.log(!false); //true 短路运算(逻辑中断)短路运算的原理:当有多个表达式(值)时,左边的表达值可以确定结果时,就不再继续运算右边的表达式的值。 逻辑与:
console.log(123 && 456); //返回456,除了0以外的所有数字都为真。 console.log(123 && 456 && 789); //返回789,依次后推 console.log(0 && 456); //0 逻辑或:
console.log(123 || 456); //123 console.log(123 || 456 || 123 + 456); //123 console.log(0 || 456 || 123 + 456); //456 注意:逻辑中断会造成短路操作,即不执行后面的代码,影响程序员的运行结果。 var num = 0; console.log(123 || num++); //逻辑中断造成num++未执行 console.log(num); //0 赋值运算符概念:用来把数据赋值给变量的运算符
var num = 5; num += 10; console.log(num); //5+10=15 num *= 3; console.log(num); //15*3=45 运算符优先级
console.log(4 >= 6 || '我' != '你' && !(12 * 2 == 144) && true); //true /* 逻辑运算符分四段 1.4 >= 6 得false 2.'我' != '你'得true 3.!(12 * 2 == 144)得ture 4.true 然后判断逻辑与:2与3得true 3和4得true 再判断逻辑或得:true */ |
The above is the detailed content of Let’s talk about JavaScript operators. For more information, please follow other related articles on the PHP Chinese website!