I. Variables
JavaScript variables are loosely typed, and the so-called looseness is used to store any type of data. Variables are containers for storing information. When defining a variable, use the var operator (var is a keyword) followed by the variable name (variable name is an identifier). A variable is an amount that can be changed again after initialization.
Let's take a look at this example:
Just like algebra: x=2, y=3, z=x+y In algebra, we use letters (such as x) to store values (such as 2). Through the above expression z=x+y, we can calculate that the value of z is 5. In JavaScript, these letters are called variables. So we can think of variables as containers for storing data.
(1)JavaScript variable name
Like algebra, JavaScript variables can be used to store values (such as x=2) and expressions (such as z=x+y). Variables can use short names (such as x and y) or more descriptive names (such as age, sum, totalvolume).
It should be pointed out that:
The 1 variable must start with a letter.
Variables can also start with $ and _ symbols (although this is not recommended).
3 Variable names are case-sensitive (y and y are different variables)
(2)JavaScript data types
JavaScript variables can also hold other data types, such as text values (name="Bill Gates "). In JavaScript, it is similar to "Bill Gates"
A piece of text is called a string. There are many types of JavaScript variables, but at present, we only focus on numbers and strings. Assign a text value to a variable
The value should be enclosed in double or single quotation marks. Do not use quotation marks when the value assigned to a variable is a number. If you enclose a value in quotation marks, it will be
Treat as text. There are detailed data types behind.
Example:
& ltspan style = " font-size: 18px;" & gtvar pi = 3. 14;
Var name= "Bill Gates";
Var answer =' Yes, I am!' ;
document . write(pi+" & lt; br >; );
document . write(name+" & lt; br >; );
Document.write (answer+"<; br >; ); & lt/span>。 (3) Declare (create) JavaScript variables
Creating variables in JavaScript is often called "declaring" variables. A good programming habit is to detect the required variables evenly at the beginning of the code.
Ming. You can also declare variables without var, but this is not recommended.
We use the var keyword to declare the variable: var carname.
When a variable is declared, it is empty (has no value). To assign a value to a variable, use the equal sign: carname = "Volvo.
However, you can also assign a value to a variable when you declare it: var carname = "Volvo.
Example: We create a variable named carname, assign it "Volvo", and then put it in an HTML paragraph with id="demo ".
& ltspan style = " font-size: 18px;" & gt& lt! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN " " http://www . w3 . org/TR/XHTML 1/DTD/XHTML 1-Transitional . DTD " & gt;
& lthtml xmlns = " http://www . w3 . org/ 1999/XHTML " & gt;
& lthead & gt
& ltmeta http-equiv = " Content-Type " Content = " text/html; charset=gb23 12" />
& lttitle & gtJS variables and data types
& lt/head & gt;
& ltbody & gt
& ltp> Click here to create variables and display the results. & lt/p & gt;
& ltbutton onclick = " my function()" & gt; click here
& ltp id="demo " >& lt/p & gt;
& ltscript type = " text/JavaScript " & gt;
Function myFunction ()
{
Var carname= "Volvo";
document.getElementById("demo ")。 innerHTML = carname
}
& lt/script & gt;
& lt/body & gt;
& lt/html & gt; & lt/span>。 Click effect:
(4) One statement, multiple variables
You can declare many variables in one statement. This statement starts with var and separates variables with commas:
Declarations can also span multiple lines:
In computer programs, valueless variables are often declared. Variables that are not declared with values are actually undefined. After executing the following statement
After that, the value of the variable carname will be undefined: var carname.
(5) Redeclare JavaScript variables
If the JavaScript variable is redeclared, its value will not be lost: after executing the following two statements, the value of the variable carname is still "Volvo":
& ltspan style = " font-size: 18px;" & gtvar carname= "Volvo";
var carname& lt/span>。 (6)JavaScript algorithm
You can perform arithmetic operations through JavaScript variables and use operators such as and+:
Example:
& ltspan style = " font-size: 18px;" & gt& lt! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN " " http://www . w3 . org/TR/XHTML 1/DTD/XHTML 1-Transitional . DTD " & gt;
& lthtml xmlns = " http://www . w3 . org/ 1999/XHTML " & gt;
& lthead & gt
& ltmeta http-equiv = " Content-Type " Content = " text/html; charset=gb23 12" />
& lttitle & gtJS variables and data types
& lt/head & gt;
& ltbody & gt
& ltp> Assume that y=5, calculate x=y+2 and display the result. & lt/p & gt;
& ltbutton onclick = " my function()" & gt; click here
& ltp id="demo " >& lt/p & gt;
& ltscript type = " text/JavaScript " & gt;
Function myFunction ()
{
var y = 5;
var x = y+2;
var demoP = document . getelementbyid(" demo ")
demop . innerhtml = " x = "+x;
}
& lt/script & gt;
& lt/body & gt;
& lt/html & gt; & lt/span>。 Click effect:
Second, the data type.
The data types of JavaScript include string, number, Boolean, array, object, null and undefined. Before discussing data types, let's discuss an operator type of.
Type of operator
The typeof operator is used to detect the data type of a variable. Using the typeof operator on a value or variable returns the following string:
Both of the above methods are feasible.
The typeof operator can manipulate variables or literal quantities. Although you can use typeof(box) in this way, typeof is an operator, not a built-in function. Function is an object, not a data type, so it is very necessary to distinguish function and object with typeof.
The return value is an example of a function:
(1)JavaScript has a dynamic type.
JavaScript has dynamic types. This means that the same variable can be used as different types:
Example:
(2)JavaScript string type
A string is a variable that stores characters. A string can be any text in quotation marks. You can use single or double quotes:;
Example: You can use quotation marks in a string as long as they don't match the quotation marks on both sides of the string.
& ltspan style = " font-size: 18px;" & gtvar carname 1= "Bill Gates";
Var carname2=' Bill Gates';
Var answer 1= "Nice to meet you!" ;
Var answer2= "His name is Bill";
Varanswer3 =' His name is Bill';
document . write(carname 1+" & lt; br >; )
document . write(carname 2+" & lt; br >; )
document . write(answer 1+" & lt; br >; )
document . write(answer 2+" & lt; br >; )
document . write(answer 3+" & lt; br >; )& lt/span>。 (3)JavaScript numbers
JavaScript has only one number type. Numbers can be decimal or not. Number types include two kinds of numerical values: integer and floating point. The output format is all decimal number output. The most basic number of words is decimal. Literal quantities of octal values are also included. The leading character must be 0, octal sequence (0 to 7, base 8); The first two digits of hexadecimal literal must be 0x, followed by (0 to 9 and A to F); Floating-point index value must contain decimal point, and there must be at least one digit after the decimal point.
1 For those numerical values that are too large or too small, we can adopt a scientific counting method (e-notation), and use e in front of the numerical value to represent the exponential power of 10. For example:
& ltspan style = " font-size: 18px;" & gt& ltspan style = " font-size: 18px;" & gtvar box = 4. 12e-9; & lt/span>。 & lt/span>。 2 To determine whether a numerical value is out of the specified range, you can use the isFinite () function. Returns true if it does not exceed, and returns false if it exceeds.
The 3isNaN () function is used to determine whether the value is NaN. After receiving a value, the isNaN () function tries to convert the value into a numerical value.
The isNaN () function also applies to objects. In the process of calling the isNaN () function, first call the value () method, and then judge whether the return value can be converted into a numerical value. If not, call the toString () method based on this return value, and then test the return value.
Example:
& ltspan style = " font-size: 18px;" & gtvar x 1 = 36.00;
var x2 = 36
var y = 123 E5;
var z = 123 e-5;
document . write(x 1+" & lt; br />)
document . write(x2+" & lt; br />)
document . write(y+" & lt; br />)
document . write(z+" & lt; br />)& lt/span>。
(4)JavaScript Boolean value
Boolean (logic) can only have two values: true or false. For example:
var x = true
Var y = false(4)JavaScript array
Array subscripts start from zero, so the first item is [0], the second item is [1], and so on. The following code creates an array named cars:
& ltspan style = " font-size: 18px;" & gtvar cars = new Array();
Car [0]= "Audi";
Automobile [1]= "BMW";
Automobile [2]= "Volvo"; & lt/span>。
Or:
& ltspan style = " font-size: 18px;" & gtvar cars = new array ("Audi", "BMW" and "Volvo"); & lt/span>。 example
& ltspan style = " font-size: 18px;" & gtvar I;
var cars = new Array();
Car [0] = "Audi";
Automobile [1] = "BMW";
Automobile [2] = "Volvo";
for(I = 0; I< car. Length; i++)
{
document . write(cars[I]+" & lt; br >; );
} & lt/span>。 The output result is easy to know.
(5)JavaScript objects
Objects are separated by curly braces. Within parentheses, the attributes of an object are defined in the form of name and value pairs (name: value). Attributes are separated by commas:
Var person = {first name: "Bill", last name: "Gates", id:5566 };;
The object (person) in the above example has three attributes: firstname, lastname and id. Spaces and line breaks are irrelevant. Declarations can span multiple lines:
There are two ways to handle object properties:
example
(6) Undefined and invalid
The value Undefined indicates that the variable does not contain any value. You can empty a variable by setting its value to null.
Undefined type
Empty type
(7) Declare variable types
JavaScript variables are all objects. When you declare a variable, you create a new object. When declaring a new variable, you can use the keyword "new" to declare its type: