JavaScript data type

Time:2021-9-21

1. Preface

JavaScriptDifferent from other programming languages (e.gJavaCWhen defining variables, there is no need to specify specific data types (such as numeric value, string, etc.)varVariables of all data types can be defined, because the specific data type of the variable will be automatically determined during the operation of the program.

thatJavaScriptWhat data types are defined internally?

It can be divided into basic types and reference types.

  • Basic type: String(String), value(Number), Boolean(Boolean), undefined(Undefined). air to air(Null)。
  • Value types: objects(Object), arrayArray)Sum function(Function)。

2. Basic type

2.1 string

Definition of string:

var sText = "Hello World!";

Gets a single character in a string by subscript, starting with 0.

//Gets the first character of stext
console.log(sText[0]); // 'H'
//Gets the last character of stext
console.log(sText[sText.length-1]); // '!'   ,  Stext.length is the length of the string stext

useslicesubstrandsubstringCan intercept string fragments.

//Starting with subscript 3 and ending with subscript 5, but excluding subscript 5.
sText.slice(3, 5); // 'lo'
//Start with subscript 3 and intercept 2 characters.
sText.substr(3, 2); // 'lo'
//Starting with subscript 3 and ending with subscript 5, but excluding subscript 5.
sText.substring(3, 5); // 'lo'

//Slice subscripts support negative values
sText.slice(3, -2); // 'lo Worl'
//Substring starts intercepting dynamically according to the smaller value of the two parameters and ends with the larger value (excluding the larger value)
//If the smaller value is less than 0, it is automatically set to 0.
sText.substring(3, 0) == sText.substring(0, 3)  // 'Hel'
sText.substring(3, -2) == sText.substring(0, 3) // 'Hel'

useconcatConnection string.

"Hello ".concat("JavaScript!") // 'Hello JavaScript!'

2.2 values

JavaScriptuse64 bitFloating point numbers store all numbers, including integers and floating point numbers.

Numerical variable definition:

//Integer
var iNum = 1;

//Floating point number
var fNum = 1.0;

//Scientific counting method
var eNum = 1.2e3; // 1.2 * 10^3 == 1200

Floating point overflow:

var a = 0.1,b = 0.2; 
console.log(a + b);   // 0.30000000000000004

//Solution: convert to integer and calculate again
var s = (a * 10 + b * 10) / 10;
console.log(s); // 0.3

Special values:

//Positive infinity
Number.POSITIVE_INFINITY == Infinity;  // true

//Negative infinity
Number.NEGATIVE_INFINITY == -Infinity; // true

//Maximum
Number.MAX_VALUE; // 1.7976931348623157e+308

//Minimum value
Number.MIN_VALUE; // 5e-324

//Non digital Nan
typeof(NaN) == 'number'; // true
isNaN(parseInt('abc')); //  True non numeric to numeric returns Nan
NaN == NaN; //  False to judge whether it is non digital. Use IsNaN instead of directly using = = to judge

Binary conversion:

//Number to string toString
var num = 23;
console.log(num.toString()); //  23. ToString is converted to decimal by default without parameters
console.log(num.toString(2)); //  10111, convert to binary
console.log(num.toString(16)); //  17, convert to hexadecimal

//String to number parseInt parsefloat
parseInt('10111', 2); //  23, specify conversion from binary to decimal
parseInt('17', 16); //  23, specify conversion from hexadecimal to decimal

Common functions:

//Keep decimal places
Math.PI.toFixed(2); // 3.14

//Take absolute value
Math.abs(-12);  // 12

//Rounding
Math.round(4.5); // 5

//Round up
Math.ceil(2.3); // 3

//Round down
Math.floor(2.3); // 2

//Random number
Math.random(); //  Random number between 0 and 1

2.3 Boolean

Boolean types have only two values:truefalse

The following values are converted to Boolean typesfalse
0, -0, null, "", false, undefined, NaN
Except for the above value, all other Boolean conversions aretrue

It is often used to judge non empty objects. For example:

If (obj) {} replaces if (obj! = null & & obj! = "") {}

2.4 undefined

undefinedIndicates that a variable is uninitialized.

var obj;

typeof(obj) == 'undefined'; // true

2.5 air to air

nullRepresents an object that has not been created;

typeof null; // object
null == undefined; // true
null === undefined; // false

3. Reference type

3.1 object

Object creation:

var person = {
    firstName:"Bill", 
    lastName:"Gates", 
    age:62, 
    eyeColor:"blue"
};

Acquisition of object properties:

person.lastName;
person["lastName"];

3.2 array

Array creation:

var cars = ["Saab", "Volvo", "BMW"];
var cars = new Array("Saab", "Volvo", "BMW");

Get the elements of the array by subscript:

cars[0] == "Saab"; // true

Modify elements in the array:

cars[0] = "Ymal";
cars[0] == "Saab"; // false

3.3 function

Function creation:

function myFun() {
    // do something
}

Function call:

myFun()