Expressions and Operators Flashcards
List the operators used in JavaScript
- Assignment operators
- Comparison operators
- Arithmetic operators
- Bitwise operators
- Logical operators
- String operators
- Conditional (ternary) operator
- Comma operator
- Unary operators
- Relational operators
What is an assignment operator?
An assignment operator assigns a value to its left operand based on the value of its right operand
What are compound assignment operators?
Compound assignment operators are shorthand for assigning multiple operations
Some examples of compound assignment operators are:
+= - this will add the value of the right operand to the value of the left operand and assign it to the left operand
- = - in the same way as mentioned above this compound assignment subtracts and assigns
- = - this compound assignment multiplies and assigns
/= - this compound assignment divides and assigns
What is a comparison operator?
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
Explain type conversion in JavaScript
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
How do you avoid errors in JavaScript type conversion?
By using the strict equality operators (=== and !==) to perform strict equality and inequality comparisons
These operators do not attempt to convert the operands to compatible types before checking equality
List the JavaScript comparison operators
- Equal ( == )
- Not equal ( != )
- Strict equal ( === )
- Strict not equal ( !== )
- Greater than ( > )
- Greater than or equal ( >= )
- Less than ( < )
- Less than or equal ( <= )
What is an arithmetic operator?
An arithmetic operator takes numerical values (either literals or variables) as their operands and returns a single numerical value
List the JavaScript standard arithmetic operators
- Addition ( + )
- Subtraction ( - )
- Multiplication ( * )
- Division ( / )
List the JavaScript non-standard arithmetic operators
- Remainder ( % )
- Increment ( ++ )
- Decrement ( – )
- Unary negation ( - )
- Unary plus ( + )
- Exponentiation operator ( ** )
What is a bitwise operator?
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
List the JavaScript bitwise operators
- Bitwise AND
- Bitwise OR
- Bitwise XOR
- Bitwise NOT
- Left shift
- Sign-propagating right shift
- Zero-fill right shift
What is a logical operator?
Logical operators are typically used with Boolean (logical) values; when they are, they return a Boolean value
However, the && and || 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
List the JavaScript logical operators
- Logical AND ( && )
- Logical OR ( || )
- Logical NOT ( ! )
In which cases does the logical AND operator return true
Only when both operands being evaluated result in true
In which cases does the logical AND operator return false
If both operands result in false
Or if either operand results in false
In which cases does the logical OR operator return true
If both operands result in true
Or if one of the operands result in true
In which cases does the logical OR operator return false
Only when both operands being evaluated result in false
Describe the use of the logical NOT operator
This operator takes a single Boolean value and inverts it
If a result evaluated to false before adding NOT ( ! ) it would return true
If the result evaluated to true before adding NOT ( ! ) it would return false
Describe “short-circuit” evaluation
As logical expressions are evaluated left to right, they are tested for possible “short-circuit” evaluation using the following rules:
- false && anything is short-circuit evaluated to false
- true || anything is short-circuit evaluated to true
The “anything” part of the above expressions is not evaluated
Describe concatenation of String operators in JavaScript
Concatenation is when the plus sign ( + ) is used to join two string values together
This returns a string which is the union of the two operand strings
Describe the conditional (ternary) operator in JavaScript
The conditional operator is the only JavaScript operator that takes three operands
The operator can have one of two values based on a condition (Syntax: condition ? val1 : val2)
If “condition” is true, the operator has the value of “val1” otherwise it has the value of “val2”
Describe the comma operator
The comma operator ( , ) evaluates both of its operands and returns the value of the last operand
This operator is primarily used inside a for loop, to allow multiple variables to be updated each time through the loop
Describe unary operators
A unary operation is an operation with only one operand