JS data type - basic concepts, data type coercion

Article directory

1, Basic concept of data type

  • Data type: refers to the literal type. There are six data types in JS
    • String string
    • Number value
    • Boolean
    • Null null value
    • Undefined
    • Object object
  • Where String Number Boolean Null Undefined is the basic data type and Object is the reference data type

1 String string

1.1 String Basics

  • Strings need to be quoted in JS;
  • Use double quotation marks or single quotation marks, but do not mix them;
  • Quotation marks cannot be nested, double quotation marks cannot be placed in double quotation marks, and single quotation marks cannot be placed in single quotation marks;
var str = 'hello';  // Declare a variable str and assign it hello
console.log(str)  // Console output str content

// Change the value of variable str to 'I said: "today's weather is really good! "
str = 'I said:"What a nice day today!"'; // Will change the value of the original str

1.2 String escape

In the string, we can use \ as escape character. When representing some special symbols, we can use \ to escape

  • \"= > means"
  • \'= > for'
  • \N = > for newline
  • \T = > tab
  • \= > indicates\
str = "I said:\"Today\t What a nice day!\"";			
str = "\\\\\\";
//Output literal string str

1.3 String splicing

There are two forms of string splicing: use + splice and use template string

var a = "Sun WuKong";
var b = "I am"+a;
var c = `I am${a}`;

2 Number

  • All values in JS are of type Number, including integers and floating-point numbers (decimals)
  • The maximum number that can be represented in JS
    • Number.MAX_VALUE
    • 1.7976931348623157e+308
  • Number.min'value is greater than the minimum value of 0
    • 5e-324
  • If the Number represented by Number exceeds the maximum value, a
    • Infinity means positive infinity
    • -Infinity means negative infinity
  • Using typeof to check Infinity also returns number
  • NaN is a special number, which means Not A Number. Using typeof to check a NaN will also return number
var a = 123;  //Number 123
var b = "123";  //String 123
    You can use an operator typeof to check the type of a variable
    Syntax: typeof variable	
    When checking a string, string is returned
    When you check the value, number is returned
// console.log(typeof b);
a = -Number.MAX_VALUE * Number.MAX_VALUE;
a = "abc" * "bcd";
a = NaN;
// console.log(typeof a);
a = Number.MIN_VALUE;

// In JS, the operation of integers can ensure the accuracy
var c = 1865789 + 7654321;
 * If you use JS for floating-point operations, you may get an imprecise result
 * 	Therefore, do not use JS to perform operations with high accuracy requirements	
var c = 0.1 + 0.2;

3 Boolean

  • Boolean: there are only two Booleans, mainly used for logical judgment
    • True - true
    • False - indicates false
  • When a boolean value is checked with typeof, a boolean is returned
var bool = false;	
console.log(typeof bool);

4 Null null and Undefined

  • null is a value used to represent an empty object
    • When a null value is checked with typeof, object is returned
  • Undefined has only one value, undefined
    • When a variable is declared but no value is assigned to it, its value is undefined. When typeof is used to check an undefined, undefined will also be returned
var a = null;
var b = undefined;
console.log(typeof b);

2, Data type conversion


  • Cast one data type to another
  • Type conversion mainly refers to converting other data types to String, Number and Boolean

1. Convert other data types to String

  • Convert other data types to String
  • Method 1:
    • Call toString() method of converted data type
    • This method does not affect the original variable, it will return the result of the transformation
    • But note: there is no toString() method for null and undefined values. If their methods are called, an error will be reported
  • Mode two:
    • Call the String() function and pass the converted data as parameters to the function
    • When using the String() function for cast, for Number and Boolean, it is actually the toString() method called
  • But for null and undefined, toString() method will not be called. It will directly convert null to "null" and undefined to "undefined".
//Call toString() method to convert a to string
var a = 123; // Value to String
a = a.toString();
a = true; // Boolean to String
a = a.toString();
a = null; // null to String
a = a.toString(); //Report errors
a = undefined;  // undefined to String
a = a.toString(); //Report errors

//Call the String() function to convert a to a string
a = 123;
a = String(a);
a = null;
a = String(a);
a = undefined;
a = String(a);
console.log(typeof a);

2 conversion of other data types to Number

2.1 data conversion

  • Convert other data types to Number

  • Conversion mode 1:

  • Using the Number() function

  • String -- > number

    • 1. If it is a pure number string, convert it directly to a number
    • 2. Convert to NaN if there is non numeric content in the string
    • 3. If the string is an empty string or a string with all spaces, it will be converted to 0
  • Boolean -- > number

    • true to 1
    • false to 0
  • Null -- > number 0

  • Undefined - > Digital NaN

  • Conversion mode 2:

    • This is a way to deal with strings
    • parseInt() converts a string to an integer
    • parseFloat() converts a string to a floating-point number
//Call the Number() function to convert a to the Number type
var a = "123";
a = Number(a);   // 123
a = false;
a = Number(a);  // 0
a = null;
a = Number(a);  // 0
a = undefined;
a = Number(a);  // Nan
//Call the parseInt() function to convert a to Number
a = "123567a567px";
// parseInt() can extract the valid integer content in a string and convert it to Number
a = parseInt(a);
// parseFloat() is similar to parseInt(), except that it can get a valid decimal
a = "123.456.789px";
a = parseFloat(a);
// If parseInt() or parseFloat() is used for non String, it will first convert it to String and then operate
a = true;
a = parseInt(a);
a = 198.23;
a = parseInt(a);
console.log(typeof a);

2.2 numerical system

  • In js, if you need to represent a hexadecimal number, you need to start with 0x
  • If you need to represent an octal number, you need to start with 0
  • If you want to represent a binary number, you need to start with 0b
  • But not all browsers support it
a = 0x10;
a = 0xff;
a = 0xCafe;
//Octal digit
a = 070; //For strings like "070", some browsers will parse them as octal, some as decimal
//Binary number
a = 0b10;

a = "070";
//You can pass a second parameter in parseInt() to specify the base of the number
a = parseInt(a,10);
console.log(typeof a);

3. Convert other data types to Boolean

  • Convert other data types to Boolean
  • Using the Boolean() function
  • Number - > Boolean
    • All but 0 and NaN are true
  • String - > Boolean
    • Except for empty strings, the rest are true
  • null and undefined are both converted to false
  • Object will also be converted to true
// Value to Boolean
var a = 123; //true
a = -123; //true
a = Infinity; //true
a = NaN; //false
a = 0; //false
// Call the Boolean() function to convert a to a Boolean value
a = " "; // ture
a = null; //false
a = undefined; //false
a = Boolean(a);
console.log(typeof a);
// Value to Boolean
var a = 123; //true
a = -123; //true
a = Infinity; //true
a = NaN; //false
a = 0; //false
// Call the Boolean() function to convert a to a Boolean value
a = " "; // ture
a = null; //false
a = undefined; //false
a = Boolean(a);
console.log(typeof a);
16 original articles published, 10 praised and 701 visited
Private letter follow

Tags: REST

Posted on Tue, 14 Jan 2020 20:17:39 -0800 by gizzmo