Foreground interface (4) --- functions and comparison operators, foreground Operators

Source: Internet
Author: User

Foreground interface (4) --- functions and comparison operators, foreground Operators

Directory

1. Functions

1.1. Function Parameters

1.2. variable scope

1.2.1. global scope

1.2.2. Local Scope

1.3. function return value

2. Comparison Operators

2.1. Equal OPERATOR: =

2.2. Equality OPERATOR: =

2.3. Unequal OPERATOR :! =

2.4. Incomplete equality operator :! =

2.5. Greater than OPERATOR:>

2.6. Operator greater than or equal to:> =

2.7. Less than OPERATOR: <

2.8. less than or equal to the operator: <=

2.9. Logic and operators :&&

2.10. Logic or operator: |

----------------------- Golden line ----------------------1. Function

In JavaScript, we can extract the repeated parts of the code and put them inFunction(Functions.

This is an example of a function:

Function functionName (){
Console. log ("Hello World ");
}

You can call this function by adding parentheses to the function name functionName, as shown in the following figure:

FunctionName ();

Every time a function is called, it prints the "Hello World" message to the development console. The code between all braces will be executed each time the function is called.

1.1. Function Parameters

The parameter parameters of the function acts as a placeholder (also called a form parameter) in the function. The parameter can be one or more. When a function is called, the input parameter is an actual parameter, which determines the true value of the parameter. Easy to understand: Form and real parameters are the content.

This is a function with two parameters, param1 and param2:

Function testFun (param1, param2 ){
Console. log (param1, param2 );
}

Next we call testFun:

TestFun ("Hello", "World ");

We passed two parameters: "Hello" and "World ". In the function, param1 is equal to "Hello", and param2 is equal to "World ". Note that the testFun function can be called multiple times. The parameter passed during each call determines the actual value of the parameter.

1.2. Variable Scope1.2.1. GLOBAL SCOPE

In JavaScript,ScopeThe scope of the variables involved. Variables defined outside the function haveGlobalScope. This means that variables with a global scope can be called anywhere in the code.

Variables that are not defined using the var keyword are automatically created in the global scope to form a global variable. When a variable is inadvertently defined elsewhere in the Code, the variable name is the same as the global variable, unexpected consequences will occur. Therefore, you should always use the var keyword to declare your variables.

1.2.2. Local Scope

Variables declared in a function and the parameters of the function are all local variables, which means they are only visible in the function.

This is the best example of declaring the local variable loc in the function myTest:

Function myTest (){
Var loc = "foo ";
Console. log (loc );
}
MyTest (); // "foo"
Console. log (loc); // "undefined"

In addition to functions, loc is undefined.

A program may have the same nameLocalVariable andGlobalVariable. In this case, local variables take precedence over global variables.

The following is an example:

Var someVar = "Hat ";
Function myFun (){
Var someVar = "Head ";
Return someVar;
}

The function myFun returns "Head" because the local variable has a higher priority.

1.3. Function return value

We can use the FunctionParametersYou can also use the return statement to transfer data from a function.

For example

Function plusThree (num ){
Return num + 3;
}
Var answer = plusThree (5); // 8

PlusThree hasParametersAnd return (returns) a value equal to num + 3.

2. Comparison operator

In JavaScript, there are manyComparison operations. All these operators return a value of true or false.

2.1. Equality OPERATOR: =

The most basic operator is the equal OPERATOR: =. The equal operator compares two values. If they are equal, true is returned. If they are unequal, false is returned. It is worth noting that the equal operator is different from the value assignment operator (=). The value assignment operator assigns the value on the right of the equal sign to the variable on the left.

Function equalityTest (myVal ){
If (myVal = 10 ){
Return "Equal ";
}
Return "Not Equal ";
}

If myVal is Equal to 10, the Equal operator returns true, so the code in braces is executed, and the function returns "Equal ". Otherwise, the function returns "Not Equal ".

In JavaScript, to make the values of two different data types (such as numbers and strings) comparable, it must convert one type to another. However, once this is done, it can be compared as follows:

1 = 1 // true
1 = 2 // false
1 = '1' // true
"3" = 3 // true

2.2. Equality OPERATOR: ===

Equality (=) is an operator relative to the equal operator (=. Unlike the equality operator, the equality operator is strict. It compares the values and Data Types of elements at the same time.

For example

3 = 3 // true
3 = '3' // false

3 is a numeric type, while '3' is a character type, SO 3 is not completely equal to '3 '.

2.3. Unequal operators: ! =

Unequal operator (! =) Is opposite to the equal operator. This means that in the unequal operator, if "not true" and false is returned, true is returned in the equal operator, and vice versa. Similar to equal operators, unequal operators also convert the Data Type of values during comparison.

For example

1! = 2 // true
1! = "1" // false
1! = '1' // false
1! = True // false
0! = False // false

2.4. Incomplete equality Operators : ! =

Incomplete equality operator (! =) Is opposite to the full equality operator. This means that "incomplete" and false are returned. true is returned using the "full" operation, and vice versa. Full-level operators do not convert the Data Type of values.

For example

3! = 3 // false
3! = '3' // true
4! = 3 // true

2.5. Greater than Operator : >

Use the greater than operator (>) to compare two numbers. True is returned if the number on the left is greater than the number on the right. Otherwise, it returns false.

Like equal operators, Data Types of values larger than operators are converted during comparison.

For example

5> 3 // true
7> '3' // true
2> 3 // false
'1'> 9 // false

2.6. Greater than or equal Operator : > =

Use the greater than or equal operator (> =) to compare the two numbers. If the number greater than or equal to the number on the left is greater than or equal to the number on the right, true is returned. Otherwise, it returns false.

Similar to equal operators, an equal or greater operator converts the Data Type of values during comparison.

For example

6> = 6 // true
7> = '3' // true
2> = 3 // false
'7'> = 9 // false

2.7. Less Operator : <

UseLessThe operator (<) compares the two numbers. If the number smaller than the number on the left of the operator is smaller than the number on the right, true is returned. Otherwise, false is returned. Similar to equal operators,LessThe operator converts the Data Type of the value during comparison.

For example

2 <5 // true
'3' <7 // true
5 <5 // false
3 <2 // false
'8' <4 // false

2.8. Less than or equal Operator : <=

Use the <=) operator to compare the two numbers. If the number on the left is less than or equal to the number on the right, true is returned. If the number on the left is greater than or equal to the number on the right, false is returned. Equal to the equal operator type. If less than or equal to the operator, the data type is converted.

For example

4 <= 5 // true
'7' <= 7 // true
5 <= 5 // true
3 <= 2 // false
'8' <= 4 // false

2.9. Logic and Operator : &&

Sometimes you need to perform multiple operations in one judgment. If and only when both the left and right sides of the operator are true,Logic andTrue is returned only when the & operator is used.

The same effect can be achieved through the nested if statement:

If (num> 5 ){
If (num <10 ){
Return "Yes ";
}
}
Return "No ";

"Yes" is returned only when the value of num is between 6 and 9 (including 6 and 9 ". The same logic can be written as follows:

If (num> 5 & num <10 ){
Return "Yes ";
}
Return "No ";

2.10. Logic or Operator : |

If any operand is true,Logic orReturns true if the operator is (|. Otherwise, false is returned.

For example:

If (num> 10 ){
Return "No ";
}
If (num <5 ){
Return "No ";
}
Return "Yes ";

The function returns "Yes" Only when num is greater than or equal to 5 or less than or equal to 10 ". The same logic can be abbreviated:

If (num> 10 | num <5 ){
Return "No ";
}
Return "Yes ";

Finally, let's talk about the important things three times. If you think they are good, you should recommend them or comment on them. If you think they are good, you may wish to continue to improve your suggestions, your support is my greatest encouragement.

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.