JavaScript Basics for Beginners to learn basic knowledge

Source: Internet
Author: User

1. A brief introduction to JavaScript
Ø most of the JavaScript spoken in the Web or book refers to client JavaScript.
Øjavascript is a lightweight, interpretive, object-oriented programming language.
Øjavascript characteristics
1 control the appearance and content of the document
2) Control Browser
3 Interaction with HTML forms
4 Interaction with the user
5 Read and write user status with cookies
6) Other
2. Lexical structure
2.1. Character Set
JavaScript programs are written in the Unicode character set.
2.2. Case sensitive
JavaScript is a case-sensitive language.
2.3. Note
://Any text in the following line is commented
/**/: Any text between/**/is commented
2.4. Direct volume
Direct amount: Data value that appears directly in the program
12//Digit 1.2//number "Hello World"//String ' Hi '//String True//Boolean value false//Boolean/JAVASCRIPT/GI//Regular expression NULL//null object {x:1, y:2}//Object initialization program [1, 2,3,4,5]//array Initialization program 2.5. Identifier
An identifier is actually a name. In JavaScript, identifiers are used to name variables, functions, or to
The label of some loops in the JavaScript code.
Identifier naming rule, the first character must be a letter, underscore, or dollar character, followed by letters, numbers, underscores, or dollar characters. Numbers are not allowed as the first character, so JavaScript can easily differentiate between identifiers and numbers.
2.6. Reserved words
Break
Todo
If
Switch
typeof
Case
Else
In
This
Var
Catch
False
instanceof
Throw
void
Continue
Finally
New
True
While
Default
For
Null
Try
With
Delete
function
Return
3. Data type and value
3.1. Digital
In JavaScript, numbers are not divided into integer types and floating-point types, and all numbers are
A floating-point type. JavaScript represents a number in a 64-bit floating-point format defined by the IEEE754 standard, which indicates a maximum value of ±1.7976931348623157 x 10308 and a minimum of ±5 x 10-324
3.2. String
In JavaScript, a string is a sequence of Unicode characters, numbers, punctuation, and so on.
column, with ' or ' can represent a string.
3.3. Boolean type
In JavaScript, a Boolean type has only true and false values of two.
3.4. function
In JavaScript, a function is considered to be a data type. For example: var square = function (x) {return x*x;} 3.5. The object
3.6. Array
3.7. Null
The JavaScript keyword NULL is a special value that represents "no value". Null is often treated as a special value of the object type, representing the value of "no object". Null is a unique value, distinct from all other values. If the value of a variable is NULL, then you will know that its value is not a valid object, array, numeric, String, and Boolean value.
3.8. Undefined
Undefined occurs when the following conditions are encountered:
Ø use of variables not declared
Ø uses a variable that has been declared but has not been assigned a value
Ø using properties that do not exist for the object

<script type= "Text/javascript" > var test; document.write (math.test+ "<br>"); A property document.write (test+ "<br>") that does not exist for the object is used; Variable try{document.write (test2+ "<br>") that has been declared but not assigned is used;//the variable with no declaration}catch (e) {document.write (e.message+) <br > "); } </script>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

Run Result:
Undefined
Undefined
' Test2 ' not defined
3.9. Date Object
3.10. Regular Expressions
3.11. Error Object
3.12. Wrapper object of basic data type
4. Variable
4.1. JavaScript variable Features
In JavaScript, variables have the following characteristics:
Ø can be invoked without declaring variables, such as: I=1;alert (i);
Ø variables can store values of any data type, such as: i=1;i= ' ddxkj ';
4.2. Declaring variables
Promote the use of the VAR keyword to explicitly declare a variable, for example: var i = 100; If you don't have an explicit sound
Ming a variable, JavaScript will help you implicitly declare it. If you explicitly define a variable with VAR, it has an initial value of undefined before it is assigned a value
Variables declared by Var are permanent, that is, an error is raised when the variables are deleted with the delete operator.
4.2.1. Repeated statements and omissions of statements
Using the VAR statement to declare the same variable multiple times is not only legitimate, but it does not cause any errors.
If you try to read the value of an undeclared variable, JavaScript generates an error. If you try to give an
When you assign a value with a variable that is declared by Var, javascript implicitly declares the variable. Note, however, that an implicitly declared variable is always created as a global variable, even if the variable is used only within a function. For example:
<script type= "Text/javascript" > function print () {str= "ddddd";//implicitly-declared variables are always created as global variables document.write (str+ "<br&") gt; ");} Print (); function Print2 () {document.write (str+ "<br>"); document.write (' AAA ' + "<br>"); } print2 (); </script>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

Run Result:
Ddddd
Ddddd
Aaa
4.3. Scope of variables
The scope of a variable is the region in the program that defines the variable. The scope of global variables is global, that is, it can be invoked everywhere in JavaScript code. The arguments for variables and functions declared within a function are defined only in the function body. They are local variables and the scope is local.
Within the function body, local variables use higher precedence than global variables of the same name. When a local variable with the same name as a global variable is defined inside the function body, the global variable is hidden. So try to avoid a situation where the function uses a global variable rather than a local variable, and it is possible to change the value of the entire variable used by other functions of the program, creating problems that are difficult to discover.
4.3.1. No block-level scopes
Unlike Java or C languages, JavaScript is not a block-level scope. In the function body, local variables can be used throughout the function, regardless of where the local variables are defined. For example:
<script type= "Text/javascript" > Function Test (o) {var i = 0; The use of if (typeof o!= "Object") {var j =//j is not limited to the IF conditional statement for (var k = 0; k < k++) {//k is not limited to a for loop Document.wri Te (k + "<br>"); } document.write (k + "<br>"); The value of K is document.write (j+ "<br>"); J is still defined, but it is possible that J is undefined if O is an object} test (' DDD '); </script>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

Run Result:
0
1
2
3
4
5
6
7
8
9
10
100
When a function defines a local variable with the same name as a global variable, the result is often not what the person wants. For example:
<script type= "Text/javascript" > var scope= "global"; function test () {document.write (scope+ "<br>");//print out undefined var scope= "local"; document.write (scope+ "<br>"); Print the Local} test (); </script>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

Run Result:
Undefined
Local
4.3.2. Undeclared and unassigned variables
Ø Undeclared variable: Reading an undeclared variable causes a run-time error to abort the program.
Ø Unassigned Variables: When you read an unassigned variable, you get a default value, that is, undefined.
<script type= "Text/javascript" > try{document.write (i+ "<br>"); }catch (e) {document.write (e.message+ "<br>"); } var J; document.write (j+ "<br>"); </script>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

Run Result:
' I ' not defined
Undefined
4.4. Basic types and reference types
Base type: Numeric, Boolean, null, undefined value
Reference types: Arrays, objects, functions
4.5. Variable Special operators
1) in operator
The in operator requires that the operand on the left is a string, or is convertible to a string, and the right-hand operand is an object or an array. If the value on the left side of the operator is a property name of its right object, it returns TRUE or false.
<script type= "Text/javascript" > var point = {x:1, y:1}; document.write (("X" in point) + "<br>"); document.write (("Y" in point) + "<br>"); document.write (("Z" in point) + "<br>"); Inherits the property, returns True document.write (("toString" in point) + "<br>"); </script>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

Run Result:
True
True
False
True
2) instanceof operator
The instanceof operator requires that its left-hand count is an object, and the right side is a class that returns True when the object is an instance of the class, or false.
<script> var d = new Date (); document.write ((d instanceof Date) + "<br>"); Returns True, all objects are instances of object type document.write ((d instanceof Object) + "<br>"); document.write ((d instanceof number) + "<br>"); var a = [1, 2, 3]; document.write ((a instanceof Array) + "<br>"); Returns true, all arrays are object document.write ((a instanceof object) + "<br>"); document.write ((a instanceof RegExp) + "<br>"); </script>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

Run Result:
True
True
False
True
True
False
3) typeof operator
The typeof operator is a unary operator, and before a variable, this person's arithmetic can be
Any type that returns a string that describes the type of operand.
Ø Numbers: Return number
Ø string: Return string
Ø Boolean: Return Boolean
Ø object, array, null: Return object
Ø undefined variable: return to undefined
<script> var d = new Date (); var a = [1, 2, 3]; document.write (typeof 11+ "<br>"); document.write (typeof ' ddxkj ' + "<br>"); document.write (typeof true+ "<br>"); document.write (typeof d+ "<br>"); document.write (typeof + "<br>"); document.write (typeof null+ "<br>"); document.write (typeof i+ "<br>"); </script>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

Run Result:
Number
String
Boolean
Object
Object
Object
Undefined
4) Delete operator
The delete operator is a unary operator that can delete an object's properties, arrays, or variables. Returns true if the deletion succeeds, or false if it cannot be deleted. However, not all properties and variables can be deleted, some internal core properties and client properties cannot be deleted, and run-time errors occur if you attempt to delete them. Variables defined with the var keyword cannot be deleted either. Returns true if delete Deletes a property that does not exist.
<script> var o = {x:1, y:2}; Use Var to define an object variable document.write (delete o.x+ "<br>"); Deletes an object property, returns True document.write (typeof o.x+ "<br>"); Property does not exist, returns "undefined" document.write (delete o.x+ "<br>"); Deletes a property that does not exist, returning true document.write (delete o+ "<br>"); Cannot delete var-defined variable, return false try{document.write (delete 1+ "<br>");//cannot delete integral type, return True}catch (e) {document.write ( E.message+ "<br>"); } x = 1; Implicitly defines the variable document.write (delete x+ "<br>"); You can delete an obscure definition variable, return true try{x;///Runtime Error: X is not defined}catch (ex) {document.write (ex.message+ "<br>"); } </script> </P> <P>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

Run Result:
True
Undefined
True
False
Cannot delete ' [number] '
True
' X ' not defined
5) void operator
Void is a unary operator that can appear before any type variable and has two uses:
Ø discard the value of the operand, such as a function, and then return to undefined
Ø Generate undefined
<script> function f () {return ' ddxkj '; } document.write (f () + "<br>"); document.write (void F () + "<br>"); Discard ' ddxkj ' string, return undefined document.write (void 0+ "<br>"); Generate undefined </script>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

Run Result:
Ddxkj
Undefined
Undefined

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.