1. Number
JavaScript does not distinguish between integers and floating-point numbers, and is uniformly represented by number, and the following are valid number types:
// Integer 123 // floating point 0.456 // scientific notation means 1.2345x1000, equivalent to 1234.5 // Negative number // Nan means not a number, which is represented by Nan when the result cannot be evaluated // Infinity is infinitely large, and is represented as infinity when the value exceeds the maximum value that JavaScript can represent.
Number can do arithmetic directly, and the rules and math are consistent:
// 3 // 7.5 // Infinity // NaN // 1 // 1.5
2. String
strings are arbitrary text enclosed in single quotes ' or double quotes, for example, and ‘abc‘
"xyz"
so on.
3. Boolean value
A Boolean value is exactly the same as a Boolean algebra, with a Boolean value of only two values, either, either directly, by means of a true
false
true
false
true
false
Boolean, or by a Boolean operation, which is similar to the C language.
true // This is a true value false // This is a false value // This is a true value // This is a false value
&&
Operations are associated with operations
||
An operation is or an operation
!
Operation is a non-operation
comparison operators when we compare number, we can get a Boolean value from the comparison operator: // false // true // true In fact, JavaScript allows comparison of arbitrary data types: false // true false // false
Pay special attention to the equality operator ==
. At design time, JavaScript has two comparison operators:
The first is the ==
comparison, which automatically converts the data type to compare, and many times it gets very weird results;
The second is the ===
comparison, which does not automatically convert the data type if the data type is inconsistent, returns false
if consistent, and then compares.
Because of this design flaw in JavaScript, do not use ==
comparisons, always insist on using ===
comparisons.
Another exception is that NaN
this particular number is not equal to all other values, including itself:
// false
The only way to judge
NaN
The method is through
isNaN()
Function:
// true
// Finally, pay attention to the equal comparison of floating-point numbers: // false /* This is not a design flaw in JavaScript. Floating-point numbers generate errors during operation because the computer cannot accurately represent infinite loop decimals. To compare two floating-point numbers for equality, you can only calculate the absolute value of their difference to see if they are less than a certain threshold:*/Math.Abs (// true
4, null and undefined
null
Represents an "empty" value, 0
unlike an empty string, which ‘‘
0
is a numeric value that ‘‘
represents a string of length 0 and null
represents "null".
In other languages, there are also JavaScript-like null
representations, such as Java, which is also used by null
Swift nil
, which is represented by Python None
. However, in JavaScript, there is one and null
similar undefined
, which means "undefined".
The designer of JavaScript wants to null
represent an empty value, whereas the undefined
value is undefined. It turns out that there is no such thing as an egg, and the difference between the two is insignificant. In most cases, we should all use null
. undefined
useful only when judging whether a function parameter is passed or not.
An array of JavaScript can include any data type. For example:
[1, 2, 3.14, ‘Hello‘, null, true];
Another way to create an array is through a Array()
function:
new Array(1, 2, 3); // 创建了数组[1, 2, 3]
However, for the sake of readability of the code, it is strongly recommended to use it directly []
.
The elements of an array can be accessed through an index. Notice that the starting value of the index is 0
:
5. Objects
A JavaScript object is a set of unordered collections of key-value, for example:
var person = { name: ‘Bob‘, age: 20, tags: [‘js‘, ‘web‘, ‘mobile‘], city: ‘Beijing‘, zipcode: null};
The key of a JavaScript object is a string type, and the value can be any data type. All of the above person
objects define a total of 6 key-value pairs, each of which is also called an object's properties, for example, the property is person
name
‘Bob‘
zipcode
null
.
To get the properties of an object, we use 对象变量.属性名
the following method:
person.name; // ‘Bob‘person.zipcode; // null
6. Variables
Variables are represented in JavaScript by a variable name, which is a combination of uppercase and lowercase English, numeric, and numeric, and $
_
cannot begin with a number. The variable name cannot be a JavaScript keyword, such as if
, and while
so on. Declare a variable with a var
statement, such as:
var // The variable A is declared, at which point a value is undefined var // The variable $b is declared, and the value of the $b is assigned to the $b at this time, which is 1. var // s_007 is a string var true // answer is a Boolean value of True var NULL // The value of T is null
In JavaScript, use the equals sign
=
Assigns a value to a variable. Arbitrary data types can be assigned to variables, the same variable can be repeatedly assigned, and can be different types of variables, but note that only the
var
Affirm once, for example:
var // the value of A is an integer 123 // a becomes a string
This type of variable itself is called Dynamic language, which corresponds to static language. Static languages must specify the variable type when defining the variable, and if the type does not match, an error is given. For example, Java is a static language, and the assignment statement is as follows:
int // A is an integer type variable, and the type is declared with int // Error: Cannot assign string to integer variable
This is why dynamic languages are more flexible than static languages.
7. Strict mode
JavaScript at the beginning of the design, in order to facilitate learning for beginners, does not require the use of var
declaration variables. This design error has serious consequences: if a variable is used without a var
declaration, the variable is automatically declared as a global variable:
i = 10; // i现在是全局变量
In different JavaScript files on the same page, if you do not have var
to declare it, the variables i
will be used, causing i
the variables to interact with each other, resulting in hard-to-debug error results.
A var
variable that uses a declaration is not a global variable, its scope is confined to the body of the declared function of the variable (the concept of the function will be explained later), and the variable of the same name does not conflict with each other in the function body.
In order to fix the serious design flaw of JavaScript, ECMA introduced the strict mode in the following specification, the JavaScript code running in strict mode, forcing the var
declaration variable to var
be used without a declaration variable, will result in a run error.
The way to enable strict mode is to write on the first line of the JavaScript code:
‘use strict‘;
This is a string, browsers that do not support strict mode will execute it as a string statement, and browsers that support strict mode will turn on strict mode to run JavaScript.
To test if your browser can support strict mode:
' Use strict '; // if the browser supports strict mode, // The following code will report a referenceerror error: = ' Hello, world '; alert (ABC);
Run the code, if the browser error, please repair and then run. If the browser does not error, it means that your browser is too old and need to upgrade as soon as possible.
Variables that are not var
declared are considered global variables, and in order to avoid this flaw, all JavaScript code should use the strict pattern. The JavaScript code we write later will all be in strict mode.
(3) data types and variables