Data types and data conversions that must be understood in JS

Data types and data conversions that must be understood in JS

1. data type

Preface

There are only 6 types in JS, of which 5 basic data types are string, number, boolen, null, undefined, and there is one reference type, which is object. Object is a large complex, except for those 5 in JS Except for the basic data types, everything else is an object. The following are some notes compiled by self-study js recently, hoping to let more beginners understand the basic concepts of data types.

text

**1. Get the variable type**

typeof

var age = 18;

console.log(typeof age);//'number'

**2. Basic data types**

String, number, boolean

<script>

   //a. String

    var a = "laowangba"; 

   //document.write(typeof(a));

    

   //Number

    var b = 123;

    var b1 = 123.1;---> The same is number type, don't judge whether two floating-point numbers are equal.

    

   //Boolean

    var c = ture;

    var d = false;

</script>
  • Boolean literals: true and false, case sensitive
  • Computer internal storage: true is 1, false is 0

**3. Composite data type**

Object, array object

<script>

   //Object

    var e = {'name':'niu','age':18}; ---> equivalent to class a() in PHP

   

</script>

**4. Other data types**

Function, null, undefined

<script>

   //Function type

    var j = function(){};

   //document.write(j); ---> There may be disputes here, but the typeof prints out there is indeed the funcion type

    

   //null type

    var h = null;

   //document.write(h); ---> Empty object, a branch of the object type, null will be displayed when there is no such object.

   //alrt(document.getElementById('notExistElement')); --->displayed as null, this object cannot be found

    

   //undefined type

    var g;

   //document.write(i); ---> print a variable that does not exist, defined as undefined, undefined or defined, but not initialized

    

    

   //alrt(null == undefined);---> The display result is true

   //alrt(null === undefined);---> the display result is false

</script>

2. the conversion of data types

**2.1 Implicit conversion**

Implicit conversions are often used in if judgments and other places where direct results are required.

<script>

    undefined == null;//true   

    1 == true;//true  

    2 == true;//false  

    0 == false;//true

    0 =='';//true

    NaN == NaN;//false ---> NaN Note: This is a special expression in JS, NaN means that it is not equal to any value. This value means that the operand that originally wanted to return a value did not return a value (so that it would not Will throw an error). Any value divided by 0 will cause an error and terminate the program execution. Generally use isNaN() to determine whether it is of NaN type.

    [] == false;//true  

    [] == ![];//true

    '6'-'3'//3

    1234 +'abcd'//"1234abcd"

</script>
  1. undefined is equal to null, but not identical (===)
  2. When one is number and the other is string, it will try to convert string to number
  3. Implicit conversion converts boolean to number, 0 or 1
  4. Implicit conversion converts Object to number or string, depending on the type of another contrast
  5. For the judgment of 0 and empty strings, it is recommended to use "==="
  6. "==" will perform type conversion for different types of values ​​and then judge, "===" will not. It will first judge the value types on both sides, if the type does not match, it will be directly false

**2.2 Display Conversion**

Number:

var a = Number('1');

var b = Number(1);

var c = Number('c');

var d = Number(null);

var e = Number(undefined);



console.log(a,b,c,d,e);//1 1 NaN 0 NaN



//Number() can convert any value into a numeric value. If there is a character that is not numeric in the string to be converted, NaN is returned

String:

String(1234);//"1234"

String('abcd');//"abcd"

String(true);//"true"

String(undefined);//"undefined"

String(null);//"null"

Boolean:

var a = Boolean('0');

var b = Boolean(0);

var c = Boolean('1');

var d = Boolean(null);

var e = Boolean(undefined);

var f = Boolean(NaN);



console.log(a,b,c,d,e,f);//true false true false false false



//0, ``(empty string), null, undefined, NaN will be converted to false and others will be converted to true

parseFloat:

var a = parseFloat('1.2df');//1.2

var b = parseFloat('1.3.4');//1.3

var c = parseFloat('c12');//NaN

var d = parseFloat(null);//NaN

var e = parseFloat(undefined);//NaN



console.log(a,b,c,d,e); 



//parseFloat() converts a string to a floating point number

//parseFloat() and parseInt are very similar,

//The difference is that parseFloat will parse the first one. When the second one is encountered. Or non-digital end

//If there are only integers in the parsed content, parse into integers

parseInt()

var a = parseInt('1.2df');//

var b = parseInt(1);

var c = parseInt('c12');

var d = parseInt(null);

var e = parseInt(undefined);



console.log(a,b,c,d,e);//1 1 NaN NaN NaN



//If the first character is a number, it will be parsed until the end of a non-number is encountered

//If the first character is not a number or symbol, NaN is returned

2.3 Special instructions for strings

**1. Both single and double quotes can parse escape characters**

**2. Neither single quotes nor double quotes can parse variables**

**3. Variables and strings, variables and variables should be spliced ​​with + to distinguish PHP**

**4. Single quotation marks and double quotation marks can be nested with each other. If single quotation marks are nested with single quotation marks in Chinese medicine, single quotation marks need to be escaped, the same is true, double quotation marks are the same**

<script>

   //Why can't single quotes and double quotes be parsed

    var a = 10;

    alrt('a'); ---> I am stupefied here, do you want to output a variable or a string? Unlike PHP, there is a $ that can directly distinguish a variable from a string

    alrt('a'+ a);

    

   //+Connection description

    alrt('hello' + 3 +'world');//hello3world

    alrt(3 + 2 +'hello' + 5 +'world');//5hello5world

    alrt(4 + 4 +'hello' + 4 + 5 +'world');//8hello45world ---> Connect from left to right, two numbers will be understood as plus signs, until they are not numeric, they will be interpreted as splicing.

</script>

How to print a string:

var s ='I really like the spirit of/"Lei Feng\'\'/"';

console.log(s);
  • String length

The length attribute is used to get the length of the string

var str ='Hello World';

console.log(str.length);
Reference: https://cloud.tencent.com/developer/article/1543564 JS must understand the data types and data conversion-Cloud + Community-Tencent Cloud