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