Getting started with JavaScript

Source: Internet
Author: User
JavaScript has six data types. The main types include number, string, object, and Boolean. The other two types are null and undefined.
String type: the String is described by single quotation marks or double quotation marks. (Use single quotes to enter strings containing quotes .) For example, "The cow jumped over the moon ."
Numeric data type: JavaScript supports integer and floating point numbers. An integer can be a positive number, 0, or negative number. A floating point number can contain a decimal point or an "e" (both case and case sensitive. It indicates the "10 power" in the scientific Notation ") or both.
Boolean Type: Possible Boolean values include true and false. These are two special values and cannot be used as 1 or 0.
Undefined data type: A value of undefined refers to the value of Undefined after the variable is created but not assigned to the variable.
Null data type: null indicates that there is no value and nothing is expressed.
Object Type: In addition to the common types mentioned above, objects are also an important part of JavaScript. This part will be detailed in later sections.

In JavaScript, variables are used to store the values in the script. In this way, variables can be used to represent the values. A variable can be a number, text, or something else.
JavaScript is a language that does not have very strict requirements on data type variables, so you do not have to declare the type of each variable. Variable declaration is not necessary, but it is a good habit to declare variables before using them. You can use the var statement to declare variables. For example, var men = true; // The value stored in men is of the Boolean type.
Variable naming: JavaScript is a case-sensitive language. Therefore, naming a variable computer is different from naming it Computer.
In addition, the variable name length is arbitrary, but the following rules must be observed:
1. The first character must be a letter (uppercase or lowercase), an underscore (_), or a dollar sign ($ ).
2. Subsequent characters can be letters, numbers, underscores, or dollar characters.
3. The variable name cannot be a reserved word.
The statements provided by JavaScript are classified into the following categories:
1. Variable declaration, value assignment statement: var.
Syntax: var variable name [= initial value]
For example, var computer = 32 // define a computer as a variable with an initial value of 32.

2. function Definition Statement: function, return.
Syntax: function name (parameters included in the function)
{
Function execution
}

Return expression // return statement indicates the value to be returned.
Example: function square (x)
{
Return x * x
}

3. Condition and branch statement: If... else, switch.
If... the else statement completes the branch Function in the program flow block: if the condition is true, the program executes the statement or statement block followed by the condition; otherwise, the program executes the statement or statement block in the else. Syntax: If (condition)
{
Execute Statement 1
} Else {
Execute Statement 2
}
Example: If (result = true)
{
Response = "you are correct !"
} Else {
Response = "You are wrong !"
}

The branch statement switch can take different processing methods based on different values of a variable.
Syntax: Switch (expression)
{
Case label1: Statement string 1;
Case label2: Statement string 2;
Case label3: Statement string 3;
...
Default: Statement string 3;
}
If the value obtained by the expression does not match any statement provided by the program, the default statement is executed.

4. Loop statement: for, for... in, while, break, and continue.
The syntax of the for statement is as follows: for (initialization part; condition part; update part)
{
Execution part...
}
As long as the cycle condition is true, the loop body is executed repeatedly.
The for... in statement is a little different from the for statement. Its loop range is all attributes of an object or all elements of an array.

For... in statement Syntax: for (variable in object or array)
{
Statement...
}

The loop controlled by the while statement is a constant test condition. If the condition is always true, the loop continues until the condition is no longer true.
Syntax: while (condition)
{
Execute the statement...
}

The break statement ends the current loop and executes the next statement of the loop.

The continue statement ends the current loop and starts the next loop immediately.

5. Object operation statement: with, this, new.
The syntax of the with statement is as follows:
With (Object Name ){
Execution statement
?}
The function is as follows: if you want to use many attributes or methods of an object, you only need to write the object name in () of the with statement, then, you can directly write the property name or method name of the object in the following execution statement.

The new statement is an object constructor. You can use the new statement to define a new object.
The syntax is as follows: new object name = New Real Object Name
For example, we can define a new date object: var curr = new date (). Then, the variable curr has the attribute of the date object.

This operator always points to the current object.

6. Comment statement ://,/*...*/.
// This is a single line comment
/* This can be annotated with multiple lines ....*/

In JavaScript, you can use the following objects:
1. objects automatically provided by the browser based on the content on the web page.
2. Javascript built-in objects, such as date and math.
3. Inherent objects on the server.
4. User-defined objects.
Objects in JavaScript are composed of two basic elements: attributes and methods.
You can use this method to access the attributes of an object: Object Name. attribute name, for example, mycomputer. Year = 1996, mycomputer. Owner = "me ".
You can use this method to associate the object method with the function: object. method Name = function name or object. attribute. method Name, for example, this. display = display, document. writeln ("This is method ").

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.