Home >Web Front-end >JS Tutorial >JavaScript Basics: Operators and Statements (2)_Basic Knowledge

JavaScript Basics: Operators and Statements (2)_Basic Knowledge

WBOY
WBOYOriginal
2016-05-16 17:54:38943browse
javascript: Operator
In fact, if you have a computer language foundation, you should all know about operators, and you may even be more proficient than me, so I won’t explain more, but will talk about a few other unfamiliar ones. Bar!
1. in operator: The in operator requires that the operand on the left is a string or can be converted to a string, and the operand on the right is an array or object. If the value on the left is the object on the right, A property that returns true.
Copy code The code is as follows:

var objvalue = {x :1,y:7};
document.write("x is in objvalue:" ("x" in objvalue) "
");
//Output x is in objvalue: true

2. Conditional operator (?:)
This operator is the only ternary operator (3 operands) in JavaScript. The first operand must be a Boolean value, the second and third operands can be values ​​of any type. If the value of the operand is not true, the value is the value of the second operand, and false- is the value of the third operand.
Copy code The code is as follows:

document.write(1>0?8:4)
//Output 8, 1 is definitely greater than 0, so the second operand is returned

3. typeof operator
typeof is a unary operator used to determine the type of the operand , for example, if the operation is a number, it returns number, and if the string operation is a string, it returns string. Note: For null, it returns object type. This operator is still used quite often.
Copy code The code is as follows:

document.write("typeof number 8:" typeof 8 "
");//Output typeof number 8:number
document.write("typeof string money :" typeof("money") "
");//Output typeof string money :string
document.write("typeof boolean true :" typeof(true) "
");//Output typeof boolean true :boolean
document.write("typeof Array :" typeof([]) "
");//Output typeof Array :object
document.write("typeof Null :" typeof(null) "
"); //Output typeof Null :object
document.write("typeof Undefined:" typeof(undefined) "
");//Output typeof Undefined:undefined

4 , delete operator
delete is also a unary operator, used to delete the attributes, array elements or variables of the object specified by the operand. If the deletion is successful, it will return true. If the operand cannot be deleted, it will return false.
Copy code The code is as follows:

var deleteobj = {one:"one",two: "two",three:"three"};
document.write("delete element is succeed:" (delete deleteobj.one) "
");//Output delete element is succeed:true
document.write("select one in deleteobj :" typeof(deleteobj.one) "
");//Output select one in deleteobj :undefined
document.write("delete element is succeed:" (delete deleteobj) "
");//Output delete element is succeed:false
document.write("delete defined x:" (delete x) "
");//Output delete defined x:true
var x = 1;
//The above reflects that delete can delete the properties and variables of the object, but cannot delete the object and undefined variables.

javascript: statement
1. if, else if statement
Since it is a basic statement, I won’t introduce it in detail. Let’s just give a few examples. , the following control statements will be commonly used in the future.
Copy code The code is as follows:

var expression;
if(!expression) document .write("I declared it, but there is no undefined value" "
"); //Output: I declared it, but there is no undefined value
//Because the value of expression is undefined. , when using the boolean type, it is converted to false.
if(!null)document.write("It is also false when I use it in boolean" "
") //Output: It is also false when I use it in boolean
var obj1 = {};
if(obj1)document.write("obj1 is not a null object" "
");//Output obj1 is not a null object
if(!obj1.one)document.write( "obj1.one is a null object" "
");//obj1.one is a null object
You may use statements similar to the above frequently in the future, so you must understand them. Don’t be careless

2. Switch statement, while, do...while, for, for...in
The above statement has nothing special with other languages. Let’s go directly to the question I did when I learned C# last month. But this time we are using javascript.
Copy the code The code is as follows:

//Execute Array sorting
function comparenumber(objarr1)
{
if(!objarr1)
{
throw("The parameter cannot be empty!");
return;
}
var finished = true; //Used to control the while loop
do
{
finished = false;
for(var i=0;i{
if(objarr1[i]>objarr1[i 1])//Compare
{
var temp = objarr1[i];
objarr1 [i] = objarr1[i 1];
objarr1[i 1] = temp;
finished = true;//Continue to loop until the above comparison condition is not satisfied and the while loop will stop.
}
}
}while(finished);
}
//This function performs the printing task
function displayarray(arr)
{
for( var val in arr)
{
document.write(arr[val] "t");//t is a tab character
}
}
var numberarray = [34,45 ,2,3,54,65,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 lot of input. In the JavaScript client, deeply nested objects can use with, but it runs slower.
Summary: In fact, there are many other sentences, so I won’t repeat them here.
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn