Which of the following operators reverses the meaning of a test statement?
Fortran has five LOGICAL operators that can only be used with expressions whose results are logical values (i.e., .TRUE. or .FALSE.). All LOGICAL operators have priorities lower than arithmetic and relational operators. Therefore, if an expression involving arithmetic, relational and logical operators, the arithmetic operators are evaluated first, followed by the relational operators, followed by the logical operators. Show These five logical operators are
Truth TablesThe evaluation of logical expressions is determined by truth tables. Let us start with the .NOT. operator..NOT.OperandResult.TRUE..FALSE..FALSE..TRUE.Note that .NOT. is a unary operator. Therefore, .NOT. a yields .TRUE. (resp., .FALSE.) if the value of LOGICAL variable a is .FALSE. (resp., .TRUE.). The following is the truth table of .AND.:.AND..TRUE..FALSE.TRUE..TRUE..FALSE..FALSE..FALSE..FALSE. Therefore, the result of logical expression a .NEQV. b is .TRUE. if and only if both operands a and b do not have the same value. As mentioned in relational expressions, relational operators can only compare arithmetic values and cannot be used to compare logical values. To compare if two logical values are not equal, use .NEQV. Note that .NEQV is the opposite of .EQV.. Hence, to test if logical variables x and y have different values, one can use .NOT. (x .EQV. y). Here, if x and y have the same value, x .EQV. y is .TRUE. and .NOT. (x .EQV. y) is .FALSE. On the other hand, if x and y have different values, x .EQV. y is .FALSE. and .NOT. (x .EQV. y) is .TRUE. This chapter describes JavaScript's expressions and operators, including assignment, comparison, arithmetic, bitwise, logical, string, ternary and more. At a high level, an expression is a valid unit of code that resolves to a value. There are two types of expressions: those that have side effects (such as assigning values) and those that purely evaluate. The expression 8 is an example of the first type. This expression uses the 9 operator to assign the value seven to the variable 0. The expression itself evaluates to 1.The expression 2 is an example of the second type. This expression uses the 3 operator to add 4 and 5 together and produces a value, 1. However, if it's not eventually part of a bigger construct (for example, a like 7), its result will be immediately discarded — this is usually a programmer mistake because the evaluation doesn't produce any effects.As the examples above also illustrate, all complex expressions are joined by operators, such as 9 and 3. In this section, we will introduce the following operators:These operators join operands either formed by higher-precedence operators or one of the . A complete and detailed list of operators and expressions is also available in the reference. The precedence of operators determines the order they are applied when evaluating an expression. For example:
Despite 0 and 3 coming in different orders, both expressions would result in 1 because 0 has precedence over 3, so the 0-joined expression will always be evaluated first. You can override operator precedence by using parentheses (which creates a — the basic expression). To see a complete table of operator precedence as well as various caveats, see the page.JavaScript has both binary and unary operators, and one special ternary operator, the conditional operator. A binary operator requires two operands, one before the operator and one after the operator: operand1 operator operand2 For example, 2 or 7. This form is called an infix binary operator, because the operator is placed between two operands. All binary operators in JavaScript are infix.A unary operator requires a single operand, either before or after the operator: operator operand operand operator For example, 8 or 9. The 0 form is called a prefix unary operator, and the 1 form is called a postfix unary operator. 2 and 3 are the only postfix operators in JavaScript — all other operators, like 4, 5, etc. are prefix.An assignment operator assigns a value to its left operand based on the value of its right operand. The simple assignment operator is equal ( 9), which assigns the value of its right operand to its left operand. That is, 7 is an assignment expression that assigns the value of 8 to 0.There are also compound assignment operators that are shorthand for the operations listed in the following table: NameShorthand operatorMeaningAssignment 7 7Addition assignment 2 3Subtraction assignment 4 5Multiplication assignment 6 7Division assignment 8 9Remainder assignment 0 1Exponentiation assignment 2 3Left shift assignment 4 5Right shift assignment 6 7Unsigned right shift assignment 8 9Bitwise AND assignmentoperand1 operator operand200 operand1 operator operand201Bitwise XOR assignment operand1 operator operand202 operand1 operator operand203Bitwise OR assignment operand1 operator operand204 operand1 operator operand205Logical AND assignment operand1 operator operand206 operand1 operator operand207Logical OR assignment operand1 operator operand208 operand1 operator operand209Nullish coalescing assignment operand1 operator operand210 operand1 operator operand211 If an expression evaluates to an object, then the left-hand side of an assignment expression may make assignments to properties of that expression. For example:
For more information about objects, read Working with Objects. If an expression does not evaluate to an object, then assignments to properties of that expression do not assign:
In , the code above throws, because one cannot assign properties to primitives. It is an error to assign values to unmodifiable properties or to properties of an expression without properties ( operand1 operator operand212 or operand1 operator operand213). For more complex assignments, the destructuring assignment syntax is a JavaScript expression that makes it possible to extract data from arrays or objects using a syntax that mirrors the construction of array and object literals.
In general, assignments are used within a variable declaration (i.e., with operand1 operator operand214, operand1 operator operand215, or operand1 operator operand216) or as standalone statements).
However, like other expressions, assignment expressions like 7 evaluate into a result value. Although this result value is usually not used, it can then be used by another expression.Chaining assignments or nesting assignments in other expressions can result in surprising behavior. For this reason, some JavaScript style guides ). Nevertheless, assignment chaining and nesting may occur sometimes, so it is important to be able to understand how they work. By chaining or nesting an assignment expression, its result can itself be assigned to another variable. It can be logged, it can be put inside an array literal or function call, and so on.
The evaluation result matches the expression to the right of the 9 sign in the "Meaning" column of the table above. That means that 7 evaluates into whatever 8's result is, 2 evaluates into the resulting sum operand1 operator operand222, 2 evaluates into the resulting power operand1 operator operand224, and so on. In the case of logical assignments, operand1 operator operand206, operand1 operator operand208, and operand1 operator operand210, the return value is that of the logical operation without the assignment, so operand1 operator operand228, operand1 operator operand229, and operand1 operator operand230, respectively. When chaining these expressions without parentheses or other grouping operators like array literals, the assignment expressions are grouped right to left (they are right-associative), but they are evaluated left to right. Note that, for all assignment operators other than 9 itself, the resulting values are always based on the operands' values before the operation.For example, assume that the following functions operand1 operator operand232 and operand1 operator operand233 and the variables 0 and operand1 operator operand235 have been declared:
Consider these three examples:
Evaluation example 1operand1 operator operand236 is equivalent to operand1 operator operand237, because the assignment operator 9 is right-associative. However, it evaluates from left to right:
Evaluation example 2operand1 operator operand261 also evaluates from left to right:
Evaluation example 3operand1 operator operand293 also evaluates from left to right. (This example assumes that 0 is already assigned to some object. For more information about objects, read Working with Objects.)
Chaining assignments or nesting assignments in other expressions can result in surprising behavior. For this reason, ). In particular, putting a variable chain in a operand1 operator operand214, operand1 operator operand215, or operand1 operator operand216 statement often does not work. Only the outermost/leftmost variable would get declared; other variables within the assignment chain are not declared by the operand1 operator operand214/ operand1 operator operand215/ operand1 operator operand216 statement. For example: operand1 operator operand20 This statement seemingly declares the variables 0, operand1 operator operand235, and operator operand operand operator21. However, it only actually declares the variable operator operand operand operator21. operand1 operator operand235 and 0 are either invalid references to nonexistent variables (in strict mode) or, worse, would implicitly create global variables for 0 and operand1 operator operand235 in sloppy mode. A comparison operator compares its operands and returns a logical value based on whether the comparison is true. The operands can be numerical, string, logical, or object values. Strings are compared based on standard lexicographical ordering, using Unicode values. In most cases, if the two operands are not of the same type, JavaScript attempts to convert them to an appropriate type for the comparison. This behavior generally results in comparing the operands numerically. The sole exceptions to type conversion within comparisons involve the operator operand operand operator27 and operator operand operand operator28 operators, which perform strict equality and inequality comparisons. These operators do not attempt to convert the operands to compatible types before checking equality. The following table describes the comparison operators in terms of this sample code: operand1 operator operand21Comparison operatorsOperatorDescriptionExamples returning trueEqual ( operator operand operand operator29)Returns operator operand operand operator30 if the operands are equal. operator operand operand operator31 operator operand operand operator32 operator operand operand operator33Not equal ( operator operand operand operator34)Returns operator operand operand operator30 if the operands are not equal. operator operand operand operator36Strict equal ( operator operand operand operator27)Returns operator operand operand operator30 if the operands are equal and of the same type. See also operator operand operand operator39 and sameness in JS. operator operand operand operator40Strict not equal ( operator operand operand operator28)Returns operator operand operand operator30 if the operands are of the same type but not equal, or are of different type. operator operand operand operator43Greater than ( operator operand operand operator44)Returns operator operand operand operator30 if the left operand is greater than the right operand. operator operand operand operator46Greater than or equal ( operator operand operand operator47)Returns operator operand operand operator30 if the left operand is greater than or equal to the right operand. operator operand operand operator49Less than ( operator operand operand operator50)Returns operator operand operand operator30 if the left operand is less than the right operand. operator operand operand operator52Less than or equal ( operator operand operand operator53)Returns operator operand operand operator30 if the left operand is less than or equal to the right operand. operator operand operand operator55 Note: operator operand operand operator56 is not a comparison operator but rather is the notation for Arrow functions. An arithmetic operator takes numerical values (either literals or variables) as their operands and returns a single numerical value. The standard arithmetic operators are addition ( 3), subtraction (operator operand operand operator58), multiplication ( 0), and division (operator operand operand operator60). These operators work as they do in most other programming languages when used with floating point numbers (in particular, note that division by zero produces operator operand operand operator61). For example: operand1 operator operand22 In addition to the standard arithmetic operations ( 3, operator operand operand operator58, 0, operator operand operand operator60), JavaScript provides the arithmetic operators listed in the following table:Arithmetic operatorsOperatorDescriptionExampleRemainder ( operator operand operand operator66)Binary operator. Returns the integer remainder of dividing the two operands.12 % 5 returns 2.Increment ( 2)Unary operator. Adds one to its operand. If used as a prefix operator ( 9), returns the value of its operand after adding one; if used as a postfix operator ( 8), returns the value of its operand before adding one.If 0 is 3, then 9 sets 0 to 4 and returns 4, whereas 8 returns 3 and, only then, sets 0 to 4.Decrement ( 3)Unary operator. Subtracts one from its operand. The return value is analogous to that for the increment operator.If 0 is 3, then operator operand operand operator77 sets 0 to 2 and returns 2, whereas operator operand operand operator79 returns 3 and, only then, sets 0 to 2.Unary negation (operator operand operand operator58)Unary operator. Returns the negation of its operand.If 0 is 3, then operator operand operand operator83 returns -3.Unary plus ( 3)Unary operator. Attempts to , if it is not already.operator operand operand operator85 returns 4.operator operand operand operator87 returns operator operand operand operator88.Exponentiation operator ( operator operand operand operator89)Calculates the operator operand operand operator90 to the operator operand operand operator91 power, that is, operator operand operand operator92 operator operand operand operator93 returns operator operand operand operator94. operator operand operand operator95 returns operator operand operand operator96. A bitwise operator treats their operands as a set of 32 bits (zeros and ones), rather than as decimal, hexadecimal, or octal numbers. For example, the decimal number nine has a binary representation of 1001. Bitwise operators perform their operations on such binary representations, but they return standard JavaScript numerical values. The following table summarizes JavaScript's bitwise operators. OperatorUsageDescriptionBitwise ANDoperator operand operand operator97Returns a one in each bit position for which the corresponding bits of both operands are ones.Bitwise OR operator operand operand operator98Returns a zero in each bit position for which the corresponding bits of both operands are zeros.Bitwise XOR operator operand operand operator99Returns a zero in each bit position for which the corresponding bits are the same. [Returns a one in each bit position for which the corresponding bits are different.]Bitwise NOT 00Inverts the bits of its operand.Left shift 01Shifts 02 in binary representation 03 bits to the left, shifting in zeros from the right.Sign-propagating right shift 04Shifts 02 in binary representation 03 bits to the right, discarding bits shifted off.Zero-fill right shift 07Shifts 02 in binary representation 03 bits to the right, discarding bits shifted off, and shifting in zeros from the left.Conceptually, the bitwise logical operators work as follows:
For example, the binary representation of nine is 1001, and the binary representation of fifteen is 1111. So, when the bitwise operators are applied to these values, the results are as follows: ExpressionResultBinary Description 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24Note that all 32 bits are inverted using the Bitwise NOT operator, and that values with the most significant (left-most) bit set to 1 represent negative numbers (two's-complement representation). 25 evaluates to the same value that 26 evaluates to.The bitwise shift operators take two operands: the first is a quantity to be shifted, and the second specifies the number of bit positions by which the first operand is to be shifted. The direction of the shift operation is controlled by the operator used. Shift operators convert their operands to thirty-two-bit integers and return a result of either type 27 or 28: specifically, if the type of the left operand is 28, they return 28; otherwise, they return 27.The shift operators are listed in the following table. Bitwise shift operatorsOperatorDescriptionExampleLeft shift( 32)This operator shifts the first operand the specified number of bits to the left. Excess bits shifted off to the left are discarded. Zero bits are shifted in from the right. 33 yields 36, because 1001 shifted 2 bits to the left becomes 100100, which is 36.Sign-propagating right shift ( 34)This operator shifts the first operand the specified number of bits to the right. Excess bits shifted off to the right are discarded. Copies of the leftmost bit are shifted in from the left. 35 yields 2, because 1001 shifted 2 bits to the right becomes 10, which is 2. Likewise, 36 yields -3, because the sign is preserved.Zero-fill right shift ( 37)This operator shifts the first operand the specified number of bits to the right. Excess bits shifted off to the right are discarded. Zero bits are shifted in from the left. 38 yields 4, because 10011 shifted 2 bits to the right becomes 100, which is 4. For non-negative numbers, zero-fill right shift and sign-propagating right shift yield the same result.Logical operators are typically used with Boolean (logical) values; when they are, they return a Boolean value. However, the 39 and 40 operators actually return the value of one of the specified operands, so if these operators are used with non-Boolean values, they may return a non-Boolean value. The logical operators are described in the following table.Logical operatorsOperatorUsageDescriptionLogical AND ( 39) 42Returns 43 if it can be converted to 44; otherwise, returns 45. Thus, when used with Boolean values, 39 returns operator operand operand operator30 if both operands are true; otherwise, returns 44.Logical OR ( 40) 50Returns 43 if it can be converted to operator operand operand operator30; otherwise, returns 45. Thus, when used with Boolean values, 40 returns operator operand operand operator30 if either operand is true; if both are false, returns 44.Logical NOT ( 4) 58Returns 44 if its single operand that can be converted to operator operand operand operator30; otherwise, returns operator operand operand operator30. Examples of expressions that can be converted to 44 are those that evaluate to null, 0, NaN, the empty string (""), or undefined.The following code shows examples of the 39 (logical AND) operator.operand1 operator operand24 The following code shows examples of the || (logical OR) operator. operand1 operator operand25 The following code shows examples of the ! (logical NOT) operator. operand1 operator operand26 As logical expressions are evaluated left to right, they are tested for possible "short-circuit" evaluation using the following rules:
The rules of logic guarantee that these evaluations are always correct. Note that the anything part of the above expressions is not evaluated, so any side effects of doing so do not take effect. Note that for the second case, in modern code you can use the Nullish coalescing operator ( 66) that works like 40, but it only returns the second expression, when the first one is "nullish", i.e. operand1 operator operand212 or operand1 operator operand213. It is thus the better alternative to provide defaults, when values like 70 or 71 are valid values for the first expression, too.Most operators that can be used between numbers can be used between 28 values as well.operand1 operator operand27 One exception is unsigned right shift ( 37), which is not defined for BigInt values. This is because a BigInt does not have a fixed width, so technically it does not have a "highest bit".operand1 operator operand28 BigInts and numbers are not mutually replaceable — you cannot mix them in calculations. operand1 operator operand29 This is because BigInt is neither a subset nor a superset of numbers. BigInts have higher precision than numbers when representing large integers, but cannot represent decimals, so implicit conversion on either side might lose precision. Use explicit conversion to signal whether you wish the operation to be a number operation or a BigInt one. operator operand operand operator0 You can compare BigInts with numbers. operator operand operand operator1 In addition to the comparison operators, which can be used on string values, the concatenation operator (+) concatenates two string values together, returning another string that is the union of the two operand strings. For example, operator operand operand operator2 The shorthand assignment operator 74 can also be used to concatenate strings.For example, operator operand operand operator3 The conditional operator is the only JavaScript operator that takes three operands. The operator can have one of two values based on a condition. The syntax is: operator operand operand operator4 If 75 is true, the operator has the value of 76. Otherwise it has the value of 77. You can use the conditional operator anywhere you would use a standard operator.For example, operator operand operand operator5 This statement assigns the value "adult" to the variable 78 if 79 is eighteen or more. Otherwise, it assigns the value "minor" to 78.The comma operator ( 81) evaluates both of its operands and returns the value of the last operand. This operator is primarily used inside a 82 loop, to allow multiple variables to be updated each time through the loop. It is regarded bad style to use it elsewhere, when it is not necessary. Often two separate statements can and should be used instead.For example, if 02 is a 2-dimensional array with 10 elements on a side, the following code uses the comma operator to update two variables at once. The code prints the values of the diagonal elements in the array:operator operand operand operator6 A unary operation is an operation with only one operand. The 84 operator deletes an object's property. The syntax is:operator operand operand operator7 where 85 is the name of an object, 86 is an existing property, and 87 is a string or symbol referring to an existing property.If the 84 operator succeeds, it removes the property from the object. Trying to access it afterwards will yield operand1 operator operand213. The 84 operator returns operator operand operand operator30 if the operation is possible; it returns 44 if the operation is not possible.operator operand operand operator8 Deleting array elementsSince arrays are just objects, it's technically possible to 84 elements from them. This is however regarded as a bad practice, try to avoid it. When you delete an array property, the array length is not affected and other elements are not re-indexed. To achieve that behavior, it is much better to just overwrite the element with the value operand1 operator operand213. To actually manipulate the array, use the various array methods such as 95.The 5 operator is used in either of the following ways:operator operand operand operator9 The 5 operator returns a string indicating the type of the unevaluated operand. 98 is the string, variable, keyword, or object for which the type is to be returned. The parentheses are optional.Suppose you define the following variables: 0The 5 operator returns the following results for these variables: 1For the keywords operator operand operand operator30 and operand1 operator operand212, the 5 operator returns the following results: 2For a number or string, the 5 operator returns the following results: 3For property values, the 5 operator returns the type of value the property contains: 4For methods and functions, the 5 operator returns results as follows: 5For predefined objects, the 5 operator returns results as follows: 6The 07 operator is used in either of the following ways: 7The 07 operator specifies an expression to be evaluated without returning a value. 09 is a JavaScript expression to evaluate. The parentheses surrounding the expression are optional, but it is good style to use them.A relational operator compares its operands and returns a Boolean value based on whether the comparison is true. The 10 operator returns operator operand operand operator30 if the specified property is in the specified object. The syntax is: 8where 12 is a string, numeric, or symbol expression representing a property name or array index, and 13 is the name of an object.The following examples show some uses of the 10 operator. 9The 15 operator returns operator operand operand operator30 if the specified object is of the specified object type. The syntax is: 0where 13 is the name of the object to compare to 18, and 18 is an object type, such as 20 or 21.Use 15 when you need to confirm the type of an object at runtime. For example, when catching exceptions, you can branch to different exception-handling code depending on the type of exception thrown.For example, the following code uses 15 to determine whether 24 is a 20 object. Because 24 is a 20 object, the statements in the 28 statement execute. 1All operators eventually operate on one or more basic expressions. These basic expressions include and , but there are a few other kinds as well. They are briefly introduced below, and their semantics are described in detail in their respective reference sections. Use the 29 keyword to refer to the current object. In general, 29 refers to the calling object in a method. Use 29 either with the dot or the bracket notation: 2Suppose a function called 32 validates an object's 33 property, given the object and the high and low values: 3You could call 32 in each form element's 35 event handler, using 29 to pass it to the form element, as in the following example: 4The grouping operator 37 controls the precedence of evaluation in expressions. For example, you can override multiplication and division first, then addition and subtraction to evaluate addition first. 5You can use the 38 operator to create an instance of a user-defined object type or of one of the built-in object types. Use 38 as follows: 6The 40 keyword is used to call functions on an object's parent. It is useful with classes to call the parent constructor, for example. |