js Grammar Basics (3)

3. Data Types

3.1. Data Types Learning Objectives

Popular speaking in front of us, and in fact, the data type of the data is classified, then, in the js in the end the data is divided into several categories? What is the name of these classes call? Each category below those values? These issues need to remember clearly, for example: the value of 8, we put it inside the class assigned to digital, digital class has the following characteristics is its values ​​are numbers, so if like the above can remember the name of the class, able to distinguish common which belong to a certain type of value, then the data type even mastered this knowledge

3.2.Number- digital type

var number1 = 15;
var number2 = 15.0

In other languages, there are integer and floating point argument, popular to say that integers and numbers with decimal is divided into two different types, but not strictly separate area in the js, it means that 15 == = 15.0

console.log(15 === 15.0) //true

It should be noted that, when you use floating-point (decimal) for computing, it is not very precise, use the time to be careful, for example:

console.log(1 - 0.7)  //0.30000000000000004

console.log(1 - 0.9) //0.09999999999999998

3.2.String- string type

Use "" or 'enclosed in string type data is data'. E.g:

var userName = "张三";  
var sex = "女";

String type Features:

1. The single quotes are not directly nested single quotes, double quotes nested double quotes are not directly, but they may be nested within each other, for example:

var html_template = '<div style="color:red">螺钉课堂,nodeing.com</div>'

2. The length of the string has property

console.log(html_template.length)

3. string contains zero or more characters, if nothing is written within quotation marks represent an empty string, for example:

var str = ''
console.log(str.length)

3.3.Boolean- Boolean

Boolean value refers to the true and false, on or off, yes or no, this type has only two values, the keywords true and false. JavaScript comparative statements are usually the result of a Boolean value. E.g:

var a=5;
a==4;

Switch Application examples:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
        <style type="text/css">
            body{
                text-align: center;
            }
            #box{
                width: 100px;
                height: 100px;
                background-color: red;
                margin: 30px auto;
                display: none;
            }
        </style>

    </head>
    <body>
        <button id="btn">开关</button>
        <div id="box"></div>
        <script type="text/javascript">
            var oBtn = document.getElementById('btn')
            var oBox = document.getElementById('box')
            var flag = true
            oBtn.onclick = function(){
                if(flag === false){
                    oBox.style.display = "none"
                    flag = true
                }else{
                    oBox.style.display = "block"
                    flag = false
                }
            }
        </script>
    </body>
</html>

3.4.null

null is a special keyword indicates that null values. JavaScript is case-sensitive, and therefore null and Null, NULL, or other variables completely different.

null usually means "no object", i.e., there should not have a value,

3.5.undefined

undefined undefined variables represent attributes, can be understood as defined, namely "lack of values" is here should have a value, but not yet defined, for example:

var a ;
console.log(a)

3.6. Type Conversion

3.6.1. Why typecast?

Convert one type into another data type, the type of conversion is, for example: "3" is converted into a digital string type 3. Type since it has been divided, then why do you need to convert between types of it? Operation between data, only carried out between the same type of data operation makes sense, for example, "a" +13 belong string and numeric types are added, if not converted, then in the end result should be equal to what it? Therefore, if the operation to be performed between the data, different types of data need to be converted for the same type of operation. Data type conversion is divided into two, one is cast, one is automatic conversion

3.6.2. Cast

Cast method is to use a fixed force converting some type to another type.

1. cast into a digital type

var a="13";
a = parseInt(a); //使用函数parseInt 将字符串“13”,转换成数字类型13
console.log(typeof a)  //Number

var a = "13.123";
a = parseInt(a);
console.log(a); //输出结果 13

var a=“13absdf123”;
a=parseInt(a);
console.log(a);  //输出结果 13;

var  a=“a13” ;
a=parseInt(a);
console.log(a);  //输出 NaN 表示不是一个数字

Summary: parseInt function to convert to an integer variable, if the variable is numeric string, is directly converted into an integer, if the variable value string both numbers, then the previous string will transitioned to an integer number, and discarding the latter string, for example: 13adf will adf removed, directly back to 13. If the number is in the middle of a string, for example: a23a, then the final value is converted into a digital NaN.NaN type, shows the result of the conversion not a number

var a = "13.123";
a = parseFloat(a);
console.log(a); //输出结果 13.123

Summary: parseFloat and parseInt usage of the same, the difference is parseFloat is to convert a decimal variable

var a = "13.123";
a = Number(a);
console.log(a); //输出结果 13.123

var a = "13.123a";
a = parseFloat(a);
console.log(a); //输出结果 NaN

Summary: Number function is converted into a digital variable, and that the above two different functions, turn only pure Number The number of characters, if as long as with other characters, the string will be converted into a NaN.

2. cast to string type

var a=12;
a= String(a);
console.log(typeof a);  //输出string

Summary: into a string is relatively simple, just use the String (variable names) on it

3. cast to type Boolean

Because Boolean value either true or false, most conversions are true, only the conversion of data into a small number of special leave, so Boolean value, you only need to remember which converted into a false value on the line

var a=12;
var b=0
a= Boolean(a);
b= Boolean(b);
console.log( a);  //true
console.log(b);   //false

Summary: The few cases of false value is converted into (flase), there are numbers 0, -0, 'empty string, null, undefined, NaN, other data will be converted to true

3.6.3. Automatic type conversion

Automatic type conversion during JavaScript program is running, the context of the environment, automatically convert into a unified type of operation types. E.g:

<script type="text/javascript">
    var value1 = "2";
    var value2 = "1" 

    //减法的语意: 将会按照数字进行运算
    var value3 = value1 - value2;
    console.log(value3); // 输出1
    //加法: 将会按照数字进行运算
    var value4 = true;
    var value5 = value1 - value4;
    console.debug(value5); //输出1
</script>

Summary: The above code, when value1-value2 is the time to do subtraction, so will the string value1 and value2 strings are converted to numbers operation, empathy, value1-value4 too, are converted into numbers operation and automatic conversion, conversion basis is being done subtraction, subtraction both sides must be a number to be automatic type conversion usage scenarios are still many conditions such as back to learn the judge sentences if () the contents of brackets is automatic conversion of the

3.7. Job

1, the variable naming convention handwriting times -10 5 times

2, chapter to learn several types of data? What are the characteristics? Please summarize in your own words

3, the case casts Hou Buer of what is false?

4, several methods of converting digital type, respective What are the characteristics?

5, the following data are respectively converted into numeric, boolean, character

var a=“abc”
var a = “123”
var a = true;
var a = undefined;
var a = null

6, converts the data into the following values ​​(using both methods for conversion)

var a = “10px”
var a = “.123”;
var a = “a45px”;

Screw classroom video lessons Address: http://edu.nodeing.com

Guess you like

Origin www.cnblogs.com/dadifeihong/p/12027508.html