Đối tượng arguments là các tham số truyền vào hàm. Nó là biến truy cập được trong mọi hàm. Giả sử hai tham số được truyền vào hàm, bạn truy cập như sau:
arguments[0] arguments[1]
Tương tự, bạn có thể dùng typeof với arguments trong JavaScript. Trước tiên, xem cách dùng toán tử typeof. Toán tử typeof là toán tử đơn ngôi đặt trước toán hạng đơn lẻ, có thể thuộc bất kỳ kiểu nào.
Ví dụ: typeof cơ bản
<html>
<body>
<script>
var a = 20;
var b = "String";
var linebreak = "<br />";
result = (typeof b == "string" ? "B is String" : "B is Numeric");
document.write("Result => ");
document.write(result);
document.write(linebreak);
result = (typeof a == "string" ? "A is String" : "A is Numeric");
document.write("Result => ");
document.write(result);
document.write(linebreak);
</script>
</body>
</html>
typeof arguments sẽ trả về "object":
document.write(typeof arguments);
Nếu bạn có hai tham số, dùng typeof để tham chiếu đến từng phần tử:
document.write(typeof arguments[0]); document.write(typeof arguments[1]);