🏠 Quay trở lại trang chủ
Làm thế nào có thể xác định xem một giá trị biến là một số?
⭐️ NEW JAVASCRIPT COURSE launching in November! ⭐️Chúng tôi có nhiều cách khác nhau để kiểm tra xem một giá trị là một số.
Đầu tiên là
typeof 1 //'number'
const value = 2
typeof value //'number'
2, một biến toàn cầu, được gán cho đối tượng typeof 1 //'number'
const value = 2
typeof value //'number'
3 trong trình duyệt:const value = 2
isNaN[value] //false
isNaN['test'] //true
isNaN[{}] //true
isNaN[1.2] //false
Nếu
typeof 1 //'number'
const value = 2
typeof value //'number'
2 trả về sai, giá trị là một số.is a number.Một cách khác là sử dụng toán tử
typeof 1 //'number'
const value = 2
typeof value //'number'
5. Nó trả về chuỗi typeof 1 //'number'
const value = 2
typeof value //'number'
6 nếu bạn sử dụng nó trên một giá trị số:typeof 1 //'number'
const value = 2
typeof value //'number'
Vì vậy, bạn có thể thực hiện kiểm tra có điều kiện như thế này:
const value = 2
if [typeof value === 'number'] {
//it's a number
}
Kiểm tra xem giá trị có phải là số # không
Bạn có thể kiểm tra xem giá trị có phải là số theo ba cách không:
5 - Nếu giá trị là một số, chuỗitypeof 1 //'number' const value = 2 typeof value //'number'
8 được trả về.typeof 1 //'number' const value = 2 typeof value //'number'
9 - Nếu giá trị là một số,typeof 1 //'number' const value = 2 typeof value //'number'
0 được trả về.const value = 2 if [typeof value === 'number'] { //it's a number }
2 - Nếu giá trị là một số,typeof 1 //'number' const value = 2 typeof value //'number'
2 được trả về.const value = 2 if [typeof value === 'number'] { //it's a number }
Sử dụng toán tử
5 để kiểm tra xem giá trị có phải là số trong JavaScript không, ví dụ: typeof 1 //'number'
const value = 2
typeof value //'number'
4. Toán tử const value = 2
if [typeof value === 'number'] {
//it's a number
}
5 sẽ trả về chuỗi typeof 1 //'number'
const value = 2
typeof value //'number'
8 nếu giá trị là số loại.typeof 1 //'number'
const value = 2
typeof value //'number'
Copied!
const myVar = 5; if [typeof myVar === 'number'] { console.log['✅ value is a number']; } else { console.log['⛔️ value is NOT a number']; }
Chúng tôi đã sử dụng toán tử loại để kiểm tra xem giá trị có phải là số không.
Toán tử
typeof 1 //'number'
const value = 2
typeof value //'number'
5 trả về một chuỗi cho biết loại giá trị. Dưới đây là một số ví dụ.Copied!
console.log[typeof 5]; // 👉️ "number" console.log[typeof '5']; // 👉️ "string" console.log[typeof NaN]; // 👉️ "number" console.log[typeof {}]; // 👉️ "object" console.log[typeof []]; // 👉️ "object" console.log[typeof null]; // 👉️ "object"
Nếu bạn nhìn kỹ, bạn sẽ nhận thấy rằng NAN [không phải là số] cũng thuộc loại
const value = 2
if [typeof value === 'number'] {
//it's a number
}
8.Sử dụng phương thức
const value = 2
if [typeof value === 'number'] {
//it's a number
}
9 để đảm bảo các giá trị 0 không đáp ứng điều kiện.Copied!
const myVar = 5; if [typeof myVar === 'number'] { console.log['✅ value is a number']; } else { console.log['⛔️ value is NOT a number']; }
Copied!
const myVar = 5; if [typeof myVar === 'number' && !Number.isNaN[myVar]] { console.log['✅ value is a number']; } else { console.log['⛔️ value is NOT a number']; }
Chúng tôi đã sử dụng toán tử logic và [&&] để chuỗi điều kiện thứ hai. Cả hai điều kiện phải trả về giá trị sự thật cho khối
1 của chúng tôi để chạy.Copied!
const myVar = 5; if [typeof myVar === 'number'] { console.log['✅ value is a number']; } else { console.log['⛔️ value is NOT a number']; }
Lưu ý rằng chúng tôi đã sử dụng toán tử logic không [!] Để phủ nhận cuộc gọi đến phương thức
9. Toán tử không lấy giá trị trả về của phương thức, chuyển đổi nó thành boolean và lật nó.const value = 2
if [typeof value === 'number'] {
//it's a number
}
Phương thức số.
Dưới đây là một số ví dụ:
Copied!
console.log[Number.isNaN[NaN]]; // 👉️ true console.log[Number.isNaN[Number.NaN]]; // 👉️ true console.log[Number.isNaN[0 / 0]]; // 👉️ true console.log[Number.isNaN[5]]; // 👉️ false console.log[Number.isNaN['5']]; // 👉️ false console.log[Number.isNaN[{}]]; // 👉️ false
Chỉ
0,Copied!
const myVar = 5; if [typeof myVar === 'number'] { console.log['✅ value is a number']; } else { console.log['⛔️ value is NOT a number']; }
6 vàCopied!
const myVar = 5; if [typeof myVar === 'number'] { console.log['✅ value is a number']; } else { console.log['⛔️ value is NOT a number']; }
7 trả vềCopied!
const myVar = 5; if [typeof myVar === 'number'] { console.log['✅ value is a number']; } else { console.log['⛔️ value is NOT a number']; }
const value = 2
if [typeof value === 'number'] {
//it's a number
}
0.Nếu chúng ta biết rằng giá trị có một loại Copied!
8 và nó không phải là const value = 2
if [typeof value === 'number'] {
//it's a number
}
0, thì đó là một số hợp lệ.const myVar = 5;
if [typeof myVar === 'number'] {
console.log['✅ value is a number'];
} else {
console.log['⛔️ value is NOT a number'];
}
Kiểm tra xem giá trị có phải là số sử dụng số hay không.
Sử dụng phương thức Copied! Copied! Copied!
9 để kiểm tra xem giá trị có phải là số không, ví dụ: typeof 1 //'number'
const value = 2
typeof value //'number'
2. Phương thức trả về console.log[typeof 5]; // 👉️ "number"
console.log[typeof '5']; // 👉️ "string"
console.log[typeof NaN]; // 👉️ "number"
console.log[typeof {}]; // 👉️ "object"
console.log[typeof []]; // 👉️ "object"
console.log[typeof null]; // 👉️ "object"
0 nếu giá trị được cung cấp là loại const value = 2
if [typeof value === 'number'] {
//it's a number
}
8 và không dương hoặc âm const value = 2
if [typeof value === 'number'] {
//it's a number
}
5 hoặc console.log[typeof 5]; // 👉️ "number"
console.log[typeof '5']; // 👉️ "string"
console.log[typeof NaN]; // 👉️ "number"
console.log[typeof {}]; // 👉️ "object"
console.log[typeof []]; // 👉️ "object"
console.log[typeof null]; // 👉️ "object"
0.const myVar = 5;
if [typeof myVar === 'number'] {
console.log['✅ value is a number'];
} else {
console.log['⛔️ value is NOT a number'];
}
Copied!
const myVar = 5; if [Number.isFinite[myVar]] { console.log['✅ value is a number']; } else { console.log['⛔️ value is NOT a number']; }
Chúng tôi đã sử dụng phương thức số .isfinite để kết hợp các điều kiện từ ví dụ trước.
Phương thức
typeof 1 //'number'
const value = 2
typeof value //'number'
9 kiểm tra xem giá trị được cung cấp:- thuộc loại
8const value = 2 if [typeof value === 'number'] { //it's a number }
- không tích cực hoặc tiêu cực
5Copied!
console.log[typeof 5]; // 👉️ "number" console.log[typeof '5']; // 👉️ "string" console.log[typeof NaN]; // 👉️ "number" console.log[typeof {}]; // 👉️ "object" console.log[typeof []]; // 👉️ "object" console.log[typeof null]; // 👉️ "object"
- không phải là
0Copied!
const myVar = 5; if [typeof myVar === 'number'] { console.log['✅ value is a number']; } else { console.log['⛔️ value is NOT a number']; }
Dưới đây là một số ví dụ:
Copied!
console.log[Number.isFinite[5]]; // 👉️ true console.log[Number.isFinite[5.5]]; // 👉️ true console.log[Number.isFinite['5']]; // 👉️ false console.log[Number.isFinite[NaN]]; // 👉️ false console.log[Number.isFinite[null]]; // 👉️ false
Chỉ Copied! Copied! Copied!
0, const myVar = 5;
if [typeof myVar === 'number'] {
console.log['✅ value is a number'];
} else {
console.log['⛔️ value is NOT a number'];
}
6 và const myVar = 5;
if [typeof myVar === 'number'] {
console.log['✅ value is a number'];
} else {
console.log['⛔️ value is NOT a number'];
}
7 trả về const myVar = 5;
if [typeof myVar === 'number'] {
console.log['✅ value is a number'];
} else {
console.log['⛔️ value is NOT a number'];
}
0.const value = 2
if [typeof value === 'number'] {
//it's a number
}
Nếu chúng ta biết rằng giá trị có một loại const value = 2
if [typeof value === 'number'] {
//it's a number
}
8 và nó không phải là Copied!
const myVar = 5;
if [typeof myVar === 'number'] {
console.log['✅ value is a number'];
} else {
console.log['⛔️ value is NOT a number'];
}
0, thì đó là một số hợp lệ.
const value = 2
if [typeof value === 'number'] {
//it's a number
}
Copied!
const myVar = 5;
if [typeof myVar === 'number'] {
console.log['✅ value is a number'];
} else {
console.log['⛔️ value is NOT a number'];
}
Kiểm tra xem giá trị có phải là số sử dụng số hay không.
- Sử dụng phương thức
9 để kiểm tra xem giá trị có phải là số không, ví dụ:typeof 1 //'number' const value = 2 typeof value //'number'
2. Phương thức trả vềCopied!
console.log[typeof 5]; // 👉️ "number" console.log[typeof '5']; // 👉️ "string" console.log[typeof NaN]; // 👉️ "number" console.log[typeof {}]; // 👉️ "object" console.log[typeof []]; // 👉️ "object" console.log[typeof null]; // 👉️ "object"
0 nếu giá trị được cung cấp là loạiconst value = 2 if [typeof value === 'number'] { //it's a number }
8 và không dương hoặc âmconst value = 2 if [typeof value === 'number'] { //it's a number }
5 hoặcCopied!
console.log[typeof 5]; // 👉️ "number" console.log[typeof '5']; // 👉️ "string" console.log[typeof NaN]; // 👉️ "number" console.log[typeof {}]; // 👉️ "object" console.log[typeof []]; // 👉️ "object" console.log[typeof null]; // 👉️ "object"
0.Copied!
const myVar = 5; if [typeof myVar === 'number'] { console.log['✅ value is a number']; } else { console.log['⛔️ value is NOT a number']; }
- Chúng tôi đã sử dụng phương thức số .isfinite để kết hợp các điều kiện từ ví dụ trước.
Phương thức
9 kiểm tra xem giá trị được cung cấp:typeof 1 //'number'
const value = 2
typeof value //'number'
Copied!
// ⛔️ Don't do this const myVar = 5; if [!isNaN[myVar]] { console.log['value might be a number']; } else { console.log['value is NOT a number']; }
thuộc loại
const value = 2
if [typeof value === 'number'] {
//it's a number
}
8to check if the passed in value can be coerced to a
number.không tích cực hoặc tiêu cực
5Copied!
console.log[typeof 5]; // 👉️ "number" console.log[typeof '5']; // 👉️ "string" console.log[typeof NaN]; // 👉️ "number" console.log[typeof {}]; // 👉️ "object" console.log[typeof []]; // 👉️ "object" console.log[typeof null]; // 👉️ "object"
typeof 1 //'number'
const value = 2
typeof value //'number'
0không phải là Copied!
0const myVar = 5;
if [typeof myVar === 'number'] {
console.log['✅ value is a number'];
} else {
console.log['⛔️ value is NOT a number'];
}
Phương pháp Copied!
1 không được sử dụng phổ biến như toán tử const myVar = 5;
if [typeof myVar === 'number' && !Number.isNaN[myVar]] {
console.log['✅ value is a number'];
} else {
console.log['⛔️ value is NOT a number'];
}
5. Nếu bạn sử dụng nó trong mã của mình, bạn có thể gây bất ngờ cho khá nhiều nhà phát triển.typeof 1 //'number'
const value = 2
typeof value //'number'
typeof 1 //'number'
const value = 2
typeof value //'number'
1Kiểm tra xem giá trị có phải là số sử dụng nan # không
Có 2 phương thức Copied!
3 trong JavaScript:const myVar = 5;
if [typeof myVar === 'number' && !Number.isNaN[myVar]] {
console.log['✅ value is a number'];
} else {
console.log['⛔️ value is NOT a number'];
}
Copied!
const myVar = 5;
if [typeof myVar === 'number' && !Number.isNaN[myVar]] {
console.log['✅ value is a number'];
} else {
console.log['⛔️ value is NOT a number'];
}
4 - Kiểm tra xem giá trị được truyền có phải là loại const value = 2
if [typeof value === 'number'] {
//it's a number
}
8 không và là Copied!
const myVar = 5;
if [typeof myVar === 'number'] {
console.log['✅ value is a number'];
} else {
console.log['⛔️ value is NOT a number'];
}
0 [không ép buộc]
Copied!
const myVar = 5;
if [typeof myVar === 'number' && !Number.isNaN[myVar]] {
console.log['✅ value is a number'];
} else {
console.log['⛔️ value is NOT a number'];
}
const value = 2
if [typeof value === 'number'] {
//it's a number
}
Copied!
const myVar = 5;
if [typeof myVar === 'number'] {
console.log['✅ value is a number'];
} else {
console.log['⛔️ value is NOT a number'];
}
2 - ép buộc giá trị thành một số và kiểm tra xem giá trị làtypeof 1 //'number' const value = 2 typeof value //'number'
0Copied!
const myVar = 5; if [typeof myVar === 'number'] { console.log['✅ value is a number']; } else { console.log['⛔️ value is NOT a number']; }
- Bạn có thể thấy các ví dụ sử dụng phương pháp này, nhưng bạn không bao giờ nên sử dụng nó vì sau khi cảnh báo.