Home >Web Front-end >JS Tutorial >Take you step by step to understand the basics of JavaScript operators
This article will take you through the basic knowledge of operators in JavaScript: arithmetic operators, increment/decrement operators, comparison operators, logical operators and ternary operators. I hope to be helpful.
1 Arithmetic operator
Concept: It is to perform Operators for addition, subtraction, multiplication, and division, and remainder calculations
Operators: , -, *, /, % (remainder operations)
Note: When performing arithmetic operations, floating point (decimal) operations may cause precision problems
2 Increment and decrement operators
Operators:
, --
(a, a--, a, --a)
//前置递增运算符 var num = 1; ++num //或者num++ console.log(num)//结果为2 //++num 相当于是 num = num + 1 //前置++ 是先做自增再做其他运算 //前置递减运算符 var num = 1; --num //或者num-- console.log(num)//结果为0 //--num 相当于是 num = num - 1 //前置-- 是先做自减再做其他运算
3 Comparison operators
Operators: > , 28675c3d02b76e47536b84c673e04c48= , ebb451643671248cfef3bdb32bb50acb , 28675c3d02b76e47536b84c673e04c48= , <= The priority is 6
4 Logical operator
##Logical AND (&&):
var age = 18 var num ; age>18 && (num = 998); //因为 age>18没有成立,逻辑与就已经得到结果为假 //所以当逻辑与计算完毕之后,后面的num=998就不会再运行了
var age = 18; var num; age == 18 || (num = 998); //因为 age==18成立,逻辑或就已经得到结果为真 //所以当逻辑或计算完毕之后,后面的num=998就不会再运行了
Logical NOT (!): Negate true to false, false to true
var a = 5; !(a > 1)//a等于5,所以大于1为真(true),因为取反,所以这个表达式为假(false)5 Ternary operator: ?:
can be understood as a simplified way of writing the double branch of if
Grammar structure:
表达式1 ? 表达式2 : 表达式3
will be executed. When expression 1 is not established, expression 3
var a,b=2,c=3; a=b>2?b:c; //运行结果是a为3,b大于2为真就返回b给a,为假返回c给a,因为b不大于2,所以返回c给a
will be executed. [Recommended learning:
javascript advanced tutorialThe above is the detailed content of Take you step by step to understand the basics of JavaScript operators. For more information, please follow other related articles on the PHP Chinese website!