JavaScript Advanced (ii)

Source: Internet
Author: User
Tags arithmetic operators array definition logical operators ming true true ticket javascript array

The "A>b" in mathematics is also expressed in JavaScript as a>b; "B is greater than a, a, or less than C" is a "a<b<c" then it can be represented in JavaScript with &&. As follows: B>a && b<c//"&&" is and means, reading "B is greater than a" and "B is less than C" as we participate in the college entrance examination, before entering the examination room, must produce the admission ticket and ID card, both indispensable, otherwise can not take the exam, The expression is as follows: if (with admission ticket && ID) {  to take the exam} "&&" is a logical AND operator, only the "&&" both sides of the value are satisfied (and true), the entire expressions are true. Logical AND Operator Values table: A B a&&b True (True) True (True) True (True) False (False) False (false) False (False) True (True) False (False) False (false) fake (false) False ( FALSE) Note: If A is false, A&&b is false and will not execute B; Conversely, if a is true, the value of a&&b is determined by the value of B. "||" The logical OR operator, equivalent to the "or" in life, when any one of the two conditions satisfies, the "logical or" result of the operation is "true". such as: Planning a trip this week, but from Monday to Friday to work, so Saturday or Sunday will be able to go any day. In two days, if you have a day, you can travel. var A=3;var b=5;var c;c=b>a| | A>b;//b>a is True,a>b is False,c is a true logical OR operator value table: a b a| | B True True (True) True (True) True (True) False (False) True (True) False (False) True (True) True (True) False (False) False (FALSE) "!" and ""! " is the logical non-operator, that is, the meaning of "no", not true or false, not false is true. Like Floret bought a cup today, Xiao Ming said: "The cup is white", Xiao Liang said: "The cup is red", Floret said: "Xiao Ming said is not the truth, Xiao Liang said is not a lie." So what color is the cup? The answer is red. Logical non-operator values table: A! A true or false really look at the following code, the value of variable C is what: Var a=3;var b=5;var c;c=! (b>a);//b>a is true,!. The value of (B>a) is Falsec=! (b<a);//b<a is false,!. The value of (B<a) is true we all know that division, multiplication operator precedence is higher than addition and subtraction, for example: Var numa=3;var numb=6;jq=numa+30/2-numb*3;//result is 0 if we want to change the order of operations, You need to add parentheses to change the priority: Var Numa=3;var numb=6;jq= ((numa+30)/(2-NUMB) *3;//result is-24.75 precedence between operators (high to Low): arithmetic operators > Comparison operators > Logical operators > "=" assignment symbol if the operation of the sibling is done in the left-to-right order, the multi-level brackets are from the inside out. var Numa=3;var numb=6;jq=numa+30>10&&numb*3<2;//result is false we know that variables are used to store data, and a variable can store only one content. Suppose you want to store 10 people's names or store 20 People's math scores, you need 10 or 20 variables to store, and if you need to store more data, it becomes more troublesome. We use arrays to solve problems, and an array variable can hold multiple data. An array is a collection of values that you can add more values to the array as needed. Each value has an index number starting at 0 each index in the array has a corresponding value. The array is created first, and the array itself needs to be assigned to a variable. It's like we're going out for a group and have a name for the tour. Syntax for creating an array: var myarray=new array (), Var myarray is the variable that holds the array new array () is the creation of a new empty array The entire statement is to create a new array stored in the myarray variable. As we create arrays, we can also specify the length of the array, which can be arbitrarily specified. such as: Var myarray=new array (8);//create an array and store 8 data. Note: 1. The new array created is an empty array, and no value, such as output, displays Undefined2. Although the array is created with a length specified, the array is actually longer, meaning that the element can still be stored within the specified length even if the length is specified as 8. Ok array created OK next we'll assign a value to the array. We put the array looks like the tour bus, the bus has a lot of locations, each location has a number, the customer to do in which location? Step One: Group bus The second step: according to the ticket seat of the bus 1th is the Zhang San Bus seat 2nd is the expression of the John Doe array: The first step: Create an array var myarr=new array (); Second step: Assign values to the array myarr[1]= "Zhang San"; myarr[2]= "John Doe"; Create an array below to store the math scores of five people. var myarray=new array ();//Create a new empty array myarray[0]=66;//store the score of the first person myarray[1]=88;//store the results of the second person myarray[2]=77;// Store a third person's score myarray[3]=33;//store fourth person's score myarray[4]=78;//store fifth person's score note: The array has an index number for each value, starting with 0. We can also create the above array and assignment in a simple way: The first method: var myarray=new array (66,88,77,33,78);//create array at the same time assign a value the second method: Var myarray=[66,88,77,33,78];// Enter an array directly (called "literal array") Note: The data stored in an array can be any type (numeric, character, Boolean, etc.) here is an example of storing five people's scores, if now more than one person's results, how to store it? You only need to use the next unused index, and you can add new elements to the array at any time. myarray[5]=90;//uses a new index to add a new element to the array. The most important point here is the index, knowing what the index is, and using it as a basis for searching. If we want to know the size of the array, simply reference an attribute of the array length. The Length property represents the size of the array, which is the number of elements in the array. Syntax: myarray.length;//gets the length of the array myarray Note: Because the index of an array always starts with 0, the upper and lower bounds of an array are 0 and length-1, respectively. The length of the array is 5, and the upper and lower bounds of the arrays are 0 and 4. For example: Var arr=[ 1,2,3,4,5,6,7,8];//contains an array of 8 values Arrdocument.write (arr.length);//display array length 8document.write (arr[7]);//Display the value of the 8th element 8 at the same time, The length property of the JavaScript array is variable, which requires special attention. arr.length=10;//increases the length of the array document.write (arr.length);//The array length has changed to 10 as the element increases, the length changes as follows: Var arr=[98,76,54,32,11];// An array containing 5 values document.write (arr.length);//Displays the length of the array 5arr[15]=34;//add elements, usingThe index is 15 and the assignment is 34alert (arr.length);//display array length 161-dimensional array, we can see a set of boxes, each box can only put one content. The representation of one-dimensional arrays: myarray[] Two-dimensional arrays, we look at a set of boxes, but in each box can also put more than one box. Representation of a two-dimensional array: myarray[][] Note: The index values for the two dimensions of the two-dimensional array are also starting from 0, and the last index value for the two dimensions is length-1.1. Method of defining a two-dimensional array a var myarr=new array ();//First declare one-dimensional for (Var i=0; i<2;i++) {//one-dimensional length is 2 myarr[i]=new Array ();//re-declared two-dimensional  for (var j=0;j<3;j++) {//two-D length is 3 myarr[i][j]=i+ j;//assignment, the value of each array element is i+j} } Note: The description is updated after the FOR Loop statement. The above two-dimensional array is represented as a table: 0 1 myarr[0][0] myarr[0][1] myarr[0][2] Value: 0 Value: 1 value: myarr[1][0] myarr[1][1] myarr[1][2] Value: 0 Value: 2 value: 3&nbsp 2. Two-dimensional array definition method two var myarr=[[0,1,2],[1,2,3]]3. Assignment myarr[0][1]=5;//The value of 5 into the array, overwriting the original value Description: myarr[0][1],0 represents the row of the table, and 1 represents the table's columns??? Throw an exercise: Using the JavaScript language, put the following array    var  arr = [' * ', ' # # ', ' * * ', ' && ', ' * * * ', ' ##* '];   arr[ 7] = "* *"; Displays the pattern shown in the page: **********    <! doctype  html>arr[2]= "* *";
Alert ("The length of the array is" +arr.length);
for (Var i=0;i<7;i++) {
if (i%2==0) {
document.write (arr + "<br>");
}else{
document.write ("" + "<br>");
}
}
</script>

JavaScript Advanced (ii)

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.