Home > Web Front-end > JS Tutorial > A brief analysis of operators and expressions in JavaScript

A brief analysis of operators and expressions in JavaScript

青灯夜游
Release: 2022-10-27 19:21:44
forward
1608 people have browsed it

The following article will introduce you to the operators and expressions in JavaScript. I hope it will be helpful to you!

A brief analysis of operators and expressions in JavaScript

1. Expression

##1.1 Original expression

3.14  //数字直接量
"hello world"  //字符串直接量
/pattern/  //正则表达式直接量

true  //返回一个布尔值: 真
false  //返回一个布尔值: 假
null  //返回一个值: 空
this  //返回“当前”对象

i  //返回变量i的值
sum //返回sum的值
undefined  //undefined是全局变量,和null不同,它不是一个关键字
Copy after login

1.2 Expression

Operands and operators are combined.

1
1 + 2
(1 + 2) * 3
(1 + 2 ) * 3 && !flag
Copy after login

2. Operator

  • Arithmetic operator(, -, *, /, %, ** ): Ordinary addition, subtraction, multiplication, division and remainder operations, where ** is the power operator.
  •  3 ** 2 // 结果为:9
    Copy after login
  • Unary operators (, --, , -): Some operators that require only one operand, Note the difference between : , -- when placed on the left and right sides of a variable. When placed on the left side of a variable, it increases (decreases) first and then takes the value. When placed on the right side, the opposite is true.
  • let a = 2, y, z;
    y = ++a // 先执行a = a + 1, 得到 a = 3, 然后将a赋值给y,则y = 3
    z = a++ // 接上面a = 3, 将a赋值给z,则z = 3,然后再执行 a = a + 1, 得到a = 4
    
    //一元证号(+)
    +3  //3
    +'3'  //3
    +true  //1
    +functioan(a){return a}  //NaN
    Copy after login
  • Assignment operator (=, =, -=, *=, /=, %=): Change a constant Or assign the value of a variable or expression to another variable, Note: ES6’s new destructuring assignment.
  • //解构数组
    let [a, b, c] = ['one', 'two', 'three']  // a = 'one', b = 'two', c = 'three'
    //解构对象
    let { name, age } = { name: 'cc', age: 100 }  // name = 'cc', age = 100
    Copy after login
  • Equality operator (==, !=, ===, !==): used to compare left and right operations Are the numbers equal? Please refer to: The matching rules of JS comparison operators ("===" and "==") and the judgment results of if() conditions.
  • Relational operators (>, <, >=, <=): Used to compare the size of the operands around the operator.
  • Bit operators(&, |, ~, ^, <<, >>, >>>): After the operand is converted into binary, operations such as AND, OR, NOT, XOR, etc. are performed, and finally a value in JS is returned. Among them, >>> is unsigned right shift.
  • Logical operators (&&, ||, !): Mainly use AND, OR, NOT to determine whether the value of an expression is true. Note: AND and OR operations have a short-circuit calculation effect.
  • //短路计算
    false && (anything) // 结果为false
    true || (anything) // 结果为anything
    
    //方法1: 设置x的缺省值
    function test(x) {
    	x = x || 100
    }
    test(10)  //x = 10
    test()  // x = 100
    
    // 方法2: ES6的方式
    function test(x = 100) {
     ...
    }
    test(10)  //x = 10
    test()  // x = 100
    Copy after login

    • Conditional operator(? :): The only operator in JS with 3 operands, generally used for if Use the short form of the statement.
    【Related recommendations:

    javascript video tutorial, programming video

    The above is the detailed content of A brief analysis of operators and expressions in JavaScript. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template