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
List unary operators
delete - this operator deletes an object’s property
typeof - this operator returns a string indicating the type of the unevaluated operand (ie string, number, etc)
void - this operator specifies an expression to be evaluated without returning a value
Describe relational operators
A relational operator compares its operands and returns a Boolean value based on whether the comparison is true
List relational operators
in - this operator returns true if the specified property is in the specified object
instanceof - this operator returns true if the specified object is of the specified object type
Describe truthy and falsy values in JavaScript
Due to type coercion (or conversion), every value in JavaScript can be treated as if it were true or false
List JavaScript falsy values
- The traditional Boolean false
- The number zero
- An empty string
- NaN or not a number
- A variable that has not be assigned a value
List JavaScript truthy values
- The traditional Boolean true
- Numbers other than zero
- Strings with content
- Number calculations
- True written as a string
- Zero written as a string
- False written as a string
What is an expression in JavaScript
An expression is any valid unit of code that resolves to a value
There are two types of expressions: those that assign value to a variable, and those that in some sense evaluate and therefore resolve to a value
List the JavaScript expression categories
- Arithmetic - evaluates to a number
- String - evaluates to a character string
- Logical - evaluates to true or false
- Primary expressions - basic keywords and general expressions in JavaScript
- Left-hand-side expressions - left values are the destination of an assignment
Describe primary expressions in JavaScript
this - a JavaScript keyword used to refer to the current object
( ) - the grouping operator used to control the precedence of evaluation in expressions
Describe left-hand-side expressions in JavaScript
new - use the new operator to create an instance of a user-defined object type or of one of the built-in object types
super - use the super keyword to call functions on an object’s parent