Hàm parseInt() chuyển chuỗi thành số nguyên với cơ số (radix) từ 2 đến 36.
parseInt(string, radix);
- string: Giá trị cần parse. Không phải chuỗi sẽ được chuyển bằng
ToString(). Bỏ qua khoảng trắng đầu. - radix: Cơ số (2–36). Nếu bỏ qua, JS đoán: "0x"→16, "0"→8 (deprecated), khác→10.
Ví dụ
console.log(parseInt("100", 10)); // 100
console.log(parseInt("10", 8)); // 8
console.log(parseInt("101", 2)); // 5
console.log(parseInt("2FF3", 16)); // 12275
console.log(parseInt("ZZ", 36)); // 1295
Lưu ý: Luôn truyền radix để tránh hành vi ngầm (JSHint báo lỗi "missing radix parameter").