1. Data type
Different data in the computer requires different storage space, so in order to make full use of the storage space, we set different data types for the data, for example: "Xiao Ming", 18.
JavaScript is a weakly typed or dynamic language. This means that there is no need to declare the type of the variable in advance, and the type will be determined automatically during the running of the program.
For example: myname="Xiao Ming" //string
age = 18 //number type
Js judges the value on the right to judge and determine the value type of the variable. There are two types of data types in Js:
Simple data types :
Number,String,Boolean,Undefined,Null
Two, Number digital type
1. Digital base
There are decimal, octal, and hexadecimal representations of integers. As far as the commonly used values are concerned, hexadecimal refers to the value of 0-15 (0 to 9, a to f). In JavaScript, add "0x" in front of it to mark it as hexadecimal, octal It is to add "0" in front of 0-7.
2. Digital range
- Maximum value: Number.MAX_VALUE, this value is: 1.7976931348623157e+308.
- Minimum value: Number.MIN_VALUE, this value is: 5e-324.
3. Special values for numbers
Infinity, representing infinity, greater than any value
-Infinity , representing infinitesimal, less than any value
NaN, Not a number, represents a non-numeric value
4.isNaN
Used to determine whether a variable is a non-numeric type, return true or false
var age = 21;
var isNum = isNaN(age);
console.log(isNum); // false
var name="Xiao Ming";
console.log(isNaN(name)); // true, "Xiao Ming" is a non-number
Three, String string type
1. String definition
The string type can be any text in quotation marks, and its syntax is double quotation marks "" and single quotation marks ''.
Example: var name = "Xiaoming"; //If there are no quotation marks, an error will be reported
2. Quotation mark nesting
JS can nest double quotes with single quotes, or nest single quotes with double quotes (outer double inner single, outer single inner double)
var myName = "My name is 'Xiao Ming'";
3. String escape character
There are special characters in the string, and we use escape characters starting with \ to escape.
\n : newline character;
\\ : slash \;
\': ' apostrophe;
\":Double quotes";
\t: tab indentation
\b: space
4. String length
A string is composed of several characters, and the number of these characters is the length of the string.
For example: var str = "I am tall, handsome and rich!"; alert(str.length); //output is 6
5. String concatenation
Multiple strings can be spliced with + to get a new string.
Such as: var str = "The road is long and long, ",strAll="";
strAll = str + "I will search up and down";
alert(strAll);//The road is long and long, I will search up and down
alert(100+100);//200
alert(100+”100”); //100100
alert(“100”+”100”);//100100
Summarize:
- Strings are added to numbers and characters are concatenated.
- Variables can also be added to characters, variables cannot be quoted, otherwise they will be treated as strings.
Four, Boolean Boolean
The Boolean type has two values: true and false, where true means true (true) and false means false (wrong).
When Boolean and numeric 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
1. Undefined and Null
Undefined: A variable that is not assigned a value after declaration will have a default value of undefined;
var myName;
console.log(myName); // undefined
console.log('hello' + myName); // hello undefined
console.log(11 + myName); // NaN
console.log(true + myName); // NaN
Null: A declared variable gives a null value, and the value stored in it is empty;
var myName = null;
console.log('hello' + myName); // hello null
console.log(11 + myName); // 11
console.log(true + myName); // 1
2. Get variable type
typeof can be used to get the data type of the detected variable.
var num = 18;
var myname = null;
console.log(typeof num) // result number
console.log(typeof myname) // object
3. Type conversion
For example, the information submitted in the form (form) and prompt is all string information, so it cannot be added or subtracted. In this case, data type conversion is required.
4. Convert to String
toString:var num = 13; console.log(typeof num.toString());
String: var num =13;console.log(typeof String(num));
The third way string splicing: var num = 13; console.log(typeof 13 + 'I am a string');
5. Number type conversion
parseInt(string, radix): Convert the string type to an integer number type.
Example:
parseInt("12"); //returns 12
parseInt("12blue"); //returns 12
parseInt("12.8"); //returns 12
parseInt("blue"); //returns NaN
Radix is optional. Indicates the base of the number to parse. The value is between 2 ~ 36.
parseFloat(string) converts the string type into a floating-point number type.
Number(string) converts string to a number type.
6. Convert to Boolean type
Values representing empty and negated will be converted to false, such as '', 0, NaN, null, undefined, and other values will be 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
eg:
1. Enter the year of birth and calculate the age:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<script>
var year = prompt("输入出生年份:");
console.log("年龄:",2022-year,"岁");
</script>
</body>
</html>
achieve effect
2. Input your full age and output it on the console in the format: Your full age is: ? Years old, what is the false age? age.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<script>
var oldyear = prompt("请输入你的周岁年龄:");
var a = parseInt(oldyear)+1;
console.log("您的周岁是"+oldyear+"岁,"+"您的虚岁是"+a+"岁。");
</script>
</body>
</html>
achieve effect
Summarize
- Compiler: It is compiled before the code is executed to generate an intermediate code file, for example: first prepare all the dishes before they can be served on the table.
- Interpreter: The interpreter is interpreted in time at runtime and executed immediately (when the compiler runs in an interpreted mode, it is also called an interpreter) is like eating hot pot, eating and rinsing, and doing it at the same time.
- Identifier: refers to the name that developers give to variables, properties, and functions. Identifiers cannot be keywords or reserved words.
- Keywords: refers to the words that JS itself has used, and they can no longer be used as variable names and method names.
包括:break、case、catch、continue、default、delete、do、else、finally、for、function、if、in、instanceof、new、return、switch、this、throw、try、typeof、var、void、while、with 等。
- Reserved words: actually reserved "keywords", which means that although they are not keywords yet, they may become keywords in the future, and they cannot be used as variable names or method names.
包括:boolean、byte、char、class、const、debugger、double、enum、export、extends、fimal、float、goto、implements、import、int、interface、long、mative、package、private、protected、public、short、static、super、synchronized、throws、transient、volatile 等。