Basic JavaScript operators and statements (2)

Source: Internet
Author: User

Javascript: Operator
In fact, if you have a computer language foundation, you should know that operators may even be more proficient than me. In this way, I will not explain them much. Let's talk about other unfamiliar 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 array or an object, if the value on the left is an attribute of the object on the right, true is returned. Copy codeThe Code is as follows: var objvalue = {x: 1, y: 7 };
Document. write ("x is in objvalue:" + ("x" in objvalue) + "<br/> ");
// Output x is in objvalue: true

2. Conditional operators (? :)
This operator is the only ternary operator in javascript (three counts). The first counts must be a Boolean value, and the second and third counts can be values of any type. If the value of the operation number is not true, the value is the value of the second operation number, and flase-is the third operation number.Copy codeThe Code is as follows: document. write (1> 0? 8: 4)
// Output 8, 1 must be greater than 0, so the second operation is returned.

3. typeof Operator
Typeof is a unary operator used to determine the type of the Operation number. For example, if a number is used to return the number, the string will return the string. Note: For null, it also returns the object type. This operator is usually used.Copy codeThe Code is as follows: document. write ("typeof number 8:" + typeof 8 + "<br/>"); // output typeof number 8: number
Document. write ("typeof string money:" + typeof ("money") + "<br/>"); // output typeof string money: string
Document. write ("typeof boolean true:" + typeof (true) + "<br/>"); // output typeof boolean true: boolean
Document. write ("typeof Array:" + typeof ([]) + "<br/>"); // output typeof Array: object
Document. write ("typeof Null:" + typeof (null) + "<br/>"); // output typeof Null: object
Document. write ("typeof Undefined:" + typeof (undefined) + "<br/>"); // output typeof Undefined: undefined

4. delete Operator
Delete is also a unary operator used to delete the attribute, array element or variable of the object specified by the Operation number. If the operation number is deleted successfully, true is returned. If the operation number cannot be deleted, false is returned.Copy codeThe Code is as follows: var deleteobj = {one: "one", two: "two", three: "three "};
Document. write ("delete element is succeed:" + (delete deleteobj. one) + "<br/>"); // output delete element is succeed: true
Document. write ("select one in deleteobj:" + typeof (deleteobj. one) + "<br/>"); // output select one in deleteobj: undefined
Document. write ("delete element is succeed:" + (delete deleteobj) + "<br/>"); // output delete element is succeed: false
Document. write ("delete defined x:" + (delete x) + "<br/>"); // output delete defined x: true
Var x = 1;
// Delete is reflected above, which can delete the attributes and variables of an object. Objects cannot be deleted and variables are not defined.

Javascript: Statement
1. if, else if statement
As it is a basic statement, I will not introduce it much. Let's take a few examples. The following control statements will be frequently used in the future.Copy codeThe Code is as follows: var expression;
If (! Expression) document. write ("I declared, but no undefined value" + "<br/>"); // output: I declared, but no undefined Value
// Because the expression value is undefined and undefined, it is converted to false in the boolean type.
If (! Null) document. write ("I used boolean is false" + "<br/>") // output: boolean is also false.
Var obj1 = {};
If (obj1) document. write ("obj1 is not a null object" + "<br/>"); // The output obj1 is not a null object.
If (! Obj1.one) document. write ("obj1.one is a null object" + "<br/>"); // obj1.one is a null Object
Similar statements may be frequently used and understood later. Not sloppy

2. switch statement, while, do... while, for, for... in
The above statements and other languages have nothing special, so I went directly to the question I learned about C # last month, but this time we used javascript.Copy codeThe Code is as follows: // execute to sort the Array
Function comparenumber (objarr1)
{
If (! Objarr1)
{
Throw ("parameter cannot be blank! ");
Return;
}
Var finished = true; // used to control the while LOOP
Do
{
Finished = false;
For (var I = 0; I <objarr1.length; I ++) // The length attribute of the variable array returns the length of the array.
{
If (objarr1 [I]> objarr1 [I + 1]) // compare
{
Var temp = objarr1 [I];
Objarr1 [I] = objarr1 [I + 1];
Objarr1 [I + 1] = temp;
Finished = true; // continue the loop until the above comparison conditions are not met.
}
}
} While (finished );
}
// This function executes a print task.
Function displayarray (arr)
{
For (var val in arr)
{
Document. write (arr [val] + "\ t"); // \ t is a tab
}
}
Var numberarray = [123,]; // declare an array
Comparenumber (numberarray); // sort
Displayarray (numberarray); // output
// Output: 2 3 34 45 54 65 123

3. with statement
Using with can reduce a large amount of input. In the javascript client, with can be used for deeply nested objects, but it runs slowly.
Summary: there are many other statements, so I won't repeat them here.

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.