Update time 2021-10-31 11:49:18
1, Why do I need a data type?
In the computer, the storage space occupied by different data is different. In order to divide the data into data with different memory sizes and make full use of the storage space, different data types are defined.
Simply put, the data type is the category and model of the data. For example, the name "Zhang San" and age 18 are different types of data.
2, Data type of variable
Variables are where values are stored. They have names and data types. The data type of a variable determines how bits representing these values are stored in the computer's memory. JavaScript is a weakly typed or dynamic language. This means that there is no need to declare the type of variable in advance, and the type will be determined automatically during program operation.
var age = 10; // This is a numeric type var areYouOk = 'yes'; //This is a string
When the code runs, the data type of the variable is determined by the JS engine according to the data type of the variable value on the right. After running, the variable determines the data type.
JavaScript has dynamic types, which also means that the same variables can be used as different types:
var x = 6; // x is a number var x = "Bill"; // x is a string
3, Classification of data types
JS divides data types into two categories:
- Simple data type (Number,String,Boolean,Undefined,Null)
- Complex data type (object)
4, Simple data type (basic data type)
Simple data type | explain | Default value |
---|---|---|
Number | Numeric type, including positive property and floating-point value. For example: 21, 0.2 | 0 |
Boolean | Boolean value types, such as true and false, are equivalent to 0 and 1 | false |
String | String type, such as "Zhang San". Note: all strings in js are quoted | " " |
Undefined | var a; declares a variable but does not assign a value. In this case, a=undefined | undefined |
Null | var a = Null; declared that variable a is null | null |
1. Digital Number
The number type in JavaScript can be used to save both integer values and decimals (floating point numbers).
var age = 21;// integer var Age = 21.3747; // decimal
Digital hexadecimal
The most common base systems are binary, octal, decimal and hexadecimal.
// 1. Octal (0 ~ 7) add 0 before the number in the program to represent octal var num1 = 010; // 8 var num2 = 012; // 10 // 2. Hexadecimal (0 ~ 9 and a~f) digits preceded by 0x indicate hexadecimal #ffffff var num3 = 0x9; // 9 var num4 = 0xa; // 10
At this stage, we only need to remember to add 0 before octal and 0x before hexadecimal in JS
Digital range
Maximum and minimum values of values in JavaScript
alert(Number.MAX_VALUE); // 1.7976931348623157e+308 alert(Number.MIN_VALUE); // 5e-324
- Maximum value: Number.MAX_VALUE: 1.7976931348623157e+308
- Minimum value: Number.MIN_VALUE, which is 5e-322
Digital three special values
alert(Infinity); // Infinity alert(-Infinity); // -Infinity alert(NaN); // NaN
- Infinity, representing infinity, greater than any value
- -Infinity, representing infinitesimal, less than any value
- NaN, Not a number, represents a non numeric value
isNaN()
It is used to judge whether a variable is of non numeric type and returns true or false
// isNaN() this method is used to determine whether it is non numeric and returns false or true console.log(isNaN(12)); // false console.log(isNaN('Zhang San')); // true
2. String type string
The string type can be any text in quotation marks, and its syntax is double quotation marks' 'and single quotation marks''
var strMsg = "I Love Beijing Tiananmen ~"; // Use double quotation marks to represent strings var strMsg2 = 'I like pig feet~'; // Use single quotation marks to represent strings // Common errors var strMsg3 = I love big elbows; // If an error is reported and quotation marks are not used, it will be considered as js code, but js does not have these syntax
Because the attributes in HTML tags use double quotation marks, we prefer to use single quotation marks in JS
String quote nesting
JS can nest double quotation marks with single quotation marks, or nest single quotation marks with double quotation marks (outer double inner single, outer single inner double)
var strMsg = 'I am"Gao Shuaifu"Cheng xuape'; // You can include '' with '' var strMsg2 = "I am'Gao Shuaifu'Cheng xuape"; // You can also include '' with '' // Common errors var badQuotes = 'What on earth?"; // An error is reported. Single and double quotation marks are not allowed
String escape character
Similar to the special characters in HTML, there are also special characters in the string, which we call escape characters. Escape characters start with \. The common escape characters and their descriptions are as follows:
Escape character | interpretative statement |
---|---|
\n | newLine n means newLine |
\\ | Slash\ |
\' | 'single quote |
\" | Double quotes |
\t | tab indent |
\b | b means blank |
Case: pop up Web page warning box
Requirements: pop up the web page warning box and display the text!
Under the hot sun, my tall and straight posture has become the most unique scenery. When I look around, here is my stage, and I am the king of heaven and earth. At this moment, I am heroic and finally shout: "collect the rags
prompt('Under the hot sun, my tall and straight posture has become the most unique scenery. When I look around, here is my stage and I am the king of heaven and earth. At this moment, I am heroic and finally shout:"Collect junk');
String length
A string is composed of several characters. The number of these characters is the length of the string. The length of the whole string can be obtained through the length property of the string.
var strMsg = "I'm a handsome and golden program ape!"; alert(strMsg.length); // Display 11
String splicing
- Multiple strings can be spliced with + in the form of string + any type = new string after splicing
- Before splicing, any type added to the string will be converted into a string, and then spliced into a new string
//1.1 string "addition" alert('hello' + ' ' + 'world'); // hello world //1.2 numeric string "addition" alert('100' + '100'); // 100100 //1.3 numeric string + numeric alert('11' + 12); // 1112
+No. summary formula: add values and connect characters
String splicing enhancement
console.log('pink teacher' + 18); // As long as there are characters, they will be connected var age = 18; // console.log('pink teacher is old '); / / this is not OK console.log('pink teacher' + age); // Mr. pink 18 console.log('pink teacher' + age + 'Years old'); // Mr. pink is 18 years old
- We often splice strings and variables, because variables can easily modify their values
- Variables cannot be quoted because quoted variables will become strings
Case: display age
An input box pops up, requiring the user to enter the age, and then a warning box pops up to display "you are xx years old" (xx indicates the age just entered)
// An input box (prompt) pops up, allowing the user to enter the age (user input) // Save the value entered by the user with variables, and splice the age just entered with the string to be output (internal processing of the program) // Use alert statement to pop up alert box (output result) var age=prompt('Please enter your age'); var str='You have this year'+age+'Years old'; alert(str);
3. Boolean
Boolean types have two values: true and false, where true represents true (right) and false represents false (wrong). When Boolean and numeric types are added, the value of true is 1 and the value of false is 0.
console.log(true + 1); // 2 console.log(false + 1); // 1
4.undefined
Variables that are not assigned after declaration will have a default value of undefined (pay attention to the result when connecting or adding)
var variable; console.log(variable); // undefined console.log('Hello' + variable); // Hello, undefined console.log(11 + variable); // NaN console.log(true + variable);// NaN
5.Null
Declare the variable to null value, and the value stored in it is empty
var vari = null; console.log('Hello' + vari); // Hello, null console.log(11 + vari); // 11 console.log(true + vari); // 1
5, Gets the data type of the detection variable
typeof can be used to get the data type of the detection variable
var num = 18; console.log(typeof num) // Result number
Different types of return values
type | example | result |
---|---|---|
String | Type of 'hello' | "string" |
Number | typeof18 | "number" |
Boolean | typeof true | Boolean |
Undefined | typeof undefined | "undefined |
Null | typeof null | "object" |
Literal
Literal quantity is the representation of a fixed value in the source code. Generally speaking, literal quantity represents how to express this value.
- Number literal: 8, 9, 10
- String literal: 'dark horse programmer', 'big front end'
- Boolean literal: true, false
6, Data type conversion
The data obtained by using the form and prompt is of string type by default. At this time, you can't simply add directly, but you need to convert the data type of the variable. Generally speaking, it is to convert a variable of one data type into another data type.
We usually implement the conversion in three ways:
- Convert to string type
- Convert to numeric
- Convert to Boolean
Convert to string
mode | explain | case |
---|---|---|
toString() | Convert to string | var num = 1;alert(num.toString()); |
String() cast | Convert to string | var num = 1;alert(String(num)); |
Plus concatenated string | And string splicing results are strings | var num = 1;alert(num + "I am a string"); |
- toString() and String() are used differently.
- Among the three conversion methods, we prefer the third plus sign splicing string conversion method, which is also called implicit conversion.
Convert to number (key)
mode | explain | case |
---|---|---|
parseInt(string) function | Convert string type to integer numeric type | parseInt('55') |
parseFloat(string) function | Convert string type to floating-point numeric type | parseFloat('55.1') |
Number() cast function | Convert string type to numeric type | Number('12') |
js implicit conversion (- * /) | Implicit conversion to numerical type by arithmetic operation | '12'-0 |
- Note: the case of parseInt and parseFloat words is important
- Implicit conversion is that JS automatically converts data types when we perform arithmetic operations
Convert to Boolean
mode | explain | case |
---|---|---|
Boolean() function | Convert other types to Booleans | Boolean('true'); |
- Values representing null and negative will be converted to false, such as' ', 0, NaN, null and undefined
- The remaining values are converted to true
console.log(Boolean('')); // false console.log(Boolean(0)); // false console.log(Boolean(NaN)); // false console.log(Boolean(null)); // false console.log(Boolean(undefined)); // false console.log(Boolean('Xiaobai')); // true console.log(Boolean(12)); // true