Basic Introduction to javascript is suitable for beginners

Source: Internet
Author: User
Most JavaScript on the Internet or in books refer to client JavaScript. 1. A brief introduction to JavaScript
Most of the JavaScript mentioned in the internet or books refer to the client JavaScript.
JavaScript is a lightweight, interpreted, and object-oriented programming language.
Ø JavaScript features
1) control the appearance and content of the document
2) browser control
3) Interaction with HTML Forms
4) interaction with users
5) use cookies to read and write user statuses
6) Others
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. Notes
/// // Any text in the last line is commented out
/**/: Any text between/**/is commented
2.4. Direct Volume
Direct Volume: data values that appear directly in the program
12 // number 1.2 // number "hello world" // string 'Hi' // string true // Boolean value false // Boolean value/JavaScript/gi // Regular Expression null // null object {x: 1, y: 2} // object initialization program [2.5, 5] // array initialization program. identifier
An identifier is actually a name. In JAVASCRIPT, identifiers are used to name variables, functions, or
Some cyclic labels in JAVASCRIPT code.
Identifier naming rules. The first character must be a letter, underline, or dollar character, followed by letters, numbers, underscores, or dollar characters. Numbers cannot be the first character, so that JAVASCRIPT can easily distinguish between identifiers and numbers.
2.6. Reserved Words
Break
Do
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. Number
In JavaScript, numbers are not classified into integer and floating-point types.
Float Type. JavaScript uses the 64-bit floating point format defined by IEEE754 to represent a number. It can represent a maximum value of ± 1. 7976931348623157x10308, and a minimum value of ± 5x10-324.
3.2. String
In JavaScript, a string is a sequence consisting of Unicode characters, numbers, and punctuation marks.
Column. 'or "can represent a string.
3.3. Boolean
In JavaScript, The boolean type has only true and false values.
3.4. Functions
In JavaScript, a function is considered as a data type. Example: var square = function (x) {return x * x;} 3.5. Object
3.6. Array
3.7. null
The JAVASCRIPT keyword null is a special value, which indicates "no value ". Null is often seen as a special value of the object type, which represents the value of "no object. Null is a unique value, different from all other values. If the value of a variable is null, you will know that its value is not a valid object, array, number, string, or Boolean value.
3.8. undefined
Undefined is generated in the following situations:
Using undeclared Variables
Use a variable that has been declared but has not been assigned a value
Use attributes that do not exist in the object

<Script type = "text/JavaScript"> var test; document. write (Math. test + "<br>"); // the document does not exist in the object. write (test + "<br>"); // use the variable try {document. write (test2 + "<br>"); // No declared variable is used} catch (e) {document. write (e. message + "<br>");} script
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]


Running result:
Undefined
Undefined
'Test2' undefined
3.9. Date object
3.10. Regular Expression
3.11. Error object
3.12. Packaging objects of the basic data type
4. Variables
4.1. JavaScript variable features
In JavaScript, variables have the following features:
You can call variables that are not declared, for example, I = 1; alert (I );
The variable can store values of any data type, for example, I = 1; I = 'ddxkj ';
4.2. Declare Variables
We recommend that you use the var keyword to explicitly declare variables, for example, var I = 100.
A variable. JAVASCRIPT will help you declare it implicitly. If var is used to explicitly define a variable but no value is assigned, its initial value is undefined.
Variables declared by var are permanent. That is to say, an error is thrown when you delete these variables using the delete operator.
4.2.1 repeated statements and omissions
Using the var statement multiple times to declare the same variable is not only legal, but also does not cause any errors.
If you try to read the value of an undeclared variable, JAVASCRIPT will generate an error. If you try
When a value is assigned to a variable declared by var, JAVASCRIPT will implicitly declare the variable. Note that variables declared implicitly are always created as global variables, even if they are used only in one function. For example:

<Script type = "text/JavaScript"> function print () {str = "ddddd"; // variables declared implicitly are always created as global variables document. write (str + "<br>");} print (); function print2 () {document. write (str + "<br>"); document. write ('aaa' + "<br>");} print2 (); script
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]


Running result:
Ddddd
Ddddd
Aaa
4.3. Scope of Variables
The scope of a variable is the region of the variable defined in the program. The scope of a global variable is global, that is, it can be called everywhere in JAVASCRIPT code. The variables and function parameters declared in the function are defined only in the function body. They are local variables and the scope is local.
In the function body, local variables take precedence over global variables with the same name. When a local variable with the same name as the global variable is defined within the function body, the global variable is hidden. To avoid such a situation, if the function uses a global variable instead of a local variable, it is possible to change the value of all the variables used by other functions of the program, and some problems that are difficult to find.
4.3.1. No block-level scope
Unlike JAVA or C, JavaScript has no block-level scope. In the function body, local variables can be used throughout the function no matter where they are defined. For example:

<Script type = "text/javascript"> function test (o) {var I = 0; if (typeof o! = "Object") {var j = 100; // The use of j is not limited to the if Condition Statement for (var k = 0; k <10; k ++) {// k is not limited to for Loop document. write (k + "<br>");} document. write (k + "<br>"); // The value of k is 10} document. write (j + "<br>"); // j is still defined, but j may be undefined. If o is an object,} test ('ddd '); script
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]


Running 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 usually not intended by the programmer. For example:

<Script type = "text/javascript"> var scope = "global"; function test () {document. write (scope + "<br>"); // print the undefined var scope = "local"; document. write (scope + "<br>"); // print the local} test (); script
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]


Running result:
Undefined
Local
4.3.2. Undeclared variables and unassigned variables
Non-declared variables: When an un-declared variable is read, the program is aborted due to a running error.
Ø unassigned variables: when reading unassigned variables, a default value is obtained, 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 select all Note: If you need to introduce external Js, You need to refresh it to execute]


Running result:
'I' Is Not Defined
Undefined
4.4. Basic and reference types
Basic Type: Numeric value, Boolean value, null, undefined Value
Reference Type: array, object, Function
4.5. Special variable operators
1) in Operator
The in operator requires that the number of operations on the left be a string or can be converted to a string. The number of operations on the right is an object or an array. If the value on the left of the operator is an attribute name of the object on the right, true is returned; otherwise, false is returned.

<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 and returns true document. write ("toString" in point) + "<br>"); script
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]


Running result:
True
True
False
True
2) instanceof Operator
The instanceof operator requires that the number of operations on the left be an object and a class on the right. If the object is an instance of the class, true is returned; otherwise, false is returned.

Script var d = new Date (); document. write (d instanceof Date) + "<br>"); // returns true. All objects are Object-type instance documents. 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 documents. write (a instanceof Object) + "<br>"); document. write (a instanceof RegExp) + "<br>"); script
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]


Running result:
True
True
False
True
True
False
3) typeof Operator
The typeof operator is a unary operator. Before a variable, this operator can be
Returns a string of any type, indicating the type of the number of operations.
Ø number: return number
Ø string: Return string
Ø boolean: Return boolean
Ø object, array, and null: return object
Define variable: Return 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 a + "<br>"); document. write (typeof null + "<br>"); document. write (typeof I + "<br>"); script
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]


Running result:
Number
String
Boolean
Object
Object
Object
Undefined
4) delete Operator
The delete operator is a unary operator that can delete attributes, arrays, or variables of an object. If the deletion is successful, true is returned. If the deletion fails, false is returned. But not all attributes and variables can be deleted. Some internal core attributes and client attributes cannot be deleted. If you try to delete these attributes, a running error may occur. Variables defined with the var keyword cannot be deleted. Returns true if delete deletes a non-existent attribute.

Script var o = {x: 1, y: 2}; // use var to define an object variable document. write (delete o. x + "<br>"); // deletes an object attribute and returns true document. write (typeof o. x + "<br>"); // the property does not exist. "undefined" document. write (delete o. x + "<br>"); // deletes a nonexistent attribute and returns true document. write (delete o + "<br>"); // The variable defined by var cannot be deleted, and false try {document. write (delete 1 + "<br>"); // The integer cannot be deleted. Return true} catch (e) {document. write (e. message + "<br>") ;}x = 1; // implicitly defines the variable document. write (delete x + "<br>"); // you can delete the implicit definition variable and return true try {x; // an error occurs during running: x not defined} catch (ex) {document. write (ex. message + "<br>");} script </P> <P>
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]


Running result:
True
Undefined
True
False
You cannot delete '[number]'.
True
'X' undefined
5) void Operator
Void is a unary operator that can appear before any type variable. It has two functions:
Discard the value of the number of operations, such as a function, and then return undefined
Ø generate undefined

Script function f () {return 'ddxkj ';} document. write (f () + "<br>"); document. write (void f () + "<br>"); // discard the 'ddxkj 'string and return the undefined document. write (void 0 + "<br>"); // generate undefined script
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]


Running result:
Ddxkj
Undefined
Undefined
Related Article

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.