Operators, Statements, Functions of the Basic Concepts of JavaScript

unary operator

Operators that operate on only one value are called unary operators.

Increment and decrement operators

Front type

var age = 29;
++age;
alert(age);     		 	//30

var age2 = 29;
--age2;
alert(age2);				//28

var num1 = 2;
var num2 = 20;
var num3 = --num1 + num2; // 21
var num4 = num1 + num2; // 21
rear type

var age = 29;
age++;
alert(age);					//30

var num1 = 2;
var num2 = 20;
var num3 = num1-- + num2; // 22
var num4 = num1 + num2; // 21
Unary Addition and Subtraction Operators
var num = 25;
num = +num; //still 25

//The unary operator is mainly used to represent negative numbers, such as converting 1 to -1
var num = 25;
num = -num; //-25
Boolean Operators
There are 3 Boolean operators: NOT (NOT), AND (AND) and OR (OR)
Logical NOT
The logical NOT operator is represented by an exclamation mark (!) and can be used for any value in ECMAScript. This operator returns a boolean value regardless of the data type of the value.
The logical NOT operator first converts its operand to a boolean value and then negates it.
The logical NOT operator conversion rule is the opposite of the Boolean() conversion rule
alert(!false);		//true
alert(!"blue");		//false
alert(!0);			//true
alert (! NaN); //threaten
alert(!"");			//true
alert(!123456);		//false
//Using two logical NOT operators at the same time will actually simulate the Boolean() transformation function.
alert(!!"blue");	//true
alert(!!0);			//false
alert (!! NaN); // false
alert(!!"");		//false
alert(!!12345);		//true
Logical AND
The logical AND operator is represented by two ampersands (&&) and has two operands

var result = true && false		//false
Returns true only if both operands are true The
logical AND operation can be applied to any type of operand, not just booleans. In the case where one operand is not a boolean value, the logical AND operand does not necessarily return a boolean value; in this case, the following rules should be followed:
1. If the first operand is an object, return the second operand
2. If the second operand is an object, the object is returned only if the first operand evaluates to true
3. If both operands are objects, the second operand is returned
4 .If one operand is null, return null
5. If one operand is NaN, return NaN
6. If one operand is undefined, return undefined

If the first operand can determine the result, then the second operand will not be evaluated (for example, if the first operand is false, the result must be false). The
logical or
logical or operator consists of two vertical bars. The symbol (||) means that there are two operands

var result = true || false    //true
False will be returned only when both operands are false.

Similar to logical AND, if one of the operands is not a boolean, the logical OR does not necessarily return a boolean value. The following rules are followed:
1. If the first operand is Object, returns the first operand
2. If the first operand evaluates to false, returns the second operand
3. If both operands are objects, returns the first operand
4. If both operands are null, return null
5. If both operands are NaN, return NaN
6. If both operands are undefined, return undefined

You can use the behavior of logical OR to avoid assigning variables to variables null or undefined value
var myObject = obj1 || obj2;
The variable myObject will be assigned one of the two values ​​following the equals sign. If the value of obj1 is not null or undefined, then its value is assigned to myObject; if it is null, then obj2 is assigned to myObject
Multiplicative operator
ECMAScript defines three multiplicative operators: multiplication, division and modulo.

multiplication

var result = 34 * 56;
 division
var result = 66/11;
Find the modulo (remainder)
var reslut = 26% 5; // 1
Additive operator
addition

var result = 1 + 2; // 3
var result2 = 5 + "5";		//55
Subtraction
If one of the operands is an object, a boolean value, null or undefined, the Number() function is automatically called in the background to convert it to a value, and then the calculation is performed.

var result = 2 - 1; // 1
var result1 = 5 - true; //4, because true is converted to 1
var result2 = NaN - 1; // NaN
var result3 = 5 - ""; //5, because "" is converted to 0
var result4 = 5 - "2"; //3, because "2" is converted to 2
var result5 = 5 - null; //5, because null is converted to 0
Relational operators
less than (<), greater than (>), less than or equal to (<=), and greater than or equal to (>=), return a boolean value
var result1 = 5 > 3;   			//true
var result2 = 5 <3; // false
Equality Operators
Equal and Not Equal

Equal (==), Not Equal (!=) : Both operators convert their operands (often called a cast) before
comparingConvert null and undefined to any other value
var result1 = ("55" == 55); //true because it is equal after conversion
var result2 = ("55" === 55);//false, because different data types are not equal

var result3 = ("55" != 55); //false, because it is equal after conversion
var result4 = ("55" !== 55);//true, because different data types are not equal

Congruent and Inequal

Congruent (===), not congruent (!==) : Returns true if the two operands are equal or unequal without conversion

var result1 = ("55" == 55); //true because it is equal after conversion
var result2 = ("55" === 55);//false, because different data types are not equal

var result3 = ("55" != 55); //false, because it is equal after conversion
var result4 = ("55" !== 55);//true, because different data types are not equal

Conditional operator

var max = (num1 > num2) ? num1 : num2;
assignment operator

Represented by the equals ampersand (=)

var num = 10;
comma operator

var num1 = 1, num2 = 2, num3 = 3; //declare multiple variables at the same time
var num2 = {5, 1, 2, 4, 6}; //6 (assignment: always returns the last item of the expression)
do-while statement

var i = 0;
do{
	i += 2;
}while(i < 10)

alert(i);		//10
while statement
var i = 0;
while(i < 10){
	i += 2;
}
for statement

var count = 10;
for (var i = 0; i < count; i++){
	alert(i);
}

for(;;){ // infinite loop
	doThing();
}
for-in statement

for (var proName in window){
	document.write(proName);
}
label statement

Use the label statement to add labels to your code for future use.
Syntax: label:statement

start: for (var i=0; i < count; i++){
	alert(i);
}
The start tag defined here can be referenced by break or continue statements in the future. Labeled statements are generally used in conjunction with loop statements such as for statements.

break 和 continue

var num = 0

for (var i=1; i < 10; i++){
	if (i % 5 == 0){
		break;
	}
	num++;
}

alert(num);    //4

var num2 = 0

for (var i=1; i < 10; i++){
	if (i % 5 == 0){
		continue;
	}
	num2++;
}

alert(num2);    //8

//Use with label
var num3 = 0

outermost:
for (var i=0; i < 10; i++){
	for (var j=0; j < 10; j++){
		if (i ==5 && j==5){
			break outermost;
		}
		num3 ++;
	}
}

alert(num3); //55(The break statement will not only exit the inner for statement, but also exit the outer for statement)
with statement

The purpose of the with statement is to scope the code to a specific object. Mainly to simplify the work of writing the same object multiple times

var qs = location.search.substring(1);
var hostName = location.hostname;
var url = location.href;
Simplified as follows:
with(location){
	var qs = search.substring(1);
	var hostName = hostname;
	var url = href;
}
The location object is associated with the with statement. This means that inside the code block of the with statement, each object is first considered as a local variable, and if the definition of the
variable location object is queried for a property with the same name. If a property with the same name is found, the value of the location object property is used as the value of the variable.

switch statement

switch(i){
	case 25:
		alert("25");
		break;
	ase 35:
		alert("35");
		break;
	default:
		alert("Other");
}
//You can use any data type in a switch statement, whether it's a string or an object. Second, the value of each case is not necessarily a constant, it can be a variable or even an expression.
switch("hellow world"){
	case "hellow"+" world":
		alert("Hello World!");
		break;
	case "goodbye":
		alert("byebye");
		break;
	default:
		alert("Other");
}
// use expression
var num = 25;
switch(true){
	case num < 0:
		alert("Less than 0");
		break;
	case num >=0 && num <= 10
		alert("Between 0 and 10");
		break;
	case num > 10 && num <= 20
		alert("Between 10 and 20");
		break;
	default:
		alert("More than 20");
}
The switch statement uses the equality operator when comparing values, so no type conversion occurs (for example, the string "10" is not equal to the number 10)

function

function sayHi(name, message){
	alert("Hello "+ name + "," + message);
}
//The parameters of the function are not necessary, you can access the parameter array through the arguments object to get each parameter to the function
function sayHi(){
	alert("Hello " + arguments[0] + "," + arguments[1]);
}

//Use the length property to determine how many parameters are passed in
function howManyArgs(){
	alert(arguments.length);
}

howManyArgs("string", 45);	//2
howManyArgs();				//0
howManyArgs(12);			//1


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325375713&siteId=291194637