1、typeof:把一种数据类型的变量转换成另外一种数据类型。
 
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script>
        var num = 10;
        console.log(typeof num); 
        var str = 'Echo';
        console.log(typeof str); 
        var flag = true;
        console.log(typeof flag); 
        var vari = undefined;
        console.log(typeof vari); 
        var timer = null;
        console.log(typeof timer); 
        
        var age = prompt('请输入您的年龄');
        console.log(age);
        console.log(typeof age);
    </script>
</head>
<body>
</body>
</html>
 
2、数据类型转换
 
2.1 转换为字符串类型=
 
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script>
        
        var num = 10;
        var str = num.toString();
        console.log(str);
        console.log(typeof str);
        
        console.log(String(num));
        
        console.log(num + '');
    </script>
</head>
<body>
</body>
</html>
 
2.2 转换为数字型
 
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script>
        
        var age = prompt('请输入您的年龄');
        console.log(parseInt(age));
        console.log(parseInt('3.14')); 
        console.log(parseInt('3.94')); 
        console.log(parseInt('120px')); 
        console.log(parseInt('rem120px')); 
        
        console.log(parseFloat('3.14')); 
        console.log(parseFloat('120px')); 
        console.log(parseFloat('rem120px')); 
        
        var str = '123';
        console.log(Number(str));
        console.log(Number('12'));
        
        console.log('12' - 0); 
        console.log('123' - '120');
        console.log('123' * 1);
    </script>
</head>
<body>
</body>
</html>
 
2.3 转换为布尔型:
 

 
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script>
        console.log(Boolean('')); 
        console.log(Boolean(0)); 
        console.log(Boolean(NaN)); 
        console.log(Boolean(null)); 
        console.log(Boolean(undefined)); 
        console.log('------------------------------');
        console.log(Boolean('123'));
        console.log(Boolean('你好吗'));
        console.log(Boolean('我很好'));
    </script>
</head>
<body>
</body>
</html>