JavaScript Basics-array-Conditional JUDGMENT statement-logical operator-SELECT STRUCTURE Statement-loop statement

Source: Internet
Author: User
Tags logical operators pear


var ateam=new Array (12); In JS, the array is declared using the keyword array, and colleagues can specify the number of the array, that is, the length of the array.

var acolor=new Array (); When you do not know the number of arrays, the declaration array can not specify the number of arrays, array Acolor defines 4 arrays, if you need to add additional colors later. can be defined acolor[4] acolor[5]
For each additional array item, the length of the array will grow dynamically.
Acolor[0]= "Blue";
Acolor[1]= "Blue";
Acolor[2]= "Blue";
Acolor[3]= "Blue";

var amap=new Array ("China", "USA", "Britain"); You can create an array directly with parameters.


var amap=new Array ("China", "USA", "Britain"); Gets the length with lengths, [2] Gets the parameters of the array (Britain)
Alert (amap.length+ "" +amap[2]);

var amap=new Array ("China", "USA", "Britain");
Amap[20]= "Korea";

Alert (amap.length+ "" +amap[10]+ "" +amap[20]);


var amap=["China", "USA", "Britain"; In addition to the array () object, the array can also be defined with square brackets []
Amap[20]= "Korea";

Alert (amap.length+ "" +amap[10]+ "" +amap[20]);

var amap=["China", "USA", "Britain"]; the array is usually converted to a string for use. ToString () can be very convenient to implement this function


Alert (amap.tostring () + "" +typeof (Amap.tostring ()); typeof is used to determine what kind of

var amap=["China", "USA", "Britain"; Join can also convert an array to a string
document.write (Amap.join () + "<br>"); No parameter, equivalent to ToString
document.write (Amap.join ("") + "<br>"); No connector
document.write (Amap.join ("[") + "<br>"); Connect using the [character]
document.write (Amap.join ("-isaac-") + "<br>"); -isaac-Connection


var sfruit= "Apple,pear,peach,orange"; The split () method converts a string to an array.
var afruit=sfruit.split (",");//split accepts a parameter, which is the identity of the split string
alert (afruit);


var afruit= "Apple,pear,peach,orange". Split (",");//split accepts a parameter, which is the identity of the split string
alert (afruit);

var afruit=["Apple", "pear", "peach", "orange"]; The reverse () method causes the array to be sorted in reverse order
Alert (Afruit.reverse ());


var afruit=["Apple", "pear", "peach", "orange"]; The reverse () method causes the array to be sorted in reverse
Alert (Afruit.reverse ());


var smystring= "ABCDEFG"; The string is arranged in reverse order, but the string does not reverse this function, so we must first turn the value into
Alert (Smystring.split (""). Reverse (). Join ("")); becomes an array, and then uses the reverse method to reverse the array, and then uses the join to turn the array into a string.


var afruit=["pear", "apple", "peach", "orange"]; Sort the array elements using the sort () method, sorted alphabetically
Alert (Afruit.sort ());


var stack=new Array ();
Stack.push ("Red");
Stack.push ("green");
Stack.push ("Blue");
document.write (stack.tostring () + "<br>") Pusu Press stack. Pop out of the stack
var vitem=stack.pop ();
document.write (vitem+ "<br>");
document.write (Stack.tostring ());

--------------------------------------------------------------------------------------------------------------- -------------------------------------

Conditional statements

Comparison operators
mainly includes equals = =, not equal to =, greater than, greater than or equal to >=, less than <, less than or equal to <=,
document.write ("pear" = = "pear");
document.write ("Apple" < "Orange"); String comparisons compare sizes with Unicode code bits
document.write ("Apple" < "Orange");


Alert ("Apple". toUpperCase () < "Orange". toUpperCase ()); toUpperCase () and toLowerCase () the case of the two converted strings



logical operators

document.write (3>2 && 4>3); The entire expression is ture, or flase, when the two are ture. &&.
document.write ("<br>")
document.write (3>2 && 4<3); or arithmetic | |, which represents two conditions as long as one is ture, the entire expression is true, otherwise false.
document.write ("<br>")
document.write (4>3 | | 3>2); Non-op! is simply to change true to Flase,flase to True
document.write ("<br>")
document.write (! ( 3>2));
document.write ("<br>")


The IF statement is the most commonly used conditional selection statement. There are three types of if statements

if (condition) {} A single branch SELECT statement condition is true to run {} contents, the Flase program skips {} statements and executes other statements directly behind the program.

if (condition)
{bidirectional branching Select struct statement condition true run if code, condition flase Run Else code
}
else{

}


The IF (condition 1) Multi-branch selection structure statement is used to select one of several code blocks to execute.
{
Code to execute when the condition is true
}else if (condition 2)
{
Code to execute when the bar is true

}else{
Code executed when condition 1 and condition 2 are both flase
}
If nested statements
var x=6;
var y=8;
if (x<4)
{
if (y==10)
{
Alert ("X<4 && y==10");
}
Else
{
Alert ("X<4 && y!=10");
}
}
else if (x>5)
{
if (y==10)
{
Alert ("X>5 && y==10");
}
Else
{
Alert ("x>5&&y!=10");
}
}


var inumber=number (Prompt ("Enter a number from 5 to 100"));
if (IsNaN (Inumber))
document.write ("Please confirm your input is correct");
else if (inumber>100| | INUMBER<5)
document.write ("The number range you entered is no longer between 5 and 100");
Else
document.write ("The number you entered is:" +inumber);

Switch statement
Use a switch statement when you need to judge more. Comparison of an expression and multiple values

Iweek=parseint (Prompt ("Please enter an integer between 1 and 7", ""));

Switch (Iweek) {
Case 1:
document.write ("Monday");
Break
Case 2:
document.write ("Tuesday");
Break
Case 3:
document.write ("Wednesday");
Break

Case 4:
document.write ("Thursday");
Break
Case 5:
document.write ("Friday");
Break
Case 6:
document.write ("Saturday");
Break
Case 7:
document.write ("Sunday");
Break
Default
document.write ("error");
}


While Loop statement is a pre-test loop, the loop condition is judged by the
The body of the loop may not be executed at all until the internal code is executed, and when the condition is ture, the condition executes the internal code continuously until the loop condition is flase.

var i=sum=0;
while (i<=100) {
Sum=i+sum;
i++;
}
alert (Sum);

JavaScript Basics-array-Conditional JUDGMENT statement-logical operator-SELECT STRUCTURE Statement-loop statement

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.