Judgment and conversion of JavaScript data types! ! !

There are two types of JavaScript data:

1. Basic data type (original value)

  • String String 

    Strings can be declared with single quotes '' or double quotes "". Since JavaScript has no character type, the following two lines of code are correct.

(Note: Single quotes in Java indicate characters, and double quotes indicate strings)

var str1 = 'apple'  //单引号
var str2 = "apple"  //双引号
  • Number 

   ① Integer

   It can also be written in the form of hexadecimal, octal, decimal, hexadecimal, and the final return value during calculation is the result of decimal.

   ② Floating point

   It can be simply understood as a decimal in mathematics, for example: 3.0, note that the decimal part cannot be omitted at this time~

   ③ Scientific notation

   Similar to very large or very small numbers, scientific notation is required. JavaScript converts 6 or more leading zero floating-point numbers into scientific notation by default. 52,000,000,000 can be written as 5.2e9

   ④ Special numbers

   NaN means not a number (Not a Number) 

alert(isNaN("apple")); //结果为 true
alert(isNaN("123"));   //结果为 false
  • Boolean 

    There are only two values, true and false. 

  • Null 

   There is only one value null, which means that an object that does not yet exist is actually empty!

  • Undefined 

    There is only one value undefined. When the declared object is not initialized, the default value of the change is undefined.

  • Unique value Symbol (new in ES6)

    Not very commonly used, you can find out on Baidu for yourself.

2. Reference data type (reference value)

  • Object Object

  • Array

  • Function

   <I won't write in detail here, I will update it separately later~>

Type judgment

  • typeof 

    Note: The type judgment of null returns Object. This is an initial error of JavaScript, but it has been used in the later period, and it will be wrong if it is wrong.

  • instanceof 

    Only valid for reference types! ! ! Instanceof does not support the judgment of basic types.

  The following figure is the demo code:

		<script type="text/javascript">
			var a = 10;
			var b = 'beautiful';
			var c = null;
			var d = new Array();
			document.write(typeof(a));  // number
			document.write('<br/>');
			document.write(typeof(b));  // string
			document.write('<br/>');
			document.write(typeof(c));  // Object
			document.write('<hr/>');
			if (d instanceof Array){   //判断 左边的 d 是否是右边的数组
				document.write("d 是一个数组");  
			}else{
				document.write("d 不是一个数组");
			}
		</script>

   The following figure is the effect of the demonstration:

   

Type conversion

  • Number (variable): Convert variables to numeric types

  • parseInt (variable): Convert variables to character types

  • parseFloat (variable): convert the variable to a floating point type

  • Blooean (variable): Convert variables to boolean type

  • String (variable): Convert the variable to a string type

    Here is the demo code:

 		<script type="text/javascript">
			var a = '52';
			var b = a+1;
			var c = Number(a)+1 ;
			document.write("没有用 Number 转换前:"+b);
			document.write('<br/>');
			document.write("用 Number 转换后:"+c);
			document.write('<hr/>');
			var num1 = 13;
			var num2 = num1+14;
			var num3 = String(num1)+14;     // 方法一
			var num4 = num1.toString()+14   // 方法二
			document.write("没有用 String 转换前:"+num2);
			document.write('<br/>');
			document.write("(方法一)用 String 转换后:"+num3);
		    document.write('<br/>');
			document.write("(方法二)用 String 转换后:"+num4);
			var x = 10;
			var y = 0;
			var x1 = Boolean(x);
			var y1 = Boolean(y);
			document.write('<br/>');
			document.write('<hr/>');
			document.write("没有用 Boolean 转换前:x="+x+" &nbsp&nbsp y="+y);
			document.write('<br/>');
			document.write("用 Boolean 转换后:x="+x1+" &nbsp&nbsp y="+y1);
		</script>

  The following figure is the effect of the demonstration:

 

Do not redistribute without permission! If there is an error, see you in the comment section~ Mengxin, thank you in advance (*≧︶≦))( ̄▽ ̄* )ゞ

Guess you like

Origin blog.csdn.net/qq_44761243/article/details/108986434