This study focuses on basic JavaScript syntax, data structure
Whether it is a traditional programming language or a scripting language, there are basic elements such as data types, constants and variables, operators, expressions, comment statements, Process Control statements, and the basic elements that make up the foundation.
First, the basic syntax of JavaScript
1. Order of execution:
The JavaScript program executes line by row in the order in which they appear in the HTML file. If you need to do this in an entire HTML file, it's best to put it in the
2, Case-sensitive:
JavaScript is sensitive to letter case, meaning that when inputting keywords, functions, variables, and other identifiers of a language, it is important to strictly distinguish the case of letters, such as variable username and variable username, which are two different variables. (Many JavaScript objects and properties have the same name as the HTML tags or attributes they represent, and in HTML, they can be entered in any case without causing confusion, but in JavaScript, these names are usually lowercase.) )
3, Semicolon:
In JavaScript statements, it is best to add a semicolon ";" at the end of each line, which guarantees the accuracy of each line of code.
Alert ("How is You"), alert ("How is You") ;
4. Spaces: JavaScript ignores extra spaces, and users can add spaces to the script to improve their readability.
as follows: Var name= "JavaScript"; var name = "JavaScript";
5. Comments:
Annotations are functions that explain the code of the program, increase the readability of the code, or block the execution of the code, not the execution of the program. Comments in JavaScript are divided into single-line comments and multiline comments.
Single-line Comment: A single-line comment is just one line, and the comment symbol is//, and the symbol is followed by the contents of the comment until the line ends.
code://Below is just a line of text alert ("I am the hint text");
Multiline Comment: Multiple lines of comments can be commented on multiple lines of code at a time, the multiline comment symbol starts with/* and ends with */.
<script type= "Text/javsscript" >
/* Below is a popup message box in this line of code, without any variables or parameters
Actually just a line of text */alert ("I am the hint text");
</script>
Typically:
If "//" is at the beginning of a line, it is used to explain the functionality of the next line or code. (Observation line comment)
If "//" is at the end of a line, it is used to interpret the functionality of the current line code.
If it is used to block the execution of a line of code, put "//" before the first character in a line.
6. Statement:
A JavaScript program is a collection of statements, and a JavaScript statement is equivalent to a complete sentence in English. JavaScript statements combine expressions to accomplish certain tasks. A statement has one or more combinations of expressions, keywords, or operators, separated by semicolons (;), that is, a semicolon is the ending symbol for a JavaScript statement.
code: var today=new Date ();
5. Statement BLOCK:
A statement block is a collection of statements that are usually enclosed in a pair of curly braces.
When the statement is called, JavaScript executes the statements in the statement block as it is written. The function of a statement block is to execute the sequence of statements together, and JavaScript functions are a typical example of combining statements in blocks, see the following example:
"Run a function that can manipulate two HTML elements":
<!doctype html>
:
Click "Click here" button to see two elements have changed, "document.getElementById ()" means: According to the ID to get the object in the page.
Second,javascript data structure
Each computer programming language has its own data structure, and the data structure of the JavaScript scripting language includes: identifiers, constants, variables, keywords, reserved words, and so on.
Identifier: When defining features such as variables and functions in JavaScript, a given name is required, and the sequence of characters used to define the feature is called an identifier. Identifiers are subject to the following naming conventions: (1), identifiers can only be composed of letters, numeric underline and Chinese, and not include spaces, identifiers, operation symbols and other symbols. (2), the first letter of the identifier must follow the letter, underline or Chinese, the number can not be used as the first character and can not use punctuation, operation symbols. (3), the identifier cannot be the same as the keyword name in JavaScript.
|
Like what: Examples of legal identifiers: UserName Int2 _file_open Sex Example of an illegal identifier: 88border The number cannot be used as the first character It ' S-all-right
Punctuation and arithmetic symbols cannot be used. |
"keywords", "reserved Words" in JavaScript
Javascript reserved words, keywords can not be used as a variable, label or function name. Some reserved keywords are used as JavaScript extensions later.
Keyword word list:
Break |
Break |
Break |
Break |
Default |
Delete |
Do |
Else |
Finally |
For |
function |
If |
Inch |
instanceof |
New |
Return |
Switch |
This |
Throw |
Try |
typeof |
Var |
void |
While |
With |
|
|
|
Reserved word word list:
Abstract |
Boolrean |
Byte |
Char |
Class |
Const |
Debugger |
Double |
Enum |
Export |
Extends |
Final |
Float |
Goto |
Implements |
Import |
Int |
Interface |
Long |
Native |
Package |
Private |
Protected |
Public |
Short |
Static |
Super |
Synchronized |
Throws |
Transient |
Volatile |
|
Constant:
JavaScript constants, often referred to as literal constants, are information that is solidified in the program code, and the values of the constants are fixed from the beginning of the definition.
numeric values (integers and real numbers) and string literals (characters or numbers enclosed in "or") are constants.
Variable:
Is that the value can be changed while the program is running. Variables are used to store data of a particular data type, and the variable name represents its storage space.
The program can store values and take out values in variables, you can compare variables to supermarket shelves (memory), shelves of goods (variables), you can take goods out of the shelves (read), you can also put goods into the shelves (Assignment).
(1), the name of the variable:
The name of the variable is an identifier, and the name of the variable can be any length. When you create a variable name, you need to follow these command rules:
{1}, the first character must be a letter or an underscore (_), cannot be text, the number cannot be used as the first character and cannot use punctuation, operation symbols.
{2}, variable names are strictly case-sensitive. For example, the variable name mycounter is different from the variable name mycounter.
{3}, variable names cannot be JavaScript keywords and reserved words.
Name of the variable: The name of the variable is an identifier, and the name of the variable can be any length. When you create a variable name, you need to follow these command rules: (1), the first character must be a letter or an underscore (_), cannot be text, the number can not be used as the first character and can not use punctuation, operation symbols. (2), variable names are strictly case-sensitive. For example, the variable name mycounter is different from the variable name mycounter. (3), variable names cannot be JavaScript's "keywords" and "reserved Words".
|
Like what: Examples of legal identifiers: _pagecount Part9 Numer Example of an illegal identifier: 12balloon The number cannot be used as the first character Summary&went
"and" symbols cannot be used in variable names |
(2), Declaration and assignment of variables
The declaration of a variable specifies a name for the variable. Once you declare variables, you can use them as storage units.
The keyword VAR is used in JavaScript to declare a variable, and the string after the keyword will represent a variable name.
code: var identifier;
A keyword var can declare multiple variable names at the same time, and multiple variable names must be separated by a comma ",".
such as: Var username,pwd,age;
Assignment of variables:
You can use the assignment operator in JavaScript, which is the equals sign (=).
When declaring a variable name, declare the variable username and assign the value "Old Luo hu Hu
The code is as follows: Var username= "Old Luo Lake";
Here's a simple example: create a variable named Treename, assign it a peach tree, and put it in an HTML paragraph id= "demo".
<!doctype html>
:
, you can see that two elements have changed by clicking the "click Here" button.
Reprint http://www.cnblogs.com/KTV123/p/6057904.html
Basic syntax, data structure of JavaScript