Là gì !

Đoạn mã của bạn trông giống như tham chiếu các phương thức từ một trong những thư viện JavaScript phổ biến (jQuery, ProtoType, mooTools, v.v.)

Không có gì bí ẩn về việc sử dụng $ trong JavaScript. $ chỉ đơn giản là một mã định danh JavaScript hợp lệ. JavaScript cho phép chữ hoa và chữ thường (trong nhiều loại chữ viết khác nhau, không chỉ tiếng Anh), số (nhưng không phải ở ký tự đầu tiên), $, _, v.v. ¹

Nguyên mẫu, jQuery và hầu hết các thư viện javascript sử dụng $ làm đối tượng (hoặc hàm) cơ sở chính. Hầu hết họ cũng có cách từ bỏ $ để nó có thể được sử dụng với một thư viện khác sử dụng nó. Trong trường hợp đó, bạn sử dụng jQuery thay vì $. Trên thực tế, $ chỉ là cách viết tắt của jQuery


¹ Đối với ký tự đầu tiên của mã định danh, JavaScript cho phép ". bất kỳ điểm mã Unicode nào có thuộc tính Unicode “ID_Start”. " cộng với $_; chi tiết trong. Đối với các ký tự tiếp theo trong mã định danh, bất kỳ ký tự nào có $2 (bao gồm _) và $ (và một vài ký tự điều khiển để tương thích lịch sử)

Trong JavaScript, toán tử là một ký hiệu đặc biệt được sử dụng để thực hiện các thao tác trên toán hạng (giá trị và biến). Ví dụ,

2 + 3; // 5

Ở đây

const x = 5;
1 là toán tử thực hiện phép cộng, và
const x = 5;
2 và
const x = 5;
3 là toán hạng


Các loại toán tử JavaScript

Đây là danh sách các toán tử khác nhau mà bạn sẽ học trong hướng dẫn này

  • Toán tử gán
  • toán tử số học
  • Toán tử so sánh
  • Toán tử logic
  • toán tử Bitwise
  • Toán tử chuỗi
  • Toán tử khác

Toán tử gán JavaScript

Toán tử gán dùng để gán giá trị cho biến. Ví dụ,

const x = 5;

Ở đây, toán tử

const x = 5;
4 được sử dụng để gán giá trị
const x = 5;
5 cho biến
const x = 5;
6

Dưới đây là danh sách các toán tử gán thường được sử dụng

OperatorNameExample

const x = 5;
4Assignment operator
const x = 5;
8
const x = 5;
9Addition assignment
const number = 3 + 5; // 8
0
const number = 3 + 5; // 8
1Subtraction Assignment
const number = 3 + 5; // 8
2
const number = 3 + 5; // 8
3Multiplication Assignment
const number = 3 + 5; // 8
4
const number = 3 + 5; // 8
5Division Assignment
const number = 3 + 5; // 8
6
const number = 3 + 5; // 8
7Remainder Assignment
const number = 3 + 5; // 8
8
const number = 3 + 5; // 8
9Exponentiation Assignment
let x = 5;
let y = 3;

// addition
console.log('x + y = ', x + y);  // 8

// subtraction
console.log('x - y = ', x - y);  // 2

// multiplication
console.log('x * y = ', x * y);  // 15

// division
console.log('x / y = ', x / y);  // 1.6666666666666667

// remainder
console.log('x % y = ', x % y);   // 2

// increment
console.log('++x = ', ++x); // x is now 6
console.log('x++ = ', x++); // prints 6 and then increased to 7
console.log('x = ', x);     // 7

// decrement
console.log('--x = ', --x); // x is now 6
console.log('x-- = ', x--); // prints 6 and then decreased to 5
console.log('x = ', x);     // 5

//exponentiation
console.log('x ** y =', x ** y);
0

Ghi chú. Toán tử gán thường được sử dụng là

const x = 5;
4. Bạn sẽ hiểu các toán tử gán khác như
const x = 5;
9,
const number = 3 + 5; // 8
1,
const number = 3 + 5; // 8
3, v.v. một khi chúng ta học toán tử số học


Toán tử số học JavaScript

Các toán tử số học được sử dụng để thực hiện các phép tính số học. Ví dụ,

const number = 3 + 5; // 8

Ở đây, toán tử

const x = 5;
1 được sử dụng để cộng hai toán hạng

OperatorNameExample

const x = 5;
1Addition
let x = 5;
let y = 3;

// addition
console.log('x + y = ', x + y);  // 8

// subtraction
console.log('x - y = ', x - y);  // 2

// multiplication
console.log('x * y = ', x * y);  // 15

// division
console.log('x / y = ', x / y);  // 1.6666666666666667

// remainder
console.log('x % y = ', x % y);   // 2

// increment
console.log('++x = ', ++x); // x is now 6
console.log('x++ = ', x++); // prints 6 and then increased to 7
console.log('x = ', x);     // 7

// decrement
console.log('--x = ', --x); // x is now 6
console.log('x-- = ', x--); // prints 6 and then decreased to 5
console.log('x = ', x);     // 5

//exponentiation
console.log('x ** y =', x ** y);
7
let x = 5;
let y = 3;

// addition
console.log('x + y = ', x + y);  // 8

// subtraction
console.log('x - y = ', x - y);  // 2

// multiplication
console.log('x * y = ', x * y);  // 15

// division
console.log('x / y = ', x / y);  // 1.6666666666666667

// remainder
console.log('x % y = ', x % y);   // 2

// increment
console.log('++x = ', ++x); // x is now 6
console.log('x++ = ', x++); // prints 6 and then increased to 7
console.log('x = ', x);     // 7

// decrement
console.log('--x = ', --x); // x is now 6
console.log('x-- = ', x--); // prints 6 and then decreased to 5
console.log('x = ', x);     // 5

//exponentiation
console.log('x ** y =', x ** y);
8Subtraction
let x = 5;
let y = 3;

// addition
console.log('x + y = ', x + y);  // 8

// subtraction
console.log('x - y = ', x - y);  // 2

// multiplication
console.log('x * y = ', x * y);  // 15

// division
console.log('x / y = ', x / y);  // 1.6666666666666667

// remainder
console.log('x % y = ', x % y);   // 2

// increment
console.log('++x = ', ++x); // x is now 6
console.log('x++ = ', x++); // prints 6 and then increased to 7
console.log('x = ', x);     // 7

// decrement
console.log('--x = ', --x); // x is now 6
console.log('x-- = ', x--); // prints 6 and then decreased to 5
console.log('x = ', x);     // 5

//exponentiation
console.log('x ** y =', x ** y);
9
const a = 3, b = 2;
console.log(a > b); // true 
0Multiplication
const a = 3, b = 2;
console.log(a > b); // true 
1
const a = 3, b = 2;
console.log(a > b); // true 
2Division
const a = 3, b = 2;
console.log(a > b); // true 
3
const a = 3, b = 2;
console.log(a > b); // true 
4Remainder
const a = 3, b = 2;
console.log(a > b); // true 
5
const a = 3, b = 2;
console.log(a > b); // true 
6Increment (increments by 1)
const a = 3, b = 2;
console.log(a > b); // true 
7 or
const a = 3, b = 2;
console.log(a > b); // true 
8
const a = 3, b = 2;
console.log(a > b); // true 
9Decrement (decrements by 1)
// equal operator
console.log(2 == 2); // true
console.log(2 == '2'); // true

// not equal operator
console.log(3 != 2); // true
console.log('hello' != 'Hello'); // true

// strict equal operator
console.log(2 === 2); // true
console.log(2 === '2'); // false

// strict not equal operator
console.log(2 !== '2'); // true
console.log(2 !== 2); // false
0 or
// equal operator
console.log(2 == 2); // true
console.log(2 == '2'); // true

// not equal operator
console.log(3 != 2); // true
console.log('hello' != 'Hello'); // true

// strict equal operator
console.log(2 === 2); // true
console.log(2 === '2'); // false

// strict not equal operator
console.log(2 !== '2'); // true
console.log(2 !== 2); // false
1
// equal operator
console.log(2 == 2); // true
console.log(2 == '2'); // true

// not equal operator
console.log(3 != 2); // true
console.log('hello' != 'Hello'); // true

// strict equal operator
console.log(2 === 2); // true
console.log(2 === '2'); // false

// strict not equal operator
console.log(2 !== '2'); // true
console.log(2 !== 2); // false
2Exponentiation (Power)
// equal operator
console.log(2 == 2); // true
console.log(2 == '2'); // true

// not equal operator
console.log(3 != 2); // true
console.log('hello' != 'Hello'); // true

// strict equal operator
console.log(2 === 2); // true
console.log(2 === '2'); // false

// strict not equal operator
console.log(2 !== '2'); // true
console.log(2 !== 2); // false
3


ví dụ 1. Toán tử số học trong JavaScript

let x = 5;
let y = 3;

// addition
console.log('x + y = ', x + y);  // 8

// subtraction
console.log('x - y = ', x - y);  // 2

// multiplication
console.log('x * y = ', x * y);  // 15

// division
console.log('x / y = ', x / y);  // 1.6666666666666667

// remainder
console.log('x % y = ', x % y);   // 2

// increment
console.log('++x = ', ++x); // x is now 6
console.log('x++ = ', x++); // prints 6 and then increased to 7
console.log('x = ', x);     // 7

// decrement
console.log('--x = ', --x); // x is now 6
console.log('x-- = ', x--); // prints 6 and then decreased to 5
console.log('x = ', x);     // 5

//exponentiation
console.log('x ** y =', x ** y);

Truy cập toán tử ++ và -- để tìm hiểu thêm

Ghi chú. Toán tử

// equal operator
console.log(2 == 2); // true
console.log(2 == '2'); // true

// not equal operator
console.log(3 != 2); // true
console.log('hello' != 'Hello'); // true

// strict equal operator
console.log(2 === 2); // true
console.log(2 === '2'); // false

// strict not equal operator
console.log(2 !== '2'); // true
console.log(2 !== 2); // false
2 đã được giới thiệu trong ECMAScript 2016 và một số trình duyệt có thể không hỗ trợ chúng. Để tìm hiểu thêm, hãy truy cập


Toán tử so sánh JavaScript

Toán tử so sánh so sánh hai giá trị và trả về một giá trị boolean, hoặc là

// equal operator
console.log(2 == 2); // true
console.log(2 == '2'); // true

// not equal operator
console.log(3 != 2); // true
console.log('hello' != 'Hello'); // true

// strict equal operator
console.log(2 === 2); // true
console.log(2 === '2'); // false

// strict not equal operator
console.log(2 !== '2'); // true
console.log(2 !== 2); // false
5 hoặc là
// equal operator
console.log(2 == 2); // true
console.log(2 == '2'); // true

// not equal operator
console.log(3 != 2); // true
console.log('hello' != 'Hello'); // true

// strict equal operator
console.log(2 === 2); // true
console.log(2 === '2'); // false

// strict not equal operator
console.log(2 !== '2'); // true
console.log(2 !== 2); // false
6. Ví dụ,

const a = 3, b = 2;
console.log(a > b); // true 

Ở đây, toán tử so sánh

// equal operator
console.log(2 == 2); // true
console.log(2 == '2'); // true

// not equal operator
console.log(3 != 2); // true
console.log('hello' != 'Hello'); // true

// strict equal operator
console.log(2 === 2); // true
console.log(2 === '2'); // false

// strict not equal operator
console.log(2 !== '2'); // true
console.log(2 !== 2); // false
7 được sử dụng để so sánh xem a có lớn hơn b hay không

Toán tử Mô tảVí dụ

const x = 5;
13đánh giá nhiều toán hạng và trả về giá trị của toán hạng cuối cùng.
const x = 5;
14
const x = 5;
15returns value based on the condition
const x = 5;
16
const x = 5;
17deletes an object's property, or an element of an array
const x = 5;
18
const x = 5;
19returns a string indicating the data type
const x = 5;
20
const x = 5;
21discards the expression's return value
const x = 5;
22
const x = 5;
23returns
// equal operator
console.log(2 == 2); // true
console.log(2 == '2'); // true

// not equal operator
console.log(3 != 2); // true
console.log('hello' != 'Hello'); // true

// strict equal operator
console.log(2 === 2); // true
console.log(2 === '2'); // false

// strict not equal operator
console.log(2 !== '2'); // true
console.log(2 !== 2); // false
5 if the specified property is in the object
const x = 5;
25
const x = 5;
26returns
// equal operator
console.log(2 == 2); // true
console.log(2 == '2'); // true

// not equal operator
console.log(3 != 2); // true
console.log('hello' != 'Hello'); // true

// strict equal operator
console.log(2 === 2); // true
console.log(2 === '2'); // false

// strict not equal operator
console.log(2 !== '2'); // true
console.log(2 !== 2); // false
5 if the specified object is of of the specified object type
const x = 5;
28

Là gì '. ' Trong JavaScript?

Bất đẳng thức (. = ) kiểm tra xem hai toán hạng của nó có bằng nhau hay không, trả về kết quả Boolean . Không giống như toán tử bất đẳng thức nghiêm ngặt, nó cố gắng chuyển đổi và so sánh các toán hạng thuộc các kiểu khác nhau.

$() trong thư viện jQuery là gì?

$ là một dạng ngắn của hàm jQuery . $() = jQuery() = cửa sổ. $() = cửa sổ. jQuery() $()/jQuery() là một chức năng chọn để chọn các phần tử DOM.

Dấu chấm hỏi kép trong JS là gì?

Dấu hỏi kép Javascript là toán tử logic nhận hai giá trị và trả về giá trị bên phải nếu giá trị bên trái không xác định hoặc null, nếu không thì trả về bên trái . Mức độ ưu tiên của toán tử đối với toán tử hợp nhất nullish là mức thấp thứ năm. . Operator precedence for the nullish coalescing operator is the fifth-lowest.

Từ khóa trong JS là gì?

Toán tử in trả về true nếu thuộc tính đã chỉ định nằm trong đối tượng đã chỉ định hoặc chuỗi nguyên mẫu của nó .