JavaScript sử dụng typeof để kiểm tra dữ liệu có phải là số? How to use typeof check variable is a number in JavaScript?

Typeof() là một toán tử của JavaScript (JavaScript Operator) được dugn2 để xác định tên của nhưng kiểu dữ liệu (Data Types).

Source check valid number with typeof:

<html>
<head>
<title>VNLIVES.NET</title>
</head>
<body>
<h1>JavaScript : typeof example</h1>

<script type="text/javascript">
 var num1 = 100;

 if(typeof num1 == 'number'){
    document.write(num1 + " is a number <br/>");
 }else{
    document.write(num1 + " is not a number <br/>");
 }

 var num2 = 'VNLIVES.NET';

 if(typeof num2 == 'number'){
    document.write(num2 + " is a number <br/>");
 }else{
    document.write(num2 + " is not a number <br/>");
 }
</script>

</body>
</html>
</html>


Demo check valid number with typeof:






No comments:

Post a Comment