JavaScript program development (8) - data types of js syntax

  Due to the environment, I made up my mind today to greatly improve my technical level within half a year, not just within the scope of my own work, but the work I do is by no means for some people And doing it is to fight for oneself, that is, the so-called man lives a face, and a tree lives a skin! Don't talk nonsense, let's get to the point, continue with yesterday's topic, and continue with the data type of js.

Boolean type

  The Boolean type is the most used data type in js, which has only two values: true and false. These two numbers are not the same thing as numeric values, so true is not necessarily equal to 1, and false is not necessarily equal to 0. The following example is an example of assigning a value of type Boolean to a variable:

	var flag = true;
	var lost = false;

  One thing to note about the Boolean type is that the literal value of the Boolean type is case-sensitive, which means that True and False are not Boolean type data, but just ordinary identifiers, but they have not increased the readability of the code and avoided some Unnecessary problems arise, and it is not recommended to use these two identifiers as variables.

  Although the Boolean type has only two values, all types of data can be converted to the corresponding Boolean type values ​​through the Boolean() function. As shown in the following example code:

   var flag = "Hello World!";
   var msg = Boolean(flag);

 The conversion rules of various data types and Boolean types are described in the following table:

type of data convert to true convert to false
Boolean true false
 String any non-empty string  "" empty string 
 Number  Any non-zero value, including infinity  0 sum Nan
 Object  any non-null object  null
 Undefined  n/a  undefined

 According to the above rules, we can write the following code example:

function test(){
   var flag = "Hello World!";
   if(flag){
	   alert("write:true");
   }else{
	   alert("write:false");
   }
}

Number type

  ECMAScript uses the IEEE754 format to represent integer and floating point values. In order to support various numeric types, ECMAScript-262 defines various numeric literal formats, commonly used are decimal, octal, binary and hexadecimal. For the numerical conversion of these different bases, I will not introduce them one by one here. If necessary, I will describe them in detail in the follow-up blog.

  The above is the integer type, and there is a floating-point type. The floating-point type is a numerical value with a decimal point. There is a knowledge point in the floating-point number that is scientific notation.

  Number type data has a numerical range problem. ECMAScript does not store all the values ​​in the world, the smallest value is stored in the MIN_VALUE property of Number, which is 5e-324 in most browsers, and the largest value is stored in the MAX_VALUE property of Number, in most browsers The value of this is 1.7976931348623157+308. If the result of the calculation exceeds these two maximum values, a special Infinity will be obtained for a long time. If it is a negative number, it will be converted to -Infinity (infinity). Infinity is a value that cannot participate in calculations. If you are not sure whether the value involved in the calculation is Infinity, you can use isFinite() to detect. Returns true if it is a number between Infinity and -Infinity.

  The NEGATIVE_INFINITY and POSITIVE_INFINITY properties of the Number class hold -Infinity and Infinity respectively.

NaN type

  NaN (not a Number ) is a special numeric value. In other programming languages, any number divided by 0 will return an exception, causing the code execution to stop, while in js, it will return a NaN, which will not affect the execution of other code. NaN itself has two characteristics:

  1. Any operation involving NaN will return NaN, but this feature can cause problems in multi-step calculations;
  2. NaN is not equal to any value, including NaN itself.

isNaN(para) in ECMAScript can be used to determine if a parameter is "not a number". isNaN(para) After receiving a value, it will try to convert the value to a numeric value, some values ​​that are not numeric values ​​may be converted to numeric values, such as "10" or a Boolean value. As shown in the following example:

	document.write(isNaN("10")); //false , "10" is converted to 10
	document.write(isNaN(10)); //false, 10 is a number
	document.write(isNaN(false)); // false, false are converted to 0
	document.write(isNaN(NaN)); //true, NaN is not a value
	document.write(isNaN("str")); //true, "str" ​​is not a value
  isNaN(para) also works on objects. When calling isNaN() based on an object, the valueOf() of the object is called first, and then it is determined whether the return value can be converted into a numerical value. If not, the toString() method is called based on this value, and then the return value is tested.



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325815529&siteId=291194637