$ Arrays and objects
Array
An array of JavaScript can include any data type. For example:
[1, 2, 3.14, ‘Hello‘, null, true];
Arrays []
are represented by ,
separate elements.
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
:
var arr = [1, 2, 3.14, ‘Hello‘, null, true];arr[0]; // 返回索引为0的元素,即1arr[5]; // 返回索引为5的元素,即truearr[6]; // 索引超出了范围,返回undefined
Object
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‘, hasCar: true, 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:
// ‘Bob‘person.zipcode; // null
Variable
The concept of variables is basically the same as the equation variables in junior algebra, but in computer programs, variables can be not only numbers, but also arbitrary data types.
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
In JavaScript, variables are assigned using an equal sign =
. Any data type can be assigned to a variable, the same variable can be repeatedly assigned, and can be different types of variables, but note that only var
one time, for example:
var a = 123; // a的值是整数123a = ‘ABC‘; // a变为字符串
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.
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.
The way to enable strict mode is to write on the first line of the JavaScript code:
‘use strict‘;
Liaoche JS Tutorial Learn to memorize---arrays and objects