Data types of v...LOGIN

Data types of variables in JavaScript

Introduction to variable data types

#Variables have data types. This type comes from the "value of the variable", in other words: value What type is the variable?

The types of variables in JS are:

Numeric type, character type, Boolean type, undefined, null, array, object, function

This Eight data types, divided into two major categories:


  • ##Basic data types: numerical, character, Boolean Type, undefined type, empty type. Very notable feature: a variable name can only store one value.

Example: var a = 10;

  • Composite data types: arrays, objects, functions. Salient feature: a variable name may store multiple values.

Example: var arr = [10,20,30,40]

In this section we introduce the basic data types, and the composite data types will be discussed later. The chapter introduces in detail


Numerical type: the

numerical value that the variable can perform mathematical operations on Types include: integer, floating point, NaN.

var a = 100;

var a = 0.9;

var a = 0;

Note: There is another very special value type The value is NaN. NaN (not a number) is not a number.

When converting other data types into numeric types and it cannot be converted, but the program cannot report an error, a NaN value will be returned.

<!DOCTYPE HTML>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <title>php.cn</title>
        <script>
           //现在我们想让一个东西的长度变成原来的10倍
           var length = "300m";
           /*
           一个字符串,是不能转换成有意义的数值的,只能转换成NaN
           一个含纯数字的字符串,可以转成有意义的数值,大家可以修改length为纯数字的字符串,输出查看结果
           */
           length = length*10;
           document.write(length);
        </script>
    </head>
    <body>
    </body>
</html>



Character type: a string enclosed in single quotes or double quotes.

var a = "This is a string";

var b = "This is also a string";

var c = “”;

Single quotes and double quotes can be nested inside each other

  • Only double quotes can be nested within single quotes;

  • Only single quotes can be nested within double quotes.

  • <!DOCTYPE HTML>
    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
            <title>php.cn</title>
            <script type="text/javascript">
                var name = "小明";
                //加号为字符串连接符,我们之后会介绍
                var str = "我的名字叫做'" +name+"'"
                document.write(str)
            </script>
        </head>
        <body>
        </body>
    </html>
If you want to nest double quotes within double quotes, the double quotes inside must be escaped (\").

In JS The escape character is backslash (\).

Commonly used escape characters are: \', \”, \\, \r, \n, etc.

That is, when the browser encounters a backslash (\), it will treat the character after it as a normal character. The so-called "normal" characters are a, b, c, &, etc.

<!DOCTYPE HTML>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <title>php.cn</title>
        <script type="text/javascript">
            var name = "小明";
            //加号为字符串连接符,我们之后会介绍
            var str = "我的名字叫做\"" +name+"\""
            document.write(str)
        </script>
    </head>
    <body>
    </body>
</html>


Boolean type

Boolean type is also called logical type. There are only two values: true (true), false (false).

Boolean type has only two states. Such as: gender, marital status, light switch, whether it is blacklisted, etc.

var a = true;

var b = false;

Boolean type is commonly used in if conditional judgment statements (we will introduce conditional judgment statements in the following chapters, everyone first observes the results )

<!DOCTYPE HTML>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <title>php.cn</title>
        <script type="text/javascript">
           var x = 10;
           var y = 110;
           //x>y比较出来的结果是布尔值
           if(x>y){
               document.write(x+"比"+y+"大些");
           }else{
               document.write(y+"比"+x+"大些");
           }
        </script>
    </head>
    <body>
    </body>
</html>



Undefined type

When a variable is defined but not assigned a value, an undefined type will be returned. The value of an undefined type has only one undefined.

When the property of an object does not exist, undefined type is also returned.

<!DOCTYPE HTML>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <title>php.cn</title>
        <script type="text/javascript">
           var x;
           document.write(x);
        </script>
    </head>
    <body>
    </body>
</html>



##empty type

When an object does not exist, a null type will be returned, and the value of the null type is only null.

can also be understood as: a placeholder for an object.

If you want to clear the value of a variable, you can assign a null value.

var a = 100;

var a = null ; //Assign a null to a variable to clear its value

<!DOCTYPE HTML>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <title>php.cn</title>
        <script type="text/javascript">
            var x = null;
            document.write(x);
        </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> //现在我们想让一个东西的长度变成原来的10倍 var length = "300m"; /* 一个字符串,是不能转换成有意义的数值的,只能转换成NaN 一个含纯数字的字符串,可以转成有意义的数值,大家可以修改length为纯数字的字符串,输出查看结果 */ length = length*10; document.write(length); </script> </head> <body> </body> </html>
submitReset Code
ChapterCourseware