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 The expression As the examples above also
illustrate, all complex expressions are joined by operators, such as These operators join operands either formed by higher-precedence operators or one of the basic expressions. 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 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, A unary operator requires a single operand, either before or after the operator: operator operand operand operator For
example, Assignment operators An assignment operator assigns a value to its left operand based on the value of its right operand. The simple assignment operator is equal ( There are also compound assignment operators that are shorthand for the operations listed in the following table:
Assigning to propertiesIf 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 strict mode, 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 ( DestructuringFor 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.
Evaluation and nestingIn general, assignments are used within a variable declaration (i.e., with
However, like other expressions, assignment expressions like
Chaining assignments or nesting assignments in other expressions can result in surprising behavior. For this reason, some JavaScript style guides discourage chaining or nesting assignments). 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 In the case of logical assignments, 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 For example, assume that the following functions
Consider these three examples:
Evaluation example 1
Evaluation example 2
Evaluation example 3
Avoid assignment chainsChaining assignments or nesting assignments in other expressions can result in surprising behavior. For this reason, chaining assignments in the same statement is discouraged). In particular,
putting a variable chain in a This statement seemingly declares the variables Comparison operators 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
Comparison operators
Note: Arithmetic operators 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 (
In addition to the standard arithmetic operations (
Bitwise operatorsA 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.
Bitwise logical operatorsConceptually, 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:
Note 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). Bitwise shift operatorsThe 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 The shift operators are listed in the following table. Bitwise shift operators
Logical operators Logical operators are typically used with Boolean (logical) values; when they are, they return a Boolean value. However, the
Examples of expressions that can be converted to The following code shows examples of the
The following code shows examples of the || (logical OR) operator.
The following code shows examples of the ! (logical NOT) operator.
Short-circuit evaluationAs 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 ( BigInt operatorsMost operators that can be used between numbers can be used between
One exception is unsigned right shift (
BigInts and numbers are not mutually replaceable — you cannot mix them in calculations.
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.
You can compare BigInts with numbers.
String operatorsIn 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,
The shorthand assignment operator For example,
Conditional (ternary) operator
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: If For example,
This statement assigns the value "adult" to the variable Comma operator The
comma operator ( For
example, if
Unary operatorsA unary operation is an operation with only one operand. delete The
where If the
Deleting array elements Since arrays are just objects, it's technically possible to
typeofThe The Suppose you define the following variables:
The
For the keywords
For a number or string, the
For property values, the
For methods and functions, the
For predefined objects, the
voidThe
The Relational operatorsA relational operator compares its operands and returns a Boolean value based on whether the comparison is true. in The
where The following examples show some uses of the
instanceof The
where Use For example, the following code uses
Basic expressionsAll operators eventually operate on one or more basic expressions. These basic expressions include identifiers and literals, 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. this Use the
Suppose a function called
You could call
Grouping operator The grouping operator
newYou can use the
super The
Is the NOT operator is a unary operator which must be used in a compound expression?The not operator is a unary operator which must be used in a compound expression. Short -circuit evaluation is only performed with the not operator. Expressions that are tested by the if statement are called Boolean expressions. Decision structures are also known as selection structures.
When using the ____ operator one or both Subexpressions must be true for the compound expression to be true?With the Boolean OR operator, you can connect two Boolean expressions into one compound expression. At least one subexpressions must be true for the compound expression to be considered true, and it doesn't matter which. If both subexpressions are false, then the expression is false.
Does Python allow you to compare strings but not case sensitive?Python strings equality can be checked using == operator or __eq__() function. Python strings are case sensitive, so these equality check methods are also case sensitive.
Which of the following is the correct If clause to determine whether choice is anything other than 10 in Python )?Review of Python Chapter 2. |