JavaScript_note 4 data types (Undefined, Null, Boolean, Number, String, Object)

ECMAScript variables (Var operator)

It is mentioned in the Red Book: ECMAScript variables are loosely typed. The loose type can be used to store any type of data. In other words, each variable is just a placeholder for storing the value.
First, how to define/initialize a variable with var:

var message;  // 定义了名叫message的变量,但未初始化
var message = 'hello'; // 定义了名叫message的变量,并保存了字符串'hello'

Secondly, the variables defined by the var operator are local variables

<script>
      function test() {
    
    
          var message = 'hi';
      }
      test(); //调用函数,创建变量message并赋值
      alert(message); //超出了message的作用域,出错
      //message相当于未声明的变量,只能执行typeof操作
</script>

to sum up:

  1. Var can define a variable, the variable can save any value, it is just a placeholder for saving the value ;
  2. If not initialized, the variable will save a value of undefined ;
  3. For variable initialization, the variable will hold a specific value, but it will not be marked as a certain type because it is a certain value. (Such as var message='hello'; the message will not be marked as a string type because it saves the string value'hello')
  4. The process of initializing variables can be understood as: just assign a value to a variable;
  5. The variables defined by the var operator are local variables.

ECMAScript data types

ECMAScript has 5 simple data types (basic data types), namely: Undefined, Null, Boolean, Number and String; there is a complex data type, Object.

typeof operator

The typeof operator returns one of the following strings:

  1. undefined (if undefined or uninitialized)
  2. boolean (if the value of the variable is a boolean)
  3. string (if the value of the variable is a string)
  4. number (if the value of the variable is a number)
  5. object (if the value of the variable is object or null)
  6. function (if the value of the variable is a function)

Verification returns undefined
1.1 Uninitialized The
variable message is defined in the test function, but it is not initialized;

    <script>
        function test() {
    
    
            var message;
            // var message = 'hello';
            // var message = true;
            // var message = 100;
            // var message = window;
            // var message = null;
            alert(typeof message);
        }
        test();
        // var message = test;
        // alert(typeof message);
    </script>

If it is not initialized, the string undefined is returned. As mentioned earlier: if you define a variable but do not initialize it, the variable saves a special value undefined;
Insert picture description here
1.2 Undefined situation After the
function test is executed, the var operator defines the variable message, initializes it, and saves the string value. hello'.

    <script>
        function test() {
    
    
            // var message;
            var message = 'hello';
            // var message = true;
            // var message = 100;
            // var message = window;
            // var message = null;
            // alert(typeof message);
        }
        test();
        // var message = test;
        alert(typeof message);
    </script>

The same will be undefined. It can indicate that the variable defined by the var operator is a local variable , or that it is an undefined variable. Using typeof will return the string undefined;
another point: undeclared variables can only perform typeof operations, and the
rest can be verified by yourself;
Another special note: null is a null object pointer, so typeof returns the string'object' . This will be further elaborated below.

Type①—Undefined type

The Undefined type has only one value-undefined.
If a variable is declared with the var operator and it is not initialized, the value saved by the variable is undefined.

        var age ;
        alert(age); // 分别执行,age未初始化保存的值是undefined
        alert(age == undefined); // 分别执行,返回 true
        alert(typeof age) // 返回字符串'undefined'
  		alert(age); // 分别执行,出错
        alert(age == undefined); // 分别执行,出错
        alert(typeof age); // 分别执行,返回字符串'undefined'

to sum up:

  1. Variables declared by var but not initialized will be saved as undefined;
  2. For declared but uninitialized variables, typeof will return the string'undefined';
  3. Undeclared variables can only perform typeof operations;
  4. For undeclared variables, the typeof operation will also return the string'undefined';

Type ②—Null type

The Null type only saves a special value, null; the
null value has two characteristics:

  1. Performing typeof on a null value will return'object', because null is a pointer to a null object
  2. null == undefined, will return true

As long as the variable intended to save the object has not actually saved the object, it is necessary to explicitly let the variable save the null value.

Type③—Boolean type

Boolean type summary:

  1. The Boolean type has two literal values, true and false .
  2. This literal value is case-sensitive. For example, neither True nor False is a Boolean value.
  3. All types of ECMAScript values ​​have equivalent values ​​to Boolean values, that is, they can be transformed with the transformation function Boolean().

Using the Boolean() function for any data type value will return a Boolean value. As for whether this Boolean value is true or false, it depends on the data type of the value and its actual value. The following are the conversion rules of the conversion function Boolean():

  1. Undefined type, the value is only undefined, converted to false
  2. Boolean type, the value of true is converted to true, the value of false is converted to false
  3. Number type, the value is converted to true for any non-zero number value, NaN and 0 are converted to false
  4. String type, the value of any non-empty string is converted to true, and an empty string is converted to false
  5. Object type, the value is turned to true for any object, and the value is turned to false

Therefore, there is a confusing statement that true must be 1, and false must be 0. It's not right.
It should be, 1 must be true, 0 must be false; true is not necessarily 1, and false is not necessarily 0;

Type④—Number type

1. Never test a specific floating point value

The highest accuracy of ECMAScript floating-point values ​​is 17 decimal places, but when performing arithmetic calculations, its accuracy is far inferior to integers.
For example: the result of 0.1+0.2 is not 0.3, but 0.30000000000000004. So it will never pass the test

if(a+b==0.3){
    
    
	alert('the result is 0.3')
}

Never test a particular floating-point value, because you don't know how small the rounding error caused the result to be biased.

2. Positive Infinity & Negative Infinity-Infinity

If the result of a calculation results in a value outside the range of values, this value will automatically be converted to a special Infinity value. If it is a negative number, it is converted to -Infinity (negative infinity); if it is a positive number, it is converted to Infinity (positive infinity).

  1. Infinity and -Inifinity are not values ​​that can participate in the calculation
  2. You can use the isFinite() function to determine whether the parameter is finite. If the parameter is between the minimum and maximum values, isFinite() will return true, otherwise it will return false

3.NaN(Not a Number)

NaN is not a number, it is a special number. It is used to indicate a situation where an operand that originally intended to return a value but did not return a value, so that it does not have to throw an error.
For example, in other languages, dividing any numeric value by a non-numeric value will cause an error. But in ECMAScript, it will return NaN instead of throwing an error.

alert(NaN/10; //NaN
alert(NaN==0; //false
alert(NaN==NaN;//false
alert(isNaN(NaN)); //true
alert(isNaN(10)); //false 10是数值
alert(isNaN('10')); //false 可以转成数值
alert(isNaN(true)); //false 可以转成数值
alert(isNaN('blue')); //true 不可以转成数值

To summarize NaN:

  1. Any operation involving NaN is to return NaN
  2. NaN is not equal to any number, including NaN itself
  3. The isNaN() function can determine whether the parameter is "not a value". The function can accept any type of parameters. First try whether it can be converted to a numeric value. If it can, return false, if not, return true. The surface is NaN.

Guess you like

Origin blog.csdn.net/qq_43263320/article/details/112686443