Sort out the basic Javascript study notes. A series of previous articles are about understanding Javascript. This article is to learn more about javascript and hope you will continue to pay attention to it.
What is a variable?
A variable is a container used to store information.
Variable Declaration
Syntax:
Var variable name
Variable name = value;
The variable must be declared before being assigned a value.
Variable values can be assigned repeatedly.
Variable naming rules
- The variable must start with a letter;
- Variables can also start with $ and _ (but we do not recommend this );
- Variable names are case sensitive (a and A are different variables ).
1. Statements
The statement ends with a semicolon. If the semicolon is omitted, the end of the statement is determined by the parser.
There is a good coding habit, all should end;
2. Data Type
In JavaScript, a piece of information is a value ). Values have different types. The most familiar type is numbers. A string value is one or more words enclosed in quotation marks.
NumberAny numeric value. A number can contain a decimal point or 68.57
StringCharacters in quotation marks. You can use single or double quotation marks "hello, world"
Boolean)True or false true
Undefined and NullUndefined indicates that the variable does not contain any value. You can clear a variable by setting its value to null.
ObjectAny value associated with the object
FunctionValue returned by the function
Var a; // a is undefinedvar a = 6; // a is the number var a = "Jason"; // a is the string
3,What is a function?
A function is a set of JavaScript statements that execute a task.
Basic Syntax:
Function Name () {function Code ;}
Function Name ();
Note:
Function defines the function keywords.
"Function name": the name you use for the function.
Replace "function code" with code that completes a specific function.
"Second function name"
Function add2 () {var sun = 3 + 2; alert (sun) ;}add2 (); // call a function to directly write a function name. The function code pops up.
4. Output content (document. write)
Document. write () directly outputs content on the webpage.
First, the output content is enclosed by "" and the content in "" is directly output.
document.write("I love JavaScript!");
Type 2: Output content through Variables
Var mystr = "hello world"; document. write (mysrt); // directly write the variable name and output the content stored in the variable.
Third: Output multiple items and connect the content with the "+" sign.
Var mystr = "hello"; document. write (mystr + "I love Java Script"); // connect multiple contents with a plus sign
Type 4: Output HTML tags and take effect. The tags are enclosed.
Var mystr = "hello"; document. write (mystr +"
"); // Output" hello ", output a linefeed document. write (" JavaScript ");
5. Warning (alert message dialog box)
When we visit a website, a small window pops up with a prompt message. If you do not click "OK", you cannot perform any operations on the webpage. This small window is implemented using alert.
Syntax:Alert (string or variable );
var mynum = 30;alert("hello!");alert(mynum);
Result: the message box is displayed in order (the alert message dialog box contains a confirmation button)
Note:
1. You cannot perform any other operations before clicking "OK" in the dialog box.
2. message dialog boxes can be used to debug programs.
3. alert output content, which can be a string or variable, similar to document. write
6. confirm the selection (confirm message dialog box)
In addition to providing information to users, we also want to obtain information from users. The confirm message dialog box is used.
The confirm message dialog box is usually used to allow users to select actions, such as: "Are you right ?" . A dialog box (including a confirmation button and a Cancel button) is displayed ).
Syntax: confirm (str );
Parameter description: str: Return Value of the text to be displayed in the message dialog box: Boolean
Return Value:
Returns true if you click OK.
If you click "cancel", false is returned.
Note: the return value can be used to determine the button clicked by the user.