Hướng dẫn how many logical operators are there in javascript? - có bao nhiêu toán tử logic trong javascript?

  • " Trước
  • Tiếp theo "

Chương này mô tả các biểu thức và toán tử của JavaScript, bao gồm gán, so sánh, số học, bitwise, logic, chuỗi, ternary và nhiều hơn nữa.

Ở cấp độ cao, một biểu thức là một đơn vị mã hợp lệ phân giải thành một giá trị. Có hai loại biểu thức: những loại có tác dụng phụ (như gán giá trị) và các loại hoàn toàn đánh giá.

Biểu thức

const val = 0;
val.x = 3;

console.log(val.x); // Prints undefined.
console.log(val); // Prints 0.
5 là một ví dụ về loại đầu tiên. Biểu thức này sử dụng toán tử
const val = 0;
val.x = 3;

console.log(val.x); // Prints undefined.
console.log(val); // Prints 0.
6 để gán giá trị bảy cho biến
const val = 0;
val.x = 3;

console.log(val.x); // Prints undefined.
console.log(val); // Prints 0.
7. Biểu thức tự đánh giá thành
const val = 0;
val.x = 3;

console.log(val.x); // Prints undefined.
console.log(val); // Prints 0.
8.

Biểu thức

const val = 0;
val.x = 3;

console.log(val.x); // Prints undefined.
console.log(val); // Prints 0.
9 là một ví dụ về loại thứ hai. Biểu thức này sử dụng toán tử
const foo = ['one', 'two', 'three'];

// without destructuring
const one   = foo[0];
const two   = foo[1];
const three = foo[2];

// with destructuring
const [one, two, three] = foo;
0 để thêm
const foo = ['one', 'two', 'three'];

// without destructuring
const one   = foo[0];
const two   = foo[1];
const three = foo[2];

// with destructuring
const [one, two, three] = foo;
1 và
const foo = ['one', 'two', 'three'];

// without destructuring
const one   = foo[0];
const two   = foo[1];
const three = foo[2];

// with destructuring
const [one, two, three] = foo;
2 lại với nhau và tạo ra một giá trị,
const val = 0;
val.x = 3;

console.log(val.x); // Prints undefined.
console.log(val); // Prints 0.
8. Tuy nhiên, nếu cuối cùng nó không phải là một phần của cấu trúc lớn hơn (ví dụ, một tuyên bố biến như
const foo = ['one', 'two', 'three'];

// without destructuring
const one   = foo[0];
const two   = foo[1];
const three = foo[2];

// with destructuring
const [one, two, three] = foo;
4), kết quả của nó sẽ bị loại bỏ ngay lập tức - đây thường là một sai lầm của lập trình viên vì việc đánh giá không tạo ra bất kỳ hiệu ứng nào.

Như các ví dụ trên cũng minh họa, tất cả các biểu thức phức tạp được nối với các toán tử, chẳng hạn như

const val = 0;
val.x = 3;

console.log(val.x); // Prints undefined.
console.log(val); // Prints 0.
6 và
const foo = ['one', 'two', 'three'];

// without destructuring
const one   = foo[0];
const two   = foo[1];
const three = foo[2];

// with destructuring
const [one, two, three] = foo;
0. Trong phần này, chúng tôi sẽ giới thiệu các nhà khai thác sau:

  • Toán tử chuyển nhượng
  • Toán tử so sánh
  • Toán tử số học
  • Các nhà khai thác bitwise
  • Toán tử logic
  • Các nhà khai thác lớn
  • Toán tử chuỗi
  • Nhà điều hành có điều kiện (ternary)
  • Nhà điều hành dấu phẩy
  • Các nhà khai thác đơn
  • Nhà khai thác quan hệ

Các toán tử này tham gia các toán hạng được hình thành bởi các nhà khai thác có độ cao cao hơn hoặc một trong các biểu thức cơ bản. Một danh sách đầy đủ và chi tiết của các toán tử và biểu thức cũng có sẵn trong tài liệu tham khảo.

Ưu tiên của các toán tử xác định thứ tự chúng được áp dụng khi đánh giá một biểu thức. Ví dụ:

const x = 1 + 2 * 3;
const y = 2 * 3 + 1;

Mặc dù

const foo = ['one', 'two', 'three'];

// without destructuring
const one   = foo[0];
const two   = foo[1];
const three = foo[2];

// with destructuring
const [one, two, three] = foo;
7 và
const foo = ['one', 'two', 'three'];

// without destructuring
const one   = foo[0];
const two   = foo[1];
const three = foo[2];

// with destructuring
const [one, two, three] = foo;
0 xuất hiện trong các đơn đặt hàng khác nhau, cả hai biểu thức sẽ dẫn đến
const val = 0;
val.x = 3;

console.log(val.x); // Prints undefined.
console.log(val); // Prints 0.
8 vì
const foo = ['one', 'two', 'three'];

// without destructuring
const one   = foo[0];
const two   = foo[1];
const three = foo[2];

// with destructuring
const [one, two, three] = foo;
7 đã được ưu tiên hơn
const foo = ['one', 'two', 'three'];

// without destructuring
const one   = foo[0];
const two   = foo[1];
const three = foo[2];

// with destructuring
const [one, two, three] = foo;
0, do đó biểu thức ________ 57 liên quan sẽ luôn được đánh giá trước tiên. Bạn có thể ghi đè lên định mức độ ưu tiên của toán tử bằng cách sử dụng dấu ngoặc đơn (tạo ra một biểu thức được nhóm - biểu thức cơ bản). Để xem một bảng hoàn chỉnh của nhà điều hành ưu tiên cũng như các cảnh báo khác nhau, hãy xem trang tham chiếu của nhà điều hành.

JavaScript có cả toán tử nhị phân và Unary, và một toán tử ternary đặc biệt, toán tử có điều kiện. Một toán tử nhị phân yêu cầu hai toán hạng, một trước khi vận hành và một sau khi vận hành:

operand1 operator operand2

Ví dụ,

const val = 0;
val.x = 3;

console.log(val.x); // Prints undefined.
console.log(val); // Prints 0.
9 hoặc
// Declares a variable x and initializes it to the result of f().
// The result of the x = f() assignment expression is discarded.
let x = f();

x = g(); // Reassigns the variable x to the result of g().
4. Hình thức này được gọi là toán tử nhị phân infix, vì toán tử được đặt giữa hai toán hạng. Tất cả các toán tử nhị phân trong JavaScript là infix.

Một toán tử Unary yêu cầu một toán hạng duy nhất, trước hoặc sau khi vận hành:

operator operand
operand operator

Ví dụ,

// Declares a variable x and initializes it to the result of f().
// The result of the x = f() assignment expression is discarded.
let x = f();

x = g(); // Reassigns the variable x to the result of g().
5 hoặc
// Declares a variable x and initializes it to the result of f().
// The result of the x = f() assignment expression is discarded.
let x = f();

x = g(); // Reassigns the variable x to the result of g().
6. Mẫu
// Declares a variable x and initializes it to the result of f().
// The result of the x = f() assignment expression is discarded.
let x = f();

x = g(); // Reassigns the variable x to the result of g().
7 được gọi là toán tử không có tiền tố và mẫu
// Declares a variable x and initializes it to the result of f().
// The result of the x = f() assignment expression is discarded.
let x = f();

x = g(); // Reassigns the variable x to the result of g().
8 được gọi là toán tử unary postfix.
// Declares a variable x and initializes it to the result of f().
// The result of the x = f() assignment expression is discarded.
let x = f();

x = g(); // Reassigns the variable x to the result of g().
9 và
let x;
const y = (x = f()); // Or equivalently: const y = x = f();
console.log(y); // Logs the return value of the assignment x = f().

console.log(x = f()); // Logs the return value directly.

// An assignment expression can be nested in any place
// where expressions are generally allowed,
// such as array literals' elements or as function calls' arguments.
console.log([ 0, x = f(), 0 ]);
console.log(f(0, x = f(), 0));
0 là các toán tử postfix duy nhất trong JavaScript - tất cả các toán tử khác, như
let x;
const y = (x = f()); // Or equivalently: const y = x = f();
console.log(y); // Logs the return value of the assignment x = f().

console.log(x = f()); // Logs the return value directly.

// An assignment expression can be nested in any place
// where expressions are generally allowed,
// such as array literals' elements or as function calls' arguments.
console.log([ 0, x = f(), 0 ]);
console.log(f(0, x = f(), 0));
1,
let x;
const y = (x = f()); // Or equivalently: const y = x = f();
console.log(y); // Logs the return value of the assignment x = f().

console.log(x = f()); // Logs the return value directly.

// An assignment expression can be nested in any place
// where expressions are generally allowed,
// such as array literals' elements or as function calls' arguments.
console.log([ 0, x = f(), 0 ]);
console.log(f(0, x = f(), 0));
2, v.v. là tiền tố.

Toán tử chuyển nhượng

Toán tử so sánh

Toán tử số học

Các nhà khai thác bitwise

Toán tử logic

const obj = {};

obj.x = 3;
console.log(obj.x); // Prints 3.
console.log(obj); // Prints { x: 3 }.

const key = "y";
obj[key] = 5;
console.log(obj[key]); // Prints 5.
console.log(obj); // Prints { x: 3, y: 5 }.

Các nhà khai thác lớn

Toán tử chuỗi

const val = 0;
val.x = 3;

console.log(val.x); // Prints undefined.
console.log(val); // Prints 0.

Nhà điều hành có điều kiện (ternary)

Nhà điều hành dấu phẩy

Các nhà khai thác đơn

Nhà khai thác quan hệ

const foo = ['one', 'two', 'three'];

// without destructuring
const one   = foo[0];
const two   = foo[1];
const three = foo[2];

// with destructuring
const [one, two, three] = foo;

Các toán tử này tham gia các toán hạng được hình thành bởi các nhà khai thác có độ cao cao hơn hoặc một trong các biểu thức cơ bản. Một danh sách đầy đủ và chi tiết của các toán tử và biểu thức cũng có sẵn trong tài liệu tham khảo.

Ưu tiên của các toán tử xác định thứ tự chúng được áp dụng khi đánh giá một biểu thức. Ví dụ:

// Declares a variable x and initializes it to the result of f().
// The result of the x = f() assignment expression is discarded.
let x = f();

x = g(); // Reassigns the variable x to the result of g().

Mặc dù

const foo = ['one', 'two', 'three'];

// without destructuring
const one   = foo[0];
const two   = foo[1];
const three = foo[2];

// with destructuring
const [one, two, three] = foo;
7 và
const foo = ['one', 'two', 'three'];

// without destructuring
const one   = foo[0];
const two   = foo[1];
const three = foo[2];

// with destructuring
const [one, two, three] = foo;
0 xuất hiện trong các đơn đặt hàng khác nhau, cả hai biểu thức sẽ dẫn đến
const val = 0;
val.x = 3;

console.log(val.x); // Prints undefined.
console.log(val); // Prints 0.
8 vì
const foo = ['one', 'two', 'three'];

// without destructuring
const one   = foo[0];
const two   = foo[1];
const three = foo[2];

// with destructuring
const [one, two, three] = foo;
7 đã được ưu tiên hơn
const foo = ['one', 'two', 'three'];

// without destructuring
const one   = foo[0];
const two   = foo[1];
const three = foo[2];

// with destructuring
const [one, two, three] = foo;
0, do đó biểu thức ________ 57 liên quan sẽ luôn được đánh giá trước tiên. Bạn có thể ghi đè lên định mức độ ưu tiên của toán tử bằng cách sử dụng dấu ngoặc đơn (tạo ra một biểu thức được nhóm - biểu thức cơ bản). Để xem một bảng hoàn chỉnh của nhà điều hành ưu tiên cũng như các cảnh báo khác nhau, hãy xem trang tham chiếu của nhà điều hành.

Chuỗi bài tập hoặc bài tập làm tổ trong các biểu thức khác có thể dẫn đến hành vi đáng ngạc nhiên. Vì lý do này, một số hướng dẫn kiểu JavaScript không khuyến khích các bài tập về chuỗi hoặc làm tổ). Tuy nhiên, đôi khi chuỗi phân công và làm tổ có thể xảy ra, vì vậy điều quan trọng là có thể hiểu cách chúng hoạt động.

Bằng cách xâu chuỗi hoặc làm tổ một biểu thức gán, kết quả của nó có thể được gán cho một biến khác. Nó có thể được ghi lại, nó có thể được đặt bên trong một cuộc gọi theo nghĩa đen hoặc chức năng, v.v.

let x;
const y = (x = f()); // Or equivalently: const y = x = f();
console.log(y); // Logs the return value of the assignment x = f().

console.log(x = f()); // Logs the return value directly.

// An assignment expression can be nested in any place
// where expressions are generally allowed,
// such as array literals' elements or as function calls' arguments.
console.log([ 0, x = f(), 0 ]);
console.log(f(0, x = f(), 0));

Kết quả đánh giá phù hợp với biểu thức ở bên phải của dấu

const val = 0;
val.x = 3;

console.log(val.x); // Prints undefined.
console.log(val); // Prints 0.
6 trong cột "Ý nghĩa" của bảng trên. Điều đó có nghĩa là
let x;
const y = (x = f()); // Or equivalently: const y = x = f();
console.log(y); // Logs the return value of the assignment x = f().

console.log(x = f()); // Logs the return value directly.

// An assignment expression can be nested in any place
// where expressions are generally allowed,
// such as array literals' elements or as function calls' arguments.
console.log([ 0, x = f(), 0 ]);
console.log(f(0, x = f(), 0));
4 đánh giá thành bất kỳ kết quả nào của ____ 75,
function f () {
  console.log('F!');
  return 2;
}
function g () {
  console.log('G!');
  return 3;
}
let x, y;
6 đánh giá thành tổng kết quả
function f () {
  console.log('F!');
  return 2;
}
function g () {
  console.log('G!');
  return 3;
}
let x, y;
7,
function f () {
  console.log('F!');
  return 2;
}
function g () {
  console.log('G!');
  return 3;
}
let x, y;
8 đánh giá thành công suất thu được
function f () {
  console.log('F!');
  return 2;
}
function g () {
  console.log('G!');
  return 3;
}
let x, y;
9, v.v.

Trong trường hợp các bài tập logic,

y = x = f()
y = [ f(), x = g() ]
x[f()] = g()
0,
y = x = f()
y = [ f(), x = g() ]
x[f()] = g()
1 và
y = x = f()
y = [ f(), x = g() ]
x[f()] = g()
2, giá trị trả về là hoạt động logic mà không có sự phân công, do đó,
y = x = f()
y = [ f(), x = g() ]
x[f()] = g()
3,
y = x = f()
y = [ f(), x = g() ]
x[f()] = g()
4 và
y = x = f()
y = [ f(), x = g() ]
x[f()] = g()
5, tương ứng.

Khi chuỗi các biểu thức này không có dấu ngoặc đơn hoặc các toán tử nhóm khác như nghĩa đen, các biểu thức gán được nhóm phải sang trái (chúng có liên quan bên phải), nhưng chúng được đánh giá từ trái sang phải.grouped right to left (they are right-associative), but they are evaluated left to right.

Lưu ý rằng, đối với tất cả các toán tử gán không phải là

const val = 0;
val.x = 3;

console.log(val.x); // Prints undefined.
console.log(val); // Prints 0.
6, các giá trị kết quả luôn dựa trên các giá trị của toán hạng trước khi hoạt động.

Ví dụ: giả sử rằng các hàm sau

y = x = f()
y = [ f(), x = g() ]
x[f()] = g()
7 và
y = x = f()
y = [ f(), x = g() ]
x[f()] = g()
8 và các biến
const val = 0;
val.x = 3;

console.log(val.x); // Prints undefined.
console.log(val); // Prints 0.
7 và
operand1 operator operand2
00 đã được khai báo:

function f () {
  console.log('F!');
  return 2;
}
function g () {
  console.log('G!');
  return 3;
}
let x, y;

Xem xét ba ví dụ sau:

y = x = f()
y = [ f(), x = g() ]
x[f()] = g()

Đánh giá Ví dụ 1

operand1 operator operand2
01 tương đương với
operand1 operator operand2
02, vì toán tử gán
const val = 0;
val.x = 3;

console.log(val.x); // Prints undefined.
console.log(val); // Prints 0.
6 là liên kết đúng. Tuy nhiên, nó đánh giá từ trái sang phải:

  1. Biểu thức gán
    operand1 operator operand2
    
    01 bắt đầu đánh giá.
    1. operand1 operator operand2
      
      00 trên phía bên trái của nhiệm vụ này đánh giá thành một tham chiếu đến biến có tên
      operand1 operator operand2
      
      00.
    2. Biểu thức gán
      let x;
      const y = (x = f()); // Or equivalently: const y = x = f();
      console.log(y); // Logs the return value of the assignment x = f().
      
      console.log(x = f()); // Logs the return value directly.
      
      // An assignment expression can be nested in any place
      // where expressions are generally allowed,
      // such as array literals' elements or as function calls' arguments.
      console.log([ 0, x = f(), 0 ]);
      console.log(f(0, x = f(), 0));
      
      4 bắt đầu đánh giá.
      1. const val = 0;
        val.x = 3;
        
        console.log(val.x); // Prints undefined.
        console.log(val); // Prints 0.
        
        7 trên phía bên trái của nhiệm vụ này đánh giá thành một tham chiếu đến biến có tên
        const val = 0;
        val.x = 3;
        
        console.log(val.x); // Prints undefined.
        console.log(val); // Prints 0.
        
        7.
      2. Hàm gọi
        let x;
        const y = (x = f()); // Or equivalently: const y = x = f();
        console.log(y); // Logs the return value of the assignment x = f().
        
        console.log(x = f()); // Logs the return value directly.
        
        // An assignment expression can be nested in any place
        // where expressions are generally allowed,
        // such as array literals' elements or as function calls' arguments.
        console.log([ 0, x = f(), 0 ]);
        console.log(f(0, x = f(), 0));
        
        5 in "F!" đối với bảng điều khiển và sau đó đánh giá đến số
        operand1 operator operand2
        
        11.
      3. Đó là kết quả
        operand1 operator operand2
        
        11 từ
        let x;
        const y = (x = f()); // Or equivalently: const y = x = f();
        console.log(y); // Logs the return value of the assignment x = f().
        
        console.log(x = f()); // Logs the return value directly.
        
        // An assignment expression can be nested in any place
        // where expressions are generally allowed,
        // such as array literals' elements or as function calls' arguments.
        console.log([ 0, x = f(), 0 ]);
        console.log(f(0, x = f(), 0));
        
        5 được gán cho
        const val = 0;
        val.x = 3;
        
        console.log(val.x); // Prints undefined.
        console.log(val); // Prints 0.
        
        7.
    3. Biểu thức gán
      let x;
      const y = (x = f()); // Or equivalently: const y = x = f();
      console.log(y); // Logs the return value of the assignment x = f().
      
      console.log(x = f()); // Logs the return value directly.
      
      // An assignment expression can be nested in any place
      // where expressions are generally allowed,
      // such as array literals' elements or as function calls' arguments.
      console.log([ 0, x = f(), 0 ]);
      console.log(f(0, x = f(), 0));
      
      4 hiện đã hoàn thành đánh giá; Kết quả của nó là giá trị mới của
      const val = 0;
      val.x = 3;
      
      console.log(val.x); // Prints undefined.
      console.log(val); // Prints 0.
      
      7, đó là
      operand1 operator operand2
      
      11.
    4. Lần lượt
      operand1 operator operand2
      
      11 đó cũng được gán cho
      operand1 operator operand2
      
      00.
  2. Biểu thức gán
    operand1 operator operand2
    
    01 hiện đã hoàn thành đánh giá; Kết quả của nó là giá trị mới của
    operand1 operator operand2
    
    00 - xảy ra là
    operand1 operator operand2
    
    11.
    const val = 0;
    val.x = 3;
    
    console.log(val.x); // Prints undefined.
    console.log(val); // Prints 0.
    
    7 và
    operand1 operator operand2
    
    00 được gán cho
    operand1 operator operand2
    
    11 và bảng điều khiển đã in "F!".

Đánh giá Ví dụ 2

operand1 operator operand2
26 cũng đánh giá từ trái sang phải:

  1. Biểu thức gán
    operand1 operator operand2
    
    26 bắt đầu đánh giá.
    1. operand1 operator operand2
      
      00 trên tay trái của nhiệm vụ này đánh giá thành một tham chiếu đến biến có tên
      operand1 operator operand2
      
      00.
    2. Mảng bên trong nghĩa đen
      operand1 operator operand2
      
      30 bắt đầu đánh giá.
      1. Hàm gọi
        let x;
        const y = (x = f()); // Or equivalently: const y = x = f();
        console.log(y); // Logs the return value of the assignment x = f().
        
        console.log(x = f()); // Logs the return value directly.
        
        // An assignment expression can be nested in any place
        // where expressions are generally allowed,
        // such as array literals' elements or as function calls' arguments.
        console.log([ 0, x = f(), 0 ]);
        console.log(f(0, x = f(), 0));
        
        5 in "F!" đối với bảng điều khiển và sau đó đánh giá đến số
        operand1 operator operand2
        
        11.
      2. Đó là kết quả
        operand1 operator operand2
        
        11 từ
        let x;
        const y = (x = f()); // Or equivalently: const y = x = f();
        console.log(y); // Logs the return value of the assignment x = f().
        
        console.log(x = f()); // Logs the return value directly.
        
        // An assignment expression can be nested in any place
        // where expressions are generally allowed,
        // such as array literals' elements or as function calls' arguments.
        console.log([ 0, x = f(), 0 ]);
        console.log(f(0, x = f(), 0));
        
        5 được gán cho
        const val = 0;
        val.x = 3;
        
        console.log(val.x); // Prints undefined.
        console.log(val); // Prints 0.
        
        7.
        1. const val = 0;
          val.x = 3;
          
          console.log(val.x); // Prints undefined.
          console.log(val); // Prints 0.
          
          7 trên phía bên trái của nhiệm vụ này đánh giá thành một tham chiếu đến biến có tên
          const val = 0;
          val.x = 3;
          
          console.log(val.x); // Prints undefined.
          console.log(val); // Prints 0.
          
          7.
        2. Hàm gọi
          let x;
          const y = (x = f()); // Or equivalently: const y = x = f();
          console.log(y); // Logs the return value of the assignment x = f().
          
          console.log(x = f()); // Logs the return value directly.
          
          // An assignment expression can be nested in any place
          // where expressions are generally allowed,
          // such as array literals' elements or as function calls' arguments.
          console.log([ 0, x = f(), 0 ]);
          console.log(f(0, x = f(), 0));
          
          5 in "F!" đối với bảng điều khiển và sau đó đánh giá đến số
          operand1 operator operand2
          
          11.
        3. Đó là kết quả
          operand1 operator operand2
          
          11 từ
          let x;
          const y = (x = f()); // Or equivalently: const y = x = f();
          console.log(y); // Logs the return value of the assignment x = f().
          
          console.log(x = f()); // Logs the return value directly.
          
          // An assignment expression can be nested in any place
          // where expressions are generally allowed,
          // such as array literals' elements or as function calls' arguments.
          console.log([ 0, x = f(), 0 ]);
          console.log(f(0, x = f(), 0));
          
          5 được gán cho
          const val = 0;
          val.x = 3;
          
          console.log(val.x); // Prints undefined.
          console.log(val); // Prints 0.
          
          7.
      3. Biểu thức gán
        let x;
        const y = (x = f()); // Or equivalently: const y = x = f();
        console.log(y); // Logs the return value of the assignment x = f().
        
        console.log(x = f()); // Logs the return value directly.
        
        // An assignment expression can be nested in any place
        // where expressions are generally allowed,
        // such as array literals' elements or as function calls' arguments.
        console.log([ 0, x = f(), 0 ]);
        console.log(f(0, x = f(), 0));
        
        4 hiện đã hoàn thành đánh giá; Kết quả của nó là giá trị mới của
        const val = 0;
        val.x = 3;
        
        console.log(val.x); // Prints undefined.
        console.log(val); // Prints 0.
        
        7, đó là
        operand1 operator operand2
        
        11.
    3. Lần lượt
      operand1 operator operand2
      
      11 đó cũng được gán cho
      operand1 operator operand2
      
      00.
    4. Biểu thức gán
      operand1 operator operand2
      
      01 hiện đã hoàn thành đánh giá; Kết quả của nó là giá trị mới của
      operand1 operator operand2
      
      00 - xảy ra là
      operand1 operator operand2
      
      11.
      const val = 0;
      val.x = 3;
      
      console.log(val.x); // Prints undefined.
      console.log(val); // Prints 0.
      
      7 và
      operand1 operator operand2
      
      00 được gán cho
      operand1 operator operand2
      
      11 và bảng điều khiển đã in "F!".
  2. Đánh giá Ví dụ 2

operand1 operator operand2
26 cũng đánh giá từ trái sang phải:

Biểu thức gán

operand1 operator operand2
26 bắt đầu đánh giá.

  1. operand1 operator operand2
    
    00 trên tay trái của nhiệm vụ này đánh giá thành một tham chiếu đến biến có tên
    operand1 operator operand2
    
    00.
    1. Mảng bên trong nghĩa đen
      operand1 operator operand2
      
      30 bắt đầu đánh giá.
      1. const val = 0;
        val.x = 3;
        
        console.log(val.x); // Prints undefined.
        console.log(val); // Prints 0.
        
        7 trong truy cập thuộc tính này đánh giá thành một tham chiếu đến biến có tên
        const val = 0;
        val.x = 3;
        
        console.log(val.x); // Prints undefined.
        console.log(val); // Prints 0.
        
        7.
      2. Sau đó, chức năng gọi
        let x;
        const y = (x = f()); // Or equivalently: const y = x = f();
        console.log(y); // Logs the return value of the assignment x = f().
        
        console.log(x = f()); // Logs the return value directly.
        
        // An assignment expression can be nested in any place
        // where expressions are generally allowed,
        // such as array literals' elements or as function calls' arguments.
        console.log([ 0, x = f(), 0 ]);
        console.log(f(0, x = f(), 0));
        
        5 in "F!" đối với bảng điều khiển và sau đó đánh giá đến số
        operand1 operator operand2
        
        11.
    2. Truy cập tài sản
      operand1 operator operand2
      
      61 trên nhiệm vụ này hiện đã hoàn thành việc đánh giá; Kết quả của nó là một tham chiếu thuộc tính biến:
      operand1 operator operand2
      
      67.
    3. Sau đó, chức năng gọi
      operand1 operator operand2
      
      36 in "G!" đến bảng điều khiển và sau đó đánh giá đến số
      const foo = ['one', 'two', 'three'];
      
      // without destructuring
      const one   = foo[0];
      const two   = foo[1];
      const three = foo[2];
      
      // with destructuring
      const [one, two, three] = foo;
      
      1.
    4. Rằng
      const foo = ['one', 'two', 'three'];
      
      // without destructuring
      const one   = foo[0];
      const two   = foo[1];
      const three = foo[2];
      
      // with destructuring
      const [one, two, three] = foo;
      
      1 hiện được gán cho
      operand1 operator operand2
      
      67. (Bước này sẽ chỉ thành công nếu
      const val = 0;
      val.x = 3;
      
      console.log(val.x); // Prints undefined.
      console.log(val); // Prints 0.
      
      7 được gán cho một đối tượng.)
  2. Biểu thức gán
    operand1 operator operand2
    
    58 hiện đã hoàn thành đánh giá; Kết quả của nó là giá trị mới của
    operand1 operator operand2
    
    67 - xảy ra là
    const foo = ['one', 'two', 'three'];
    
    // without destructuring
    const one   = foo[0];
    const two   = foo[1];
    const three = foo[2];
    
    // with destructuring
    const [one, two, three] = foo;
    
    1.
    operand1 operator operand2
    
    67 hiện được gán cho
    const foo = ['one', 'two', 'three'];
    
    // without destructuring
    const one   = foo[0];
    const two   = foo[1];
    const three = foo[2];
    
    // with destructuring
    const [one, two, three] = foo;
    
    1 và bảng điều khiển đã in "F!" Sau đó "G!".

Tránh chuỗi phân công

Chuỗi bài tập hoặc bài tập làm tổ trong các biểu thức khác có thể dẫn đến hành vi đáng ngạc nhiên. Vì lý do này, chuỗi bài tập trong cùng một tuyên bố không được khuyến khích).

Cụ thể, việc đặt một chuỗi biến trong câu lệnh

let x;
const y = (x = f()); // Or equivalently: const y = x = f();
console.log(y); // Logs the return value of the assignment x = f().

console.log(x = f()); // Logs the return value directly.

// An assignment expression can be nested in any place
// where expressions are generally allowed,
// such as array literals' elements or as function calls' arguments.
console.log([ 0, x = f(), 0 ]);
console.log(f(0, x = f(), 0));
9,
function f () {
  console.log('F!');
  return 2;
}
function g () {
  console.log('G!');
  return 3;
}
let x, y;
0 hoặc
function f () {
  console.log('F!');
  return 2;
}
function g () {
  console.log('G!');
  return 3;
}
let x, y;
1 thường không hoạt động. Chỉ có biến ngoài cùng/ngoài cùng bên trái mới được khai báo; Các biến khác trong chuỗi gán không được khai báo bởi câu lệnh ____ 79/________ 80/________ 81. Ví dụ:

Tuyên bố này dường như tuyên bố các biến

const val = 0;
val.x = 3;

console.log(val.x); // Prints undefined.
console.log(val); // Prints 0.
7,
operand1 operator operand2
00 và
operand1 operator operand2
86. Tuy nhiên, nó chỉ thực sự tuyên bố biến
operand1 operator operand2
86.
operand1 operator operand2
00 và
const val = 0;
val.x = 3;

console.log(val.x); // Prints undefined.
console.log(val); // Prints 0.
7 là các tham chiếu không hợp lệ đối với các biến không tồn tại (ở chế độ nghiêm ngặt) hoặc tệ hơn, sẽ ngầm tạo các biến toàn cầu cho
const val = 0;
val.x = 3;

console.log(val.x); // Prints undefined.
console.log(val); // Prints 0.
7 và
operand1 operator operand2
00 ở chế độ cẩu thả.

Toán tử so sánh

Một toán tử so sánh so sánh các toán hạng của nó và trả về giá trị logic dựa trên việc so sánh có đúng hay không. Các toán hạng có thể là các giá trị số, chuỗi, logic hoặc đối tượng. Các chuỗi được so sánh dựa trên thứ tự từ vựng tiêu chuẩn, sử dụng các giá trị Unicode. Trong hầu hết các trường hợp, nếu hai toán hạng không thuộc cùng loại, JavaScript cố gắng chuyển đổi chúng thành một loại thích hợp để so sánh. Hành vi này thường dẫn đến việc so sánh các toán hạng bằng số. Các ngoại lệ duy nhất để chuyển đổi loại trong các so sánh liên quan đến các toán tử

operand1 operator operand2
92 và
operand1 operator operand2
93, thực hiện các so sánh bình đẳng và bất bình đẳng nghiêm ngặt. Các toán tử này không cố gắng chuyển đổi các toán hạng thành các loại tương thích trước khi kiểm tra bình đẳng. Bảng sau đây mô tả các toán tử so sánh theo mã mẫu này:

operand1 operator operand2
0

Lưu ý:

operand1 operator operand2
94 không phải là toán tử so sánh mà là ký hiệu cho các hàm mũi tên.
operand1 operator operand2
94 is not a comparison operator but rather is the notation for Arrow functions.

Toán tử số học

Một toán tử số học lấy các giá trị số (theo nghĩa đen hoặc biến) làm toán hạng của chúng và trả về một giá trị số duy nhất. Các toán tử số học tiêu chuẩn là bổ sung (

const foo = ['one', 'two', 'three'];

// without destructuring
const one   = foo[0];
const two   = foo[1];
const three = foo[2];

// with destructuring
const [one, two, three] = foo;
0), phép trừ (
operand1 operator operand2
96), nhân (
const foo = ['one', 'two', 'three'];

// without destructuring
const one   = foo[0];
const two   = foo[1];
const three = foo[2];

// with destructuring
const [one, two, three] = foo;
7) và chia (
operand1 operator operand2
98). Các nhà khai thác này hoạt động như chúng trong hầu hết các ngôn ngữ lập trình khác khi được sử dụng với số điểm nổi (đặc biệt, lưu ý rằng sự phân chia theo 0 tạo ra
operand1 operator operand2
99). Ví dụ:

operand1 operator operand2
1

Ngoài các hoạt động số học tiêu chuẩn (

const foo = ['one', 'two', 'three'];

// without destructuring
const one   = foo[0];
const two   = foo[1];
const three = foo[2];

// with destructuring
const [one, two, three] = foo;
0,
operand1 operator operand2
96,
const foo = ['one', 'two', 'three'];

// without destructuring
const one   = foo[0];
const two   = foo[1];
const three = foo[2];

// with destructuring
const [one, two, three] = foo;
7,
operand1 operator operand2
98), JavaScript cung cấp các toán tử số học được liệt kê trong bảng sau:

Các nhà khai thác bitwise

Một toán tử bitwise coi các toán hạng của họ là một tập hợp 32 bit (số không và các bit), thay vì là số thập phân, thập lục phân hoặc octal. Ví dụ, số chín thập phân có biểu diễn nhị phân là 1001. Các toán tử bitwise thực hiện các hoạt động của chúng trên các biểu diễn nhị phân như vậy, nhưng chúng trả về các giá trị số JavaScript tiêu chuẩn.

Bảng sau đây tóm tắt các toán tử bitwise của JavaScript.

Các toán tử logic bitwise

Về mặt khái niệm, các toán tử logic bitwise hoạt động như sau:

  • Các toán hạng được chuyển đổi thành các số nguyên ba mươi hai bit và được thể hiện bằng một loạt các bit (số không và các bit). Những con số có hơn 32 bit nhận được các bit quan trọng nhất của chúng bị loại bỏ. Ví dụ: số nguyên sau với hơn 32 bit sẽ được chuyển đổi thành số nguyên 32 bit:
    operand1 operator operand2
    
    2
  • Mỗi bit trong toán hạng đầu tiên được kết hợp với bit tương ứng trong toán hạng thứ hai: bit thứ nhất đến bit thứ nhất, bit thứ hai đến bit thứ hai, v.v.
  • Toán tử được áp dụng cho từng cặp bit và kết quả được xây dựng bitwise.

Ví dụ, biểu diễn nhị phân của chín là 1001 và biểu diễn nhị phân của mười lăm là 1111. Vì vậy, khi các toán tử bitwise được áp dụng cho các giá trị này, kết quả như sau:

Lưu ý rằng tất cả 32 bit được đảo ngược bằng cách sử dụng bitwise không phải toán tử và các giá trị đó có bit có ý nghĩa nhất (nhất) được đặt thành 1 biểu thị số âm (biểu diễn hai phần bổ sung).

operator operand
operand operator
04 đánh giá theo cùng một giá trị mà
operator operand
operand operator
05 đánh giá.

Các nhà khai thác thay đổi bitwise

Các toán tử dịch chuyển bitwise có hai toán hạng: thứ nhất là một đại lượng được thay đổi và thứ hai chỉ định số lượng vị trí bit mà toán hạng đầu tiên sẽ được thay đổi. Hướng của hoạt động thay đổi được điều khiển bởi người vận hành được sử dụng.

Các toán tử thay đổi chuyển đổi toán hạng của họ thành ba mươi hai số nguyên bit và trả về kết quả của loại

operator operand
operand operator
06 hoặc
operator operand
operand operator
07: cụ thể, nếu loại của toán hạng bên trái là
operator operand
operand operator
07, họ sẽ trả về
operator operand
operand operator
07; Nếu không, họ trở lại
operator operand
operand operator
06.

Các toán tử thay đổi được liệt kê trong bảng sau.

Toán tử logic

Các toán tử logic thường được sử dụng với các giá trị boolean (logic); Khi họ, họ trả lại một giá trị boolean. Tuy nhiên, các toán tử

operator operand
operand operator
11 và
operator operand
operand operator
12 thực sự trả về giá trị của một trong các toán hạng được chỉ định, vì vậy nếu các toán tử này được sử dụng với các giá trị phi Boolean, chúng có thể trả về giá trị phi Boolean. Các toán tử logic được mô tả trong bảng sau.

Ví dụ về các biểu thức có thể được chuyển đổi thành

operator operand
operand operator
13 là những biểu thức đánh giá thành NULL, 0, NAN, chuỗi trống ("") hoặc không xác định.

Mã sau đây hiển thị các ví dụ về toán tử

operator operand
operand operator
11 (logic và).

operand1 operator operand2
3

Mã sau đây hiển thị các ví dụ về || (logic hoặc) toán tử.

operand1 operator operand2
4

Mã sau đây hiển thị các ví dụ về! (logic không) toán tử.

operand1 operator operand2
5

Đánh giá ngắn mạch

Khi các biểu thức logic được đánh giá từ trái sang phải, chúng được kiểm tra để đánh giá "ngắn mạch" có thể bằng cách sử dụng các quy tắc sau:

  • operator operand
    operand operator
    
    15 là ngắn mạch được đánh giá là sai.
  • operator operand
    operand operator
    
    16 là ngắn mạch được đánh giá là true.

Các quy tắc logic đảm bảo rằng các đánh giá này luôn luôn chính xác. Lưu ý rằng bất cứ điều gì của các biểu thức trên không được đánh giá, vì vậy bất kỳ tác dụng phụ nào của việc làm như vậy không có hiệu lực.

Lưu ý rằng đối với trường hợp thứ hai, trong mã hiện đại, bạn có thể sử dụng toán tử kết hợp Nullish (

operator operand
operand operator
17) hoạt động như
operator operand
operand operator
12, nhưng nó chỉ trả về biểu thức thứ hai, khi từ đầu tiên là "nullish", tức là
let x;
const y = (x = f()); // Or equivalently: const y = x = f();
console.log(y); // Logs the return value of the assignment x = f().

console.log(x = f()); // Logs the return value directly.

// An assignment expression can be nested in any place
// where expressions are generally allowed,
// such as array literals' elements or as function calls' arguments.
console.log([ 0, x = f(), 0 ]);
console.log(f(0, x = f(), 0));
7 hoặc
let x;
const y = (x = f()); // Or equivalently: const y = x = f();
console.log(y); // Logs the return value of the assignment x = f().

console.log(x = f()); // Logs the return value directly.

// An assignment expression can be nested in any place
// where expressions are generally allowed,
// such as array literals' elements or as function calls' arguments.
console.log([ 0, x = f(), 0 ]);
console.log(f(0, x = f(), 0));
8. Do đó, đó là sự thay thế tốt hơn để cung cấp mặc định, khi các giá trị như
operator operand
operand operator
21 hoặc
operator operand
operand operator
22 cũng là các giá trị hợp lệ cho biểu thức đầu tiên.

Các nhà khai thác lớn

Hầu hết các toán tử có thể được sử dụng giữa các số cũng có thể được sử dụng giữa các giá trị

operator operand
operand operator
07.

operand1 operator operand2
6

Một ngoại lệ là không dấu sự thay đổi đúng (

operator operand
operand operator
24), không được xác định cho các giá trị lớn. Điều này là do một Bigint không có chiều rộng cố định, vì vậy về mặt kỹ thuật, nó không có "bit cao nhất".

operand1 operator operand2
7

Bigint và số không thể thay thế lẫn nhau - bạn không thể trộn chúng theo tính toán.

operand1 operator operand2
8

Điều này là do Bigint không phải là một tập hợp con cũng không phải là siêu số. Bigint có độ chính xác cao hơn số khi đại diện cho các số nguyên lớn, nhưng không thể đại diện cho số thập phân, do đó, chuyển đổi ngầm ở hai bên có thể mất độ chính xác. Sử dụng chuyển đổi rõ ràng để báo hiệu cho dù bạn muốn hoạt động là một hoạt động số hay một hoạt động lớn.

operand1 operator operand2
9

Bạn có thể so sánh Bigint với các con số.

operator operand
operand operator
0

Toán tử chuỗi

Ngoài các toán tử so sánh, có thể được sử dụng trên các giá trị chuỗi, toán tử nối (+) kết hợp hai giá trị chuỗi với nhau, trả về một chuỗi khác là kết hợp của hai chuỗi toán hạng.

Ví dụ,

operator operand
operand operator
1

Toán tử gán tốc ký

operator operand
operand operator
25 cũng có thể được sử dụng để nối các chuỗi.

Ví dụ,

operator operand
operand operator
2

Toán tử gán tốc ký operator operand operand operator 25 cũng có thể được sử dụng để nối các chuỗi.

Nhà điều hành có điều kiện (ternary)

Toán tử có điều kiện là toán tử JavaScript duy nhất có ba toán hạng. Toán tử có thể có một trong hai giá trị dựa trên một điều kiện. Cú pháp là:

Ví dụ,

operator operand
operand operator
3

Toán tử gán tốc ký

operator operand
operand operator
25 cũng có thể được sử dụng để nối các chuỗi.

Nhà điều hành có điều kiện (ternary)

Toán tử có điều kiện là toán tử JavaScript duy nhất có ba toán hạng. Toán tử có thể có một trong hai giá trị dựa trên một điều kiện. Cú pháp là:

Ví dụ: nếu

operator operand
operand operator
34 là một mảng 2 chiều với 10 phần tử ở một bên, mã sau sử dụng toán tử dấu phẩy để cập nhật hai biến cùng một lúc. Mã in các giá trị của các phần tử chéo trong mảng:

operator operand
operand operator
4

Các nhà khai thác đơn

Một hoạt động unary là một hoạt động chỉ có một toán hạng.

delete

Toán tử

operator operand
operand operator
35 xóa thuộc tính của một đối tượng. Cú pháp là:

operator operand
operand operator
5

Trong đó

operator operand
operand operator
36 là tên của một đối tượng,
operator operand
operand operator
37 là một thuộc tính hiện có và
operator operand
operand operator
38 là một chuỗi hoặc biểu tượng đề cập đến một thuộc tính hiện có.

Nếu toán tử

operator operand
operand operator
35 thành công, nó sẽ loại bỏ thuộc tính khỏi đối tượng. Cố gắng truy cập nó sau đó sẽ mang lại
let x;
const y = (x = f()); // Or equivalently: const y = x = f();
console.log(y); // Logs the return value of the assignment x = f().

console.log(x = f()); // Logs the return value directly.

// An assignment expression can be nested in any place
// where expressions are generally allowed,
// such as array literals' elements or as function calls' arguments.
console.log([ 0, x = f(), 0 ]);
console.log(f(0, x = f(), 0));
8. Toán tử
operator operand
operand operator
35 trả về
operator operand
operand operator
42 nếu hoạt động có thể; Nó trả về
operator operand
operand operator
13 nếu không thể hoạt động.

operator operand
operand operator
6

Xóa các yếu tố mảng

Vì các mảng chỉ là đối tượng, về mặt kỹ thuật có thể cho các yếu tố ____235 từ chúng. Tuy nhiên, điều này được coi là một thực hành xấu, hãy cố gắng tránh nó. Khi bạn xóa một thuộc tính mảng, độ dài mảng không bị ảnh hưởng và các yếu tố khác không được chỉ định lại. Để đạt được hành vi đó, tốt hơn là chỉ cần ghi đè lên phần tử với giá trị

let x;
const y = (x = f()); // Or equivalently: const y = x = f();
console.log(y); // Logs the return value of the assignment x = f().

console.log(x = f()); // Logs the return value directly.

// An assignment expression can be nested in any place
// where expressions are generally allowed,
// such as array literals' elements or as function calls' arguments.
console.log([ 0, x = f(), 0 ]);
console.log(f(0, x = f(), 0));
8. Để thực sự thao tác mảng, sử dụng các phương thức mảng khác nhau như
operator operand
operand operator
46.

typeof

Toán tử

let x;
const y = (x = f()); // Or equivalently: const y = x = f();
console.log(y); // Logs the return value of the assignment x = f().

console.log(x = f()); // Logs the return value directly.

// An assignment expression can be nested in any place
// where expressions are generally allowed,
// such as array literals' elements or as function calls' arguments.
console.log([ 0, x = f(), 0 ]);
console.log(f(0, x = f(), 0));
2 được sử dụng theo một trong các cách sau:

Toán tử

let x;
const y = (x = f()); // Or equivalently: const y = x = f();
console.log(y); // Logs the return value of the assignment x = f().

console.log(x = f()); // Logs the return value directly.

// An assignment expression can be nested in any place
// where expressions are generally allowed,
// such as array literals' elements or as function calls' arguments.
console.log([ 0, x = f(), 0 ]);
console.log(f(0, x = f(), 0));
2 trả về một chuỗi chỉ ra loại của toán hạng không được đánh giá.
operator operand
operand operator
49 là chuỗi, biến, từ khóa hoặc đối tượng mà loại sẽ được trả về. Các dấu ngoặc đơn là tùy chọn.

Giả sử bạn xác định các biến sau:

operator operand
operand operator
7

Toán tử

let x;
const y = (x = f()); // Or equivalently: const y = x = f();
console.log(y); // Logs the return value of the assignment x = f().

console.log(x = f()); // Logs the return value directly.

// An assignment expression can be nested in any place
// where expressions are generally allowed,
// such as array literals' elements or as function calls' arguments.
console.log([ 0, x = f(), 0 ]);
console.log(f(0, x = f(), 0));
2 trả về các kết quả sau cho các biến sau:

operator operand
operand operator
8

Đối với các từ khóa

operator operand
operand operator
42 và
let x;
const y = (x = f()); // Or equivalently: const y = x = f();
console.log(y); // Logs the return value of the assignment x = f().

console.log(x = f()); // Logs the return value directly.

// An assignment expression can be nested in any place
// where expressions are generally allowed,
// such as array literals' elements or as function calls' arguments.
console.log([ 0, x = f(), 0 ]);
console.log(f(0, x = f(), 0));
7, toán tử
let x;
const y = (x = f()); // Or equivalently: const y = x = f();
console.log(y); // Logs the return value of the assignment x = f().

console.log(x = f()); // Logs the return value directly.

// An assignment expression can be nested in any place
// where expressions are generally allowed,
// such as array literals' elements or as function calls' arguments.
console.log([ 0, x = f(), 0 ]);
console.log(f(0, x = f(), 0));
2 trả về các kết quả sau:

operator operand
operand operator
9

Đối với một số hoặc chuỗi, toán tử

let x;
const y = (x = f()); // Or equivalently: const y = x = f();
console.log(y); // Logs the return value of the assignment x = f().

console.log(x = f()); // Logs the return value directly.

// An assignment expression can be nested in any place
// where expressions are generally allowed,
// such as array literals' elements or as function calls' arguments.
console.log([ 0, x = f(), 0 ]);
console.log(f(0, x = f(), 0));
2 trả về các kết quả sau:

const obj = {};

obj.x = 3;
console.log(obj.x); // Prints 3.
console.log(obj); // Prints { x: 3 }.

const key = "y";
obj[key] = 5;
console.log(obj[key]); // Prints 5.
console.log(obj); // Prints { x: 3, y: 5 }.
0

Đối với các giá trị thuộc tính, toán tử

let x;
const y = (x = f()); // Or equivalently: const y = x = f();
console.log(y); // Logs the return value of the assignment x = f().

console.log(x = f()); // Logs the return value directly.

// An assignment expression can be nested in any place
// where expressions are generally allowed,
// such as array literals' elements or as function calls' arguments.
console.log([ 0, x = f(), 0 ]);
console.log(f(0, x = f(), 0));
2 trả về loại giá trị mà thuộc tính chứa:

const obj = {};

obj.x = 3;
console.log(obj.x); // Prints 3.
console.log(obj); // Prints { x: 3 }.

const key = "y";
obj[key] = 5;
console.log(obj[key]); // Prints 5.
console.log(obj); // Prints { x: 3, y: 5 }.
1

Đối với các phương thức và chức năng, toán tử

let x;
const y = (x = f()); // Or equivalently: const y = x = f();
console.log(y); // Logs the return value of the assignment x = f().

console.log(x = f()); // Logs the return value directly.

// An assignment expression can be nested in any place
// where expressions are generally allowed,
// such as array literals' elements or as function calls' arguments.
console.log([ 0, x = f(), 0 ]);
console.log(f(0, x = f(), 0));
2 trả về kết quả như sau:

const obj = {};

obj.x = 3;
console.log(obj.x); // Prints 3.
console.log(obj); // Prints { x: 3 }.

const key = "y";
obj[key] = 5;
console.log(obj[key]); // Prints 5.
console.log(obj); // Prints { x: 3, y: 5 }.
2

Đối với các đối tượng được xác định trước, toán tử

let x;
const y = (x = f()); // Or equivalently: const y = x = f();
console.log(y); // Logs the return value of the assignment x = f().

console.log(x = f()); // Logs the return value directly.

// An assignment expression can be nested in any place
// where expressions are generally allowed,
// such as array literals' elements or as function calls' arguments.
console.log([ 0, x = f(), 0 ]);
console.log(f(0, x = f(), 0));
2 trả về kết quả như sau:

const obj = {};

obj.x = 3;
console.log(obj.x); // Prints 3.
console.log(obj); // Prints { x: 3 }.

const key = "y";
obj[key] = 5;
console.log(obj[key]); // Prints 5.
console.log(obj); // Prints { x: 3, y: 5 }.
3

void

Toán tử

operator operand
operand operator
58 được sử dụng theo một trong các cách sau:

const obj = {};

obj.x = 3;
console.log(obj.x); // Prints 3.
console.log(obj); // Prints { x: 3 }.

const key = "y";
obj[key] = 5;
console.log(obj[key]); // Prints 5.
console.log(obj); // Prints { x: 3, y: 5 }.
4

Toán tử

operator operand
operand operator
58 chỉ định một biểu thức được đánh giá mà không trả lại giá trị.
operator operand
operand operator
60 là một biểu thức JavaScript để đánh giá. Các dấu ngoặc đơn xung quanh biểu thức là tùy chọn, nhưng đó là phong cách tốt để sử dụng chúng.

Nhà khai thác quan hệ

Một toán tử quan hệ so sánh các toán hạng của nó và trả về giá trị boolean dựa trên việc so sánh có đúng hay không.

in

Toán tử

operator operand
operand operator
61 trả về
operator operand
operand operator
42 nếu thuộc tính được chỉ định nằm trong đối tượng được chỉ định. Cú pháp là:

const obj = {};

obj.x = 3;
console.log(obj.x); // Prints 3.
console.log(obj); // Prints { x: 3 }.

const key = "y";
obj[key] = 5;
console.log(obj[key]); // Prints 5.
console.log(obj); // Prints { x: 3, y: 5 }.
5

trong đó

operator operand
operand operator
63 là một chuỗi, số hoặc biểu thức biểu tượng biểu thị tên thuộc tính hoặc chỉ mục mảng và
operator operand
operand operator
64 là tên của một đối tượng.

Các ví dụ sau đây cho thấy một số cách sử dụng của toán tử

operator operand
operand operator
61.

const obj = {};

obj.x = 3;
console.log(obj.x); // Prints 3.
console.log(obj); // Prints { x: 3 }.

const key = "y";
obj[key] = 5;
console.log(obj[key]); // Prints 5.
console.log(obj); // Prints { x: 3, y: 5 }.
6

ví dụ

Toán tử

operator operand
operand operator
66 trả về
operator operand
operand operator
42 nếu đối tượng được chỉ định thuộc loại đối tượng được chỉ định. Cú pháp là:

const obj = {};

obj.x = 3;
console.log(obj.x); // Prints 3.
console.log(obj); // Prints { x: 3 }.

const key = "y";
obj[key] = 5;
console.log(obj[key]); // Prints 5.
console.log(obj); // Prints { x: 3, y: 5 }.
7

trong đó

operator operand
operand operator
64 là tên của đối tượng so sánh với
operator operand
operand operator
69 và
operator operand
operand operator
69 là loại đối tượng, chẳng hạn như
operator operand
operand operator
71 hoặc
operator operand
operand operator
72.

Sử dụng

operator operand
operand operator
66 khi bạn cần xác nhận loại đối tượng trong thời gian chạy. Ví dụ: khi bắt các ngoại lệ, bạn có thể phân nhánh theo mã xử lý ngoại lệ khác nhau tùy thuộc vào loại ngoại lệ ném.

Ví dụ: mã sau sử dụng

operator operand
operand operator
66 để xác định xem
operator operand
operand operator
75 có phải là đối tượng
operator operand
operand operator
71 hay không. Vì
operator operand
operand operator
75 là đối tượng
operator operand
operand operator
71, các câu lệnh trong câu lệnh
operator operand
operand operator
79 thực thi.

const obj = {};

obj.x = 3;
console.log(obj.x); // Prints 3.
console.log(obj); // Prints { x: 3 }.

const key = "y";
obj[key] = 5;
console.log(obj[key]); // Prints 5.
console.log(obj); // Prints { x: 3, y: 5 }.
8

Biểu thức cơ bản

Tất cả các nhà khai thác cuối cùng hoạt động trên một hoặc nhiều biểu thức cơ bản. Những biểu thức cơ bản này bao gồm các định danh và nghĩa đen, nhưng cũng có một vài loại khác. Chúng được giới thiệu ngắn gọn dưới đây, và ngữ nghĩa của chúng được mô tả chi tiết trong các phần tham chiếu tương ứng của họ.

this

Sử dụng từ khóa

operator operand
operand operator
80 để tham khảo đối tượng hiện tại. Nói chung,
operator operand
operand operator
80 đề cập đến đối tượng gọi trong một phương thức. Sử dụng
operator operand
operand operator
80 với DOT hoặc ký hiệu khung:

const obj = {};

obj.x = 3;
console.log(obj.x); // Prints 3.
console.log(obj); // Prints { x: 3 }.

const key = "y";
obj[key] = 5;
console.log(obj[key]); // Prints 5.
console.log(obj); // Prints { x: 3, y: 5 }.
9

Giả sử một hàm gọi là

operator operand
operand operator
83 xác thực thuộc tính ____284 của đối tượng, được đưa ra đối tượng và các giá trị cao và thấp:

const val = 0;
val.x = 3;

console.log(val.x); // Prints undefined.
console.log(val); // Prints 0.
0

Bạn có thể gọi

operator operand
operand operator
83 trong mỗi trình xử lý sự kiện
operator operand
operand operator
86 của Biểu mẫu, sử dụng
operator operand
operand operator
80 để chuyển nó đến phần tử biểu mẫu, như trong ví dụ sau:

const val = 0;
val.x = 3;

console.log(val.x); // Prints undefined.
console.log(val); // Prints 0.
1

Nhà điều hành nhóm

Toán tử nhóm

operator operand
operand operator
88 kiểm soát mức độ ưu tiên của việc đánh giá trong các biểu thức. Ví dụ: bạn có thể ghi đè lên nhân và phân chia trước, sau đó bổ sung và trừ để đánh giá bổ sung trước.

const val = 0;
val.x = 3;

console.log(val.x); // Prints undefined.
console.log(val); // Prints 0.
2

new

Bạn có thể sử dụng toán tử

operator operand
operand operator
89 để tạo một thể hiện của loại đối tượng do người dùng xác định hoặc của một trong các loại đối tượng tích hợp. Sử dụng
operator operand
operand operator
89 như sau:

const val = 0;
val.x = 3;

console.log(val.x); // Prints undefined.
console.log(val); // Prints 0.
3

super

Từ khóa

operator operand
operand operator
91 được sử dụng để gọi các hàm trên cha mẹ của đối tượng. Nó rất hữu ích với các lớp để gọi hàm tạo cha mẹ chẳng hạn.

const val = 0;
val.x = 3;

console.log(val.x); // Prints undefined.
console.log(val); // Prints 0.
4

  • " Trước
  • Tiếp theo "

Có bao nhiêu toán tử logic?

Có ba toán tử logic: và, hoặc, không.Các ngữ nghĩa (có nghĩa) của các toán tử này tương tự như ý nghĩa của chúng trong tiếng Anh.three logical operators: and , or , and not . The semantics (meaning) of these operators is similar to their meaning in English.

Có bao nhiêu nhà khai thác trong JavaScript?

Các nhà khai thác JavaScript phong phú như những gì bạn mong đợi từ bất kỳ ngôn ngữ hiện đại nào.Có bốn loại: số học, so sánh, gán và logic.....

3 toán tử logic trong JavaScript là gì?

Các toán tử logic được sử dụng để tìm logic giữa các biến trong JavaScript.Có ba toán tử logic trong JavaScript: ||(Hoặc), && (và),! (Không).|| (OR), && (AND), ! (NOT).

7 toán tử JavaScript phổ biến nhất là gì?

Đây là những nhà khai thác khác nhau mà JavaScript hỗ trợ:..
Toán tử số học ..
Người vận hành chuyển nhượng ..
Toán tử chuỗi ..
Toán tử so sánh..
Toán tử logic ..
Các nhà khai thác bitwise ..
Các nhà khai thác đặc biệt ..