Several related...LOGIN

Several related functions of variables in JavaScript

Determine the data type of the variable:

##typeof()

  • Using typeof(), you can test the type of a variable.

  • The result of the typeof() test is a type string.

The result string of typeof() has several situations: "string", "number", "boolean", "undefined", "object", "function"

In addition: null, object, and array will all return "object".

<!DOCTYPE HTML>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <title>php.cn</title>
        <script>
            var x1 = "abc";     //string
            var x2 = 110;       //number
            var x3 = true;      //boolean
            var x4;             //undefined
            var x5 = null;      //object        
        //使用typeof()判断变量的数据类型
        var result = typeof(x5);
        //输出变量的类型和结果
        document.write(x5+"的数据类型为:"+result);
        </script>
    </head>
    <body>
    </body>
</html>

Note: You can try to determine the data types of several other variables


##Extract integers and floating point numbers from strings

parseInt() system function, global function

Function: Extract integers from left to right in a string. If non-integer content is encountered, the extraction is stopped and the result is returned.

Note: If the first character is a non-integer, it will stop immediately and return NaN.

<!DOCTYPE HTML>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <title>php.cn</title>
        <script>
            document.write(parseInt("500eps")+"<br/>");
            document.write(parseInt("500.88")+"<br/>");
            document.write(parseInt("a120px")+"<br/>");
        </script>
    </head>
    <body>
    </body>
</html>

parseFloat() system function, global function

Function: Extract floating point types from left to right in a string; encounter non-floating point types content, stop extracting and return the result.

Note: If the first character is a non-floating point type, it stops immediately and returns NaN.

<!DOCTYPE HTML>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <title>php.cn</title>
        <script>
            document.write(parseFloat("500eps")+"<br/>");
            document.write(parseFloat("500.88")+"<br/>");
            document.write(parseFloat("a120px")+"<br/>");
        </script>
    </head>
    <body>
    </body>
</html>


Next Section

<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>php.cn</title> <script> document.write(parseInt("500eps")+"<br/>"); document.write(parseInt("500.88")+"<br/>"); document.write(parseInt("a120px")+"<br/>"); </script> </head> <body> </body> </html>
submitReset Code
ChapterCourseware