Home>Article>Web Front-end> Let’s talk about JavaScript operators

Let’s talk about JavaScript operators

WBOY
WBOY forward
2022-08-03 17:39:11 1849browse

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.

Let’s talk about JavaScript operators

[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:

  • Arithmetic operators
  • Increment and decrement operators
  • Comparison Operator
  • Logical operator
  • Assignment operator

Arithmetic operator

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
  • Arithmetic operator priority: multiplication and division first, addition and subtraction
  • You can use the % remainder operator to determine whether a number is divisible

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.

Increment and decrement operators

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

Preceding increment operator:

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

Postincrement operator

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

Difference summary

  • The pre-increment and post-increment operators can simplify the writing of code, making the variable value 1 easier to write than before.
  • When used alone, the running results are the same.
  • When used in conjunction with other codes, the execution results will be different.
  • Preposition: add self first, then calculate ( put yourself first, then others)
  • Postposition: first calculate the original value, then add yourself ( first, others Later, )
  • When developing, post-increment/decrement is mostly used, and the code occupies one line. Example: num ;

Exercise:

var e = 10; var f = e++ + ++e; //1.e++=10 e=11 2.++e=12 f=10+12 console.log(f); //22

Comparison operator

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.

Operator nameDescriptionCaseResult<Less than number1>2true##>##>=Greater than or equal to sign (greater than or equal to) 2>=2trueLess than or equal sign (less than or equal to)determine equal sign (will transform)unequal signCongruent, the value and data type are required to be consistent
Greater than sign 1>2 false
##<=
3<=2false==
17==17 true!=
17!=17false=== !==
17==='17'false
console.log(2 <= 5); //true console.log('岳泽以' = '个人博客'); //false console.log(17 == '17'); //true 默认转换数据类型,字符串型转换为数字型 console.log(17 = '17'); //false 数据类型不同,要求值和数据类型一致
SymbolFunction=AssignmentJudgementCongruent
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

逻辑运算符

概念:逻辑运算符是用来进行布尔值运算的运算符,其返回值也是布尔值。后面开发中经常用于多个条件的判断。

逻辑运算符说明案例
&&"逻辑与",简称“与”andture &&false
丨丨"逻辑或",简称“或”orture 丨丨false
!"逻辑非",简称“非”not!true

逻辑与

符号:&& 相对于and

两侧都为 true结果才是 true,只要有一侧为 false,结果就为 false

console.log(3 > 5 && 3 > 2); //false console.log(3 < 5 && 3 < 7); //true

逻辑或

符号:||相当于or

俩侧都为 false,结果才是 false,只要有一侧为 true,结果就是 true

console.log(3 > 5 && 3 > 2); //false console.log(3 < 5 && 3 < 7); //true

逻辑非

符号:!相对于not

逻辑非也叫作取反符,用来取一个布尔值相反的值。

console.log(!true); //false console.log(!false); //true

短路运算(逻辑中断)

短路运算的原理:当有多个表达式(值)时,左边的表达值可以确定结果时,就不再继续运算右边的表达式的值。

逻辑与:

  • 语法表达式1 && 表达式2
  • 如果第一个表达式的值为真,则返回表达上2
  • 如果第一个表达式的值为假,则返回表达式1
console.log(123 && 456); //返回456,除了0以外的所有数字都为真。 console.log(123 && 456 && 789); //返回789,依次后推 console.log(0 && 456); //0

逻辑或:

  • 语法表达式1 || 表达式2
  • 如果表达式1结果为真,则返回表达式1
  • 如果表达式1结果为假,则返回表达式2
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 name='岳泽以';
+=、-=加、减一个数后再赋值var age=10; age+=5; //15
*=、/=、%=乘、除、取余后再赋值var age=10; age*=5; //10
var num = 5; num += 10; console.log(num); //5+10=15 num *= 3; console.log(num); //15*3=45

运算符优先级

优先级运算符顺序
1小括号()
2一元运算符++ -- !
3算术运算符* / + -
4关系运算符> >= < <=
5相等运算符== != === !==
6逻辑运算符&&丨丨
7赋值运算符=
8逗号运算符,
  • 一元运算符里的逻辑非优先级很高。
  • 逻辑与比逻辑或优先级高
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 */

【相关推荐:javascript视频教程web前端

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!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete