Expressions and Operators Flashcards

1
Q

List the operators used in JavaScript

A
  1. Assignment operators
  2. Comparison operators
  3. Arithmetic operators
  4. Bitwise operators
  5. Logical operators
  6. String operators
  7. Conditional (ternary) operator
  8. Comma operator
  9. Unary operators
  10. Relational operators
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is an assignment operator?

A

An assignment operator assigns a value to its left operand based on the value of its right operand

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are compound assignment operators?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is a comparison operator?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Explain type conversion in JavaScript

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How do you avoid errors in JavaScript type conversion?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

List the JavaScript comparison operators

A
  1. Equal ( == )
  2. Not equal ( != )
  3. Strict equal ( === )
  4. Strict not equal ( !== )
  5. Greater than ( > )
  6. Greater than or equal ( >= )
  7. Less than ( < )
  8. Less than or equal ( <= )
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is an arithmetic operator?

A

An arithmetic operator takes numerical values (either literals or variables) as their operands and returns a single numerical value

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

List the JavaScript standard arithmetic operators

A
  1. Addition ( + )
  2. Subtraction ( - )
  3. Multiplication ( * )
  4. Division ( / )
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

List the JavaScript non-standard arithmetic operators

A
  1. Remainder ( % )
  2. Increment ( ++ )
  3. Decrement ( – )
  4. Unary negation ( - )
  5. Unary plus ( + )
  6. Exponentiation operator ( ** )
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is a bitwise operator?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

List the JavaScript bitwise operators

A
  1. Bitwise AND
  2. Bitwise OR
  3. Bitwise XOR
  4. Bitwise NOT
  5. Left shift
  6. Sign-propagating right shift
  7. Zero-fill right shift
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is a logical operator?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

List the JavaScript logical operators

A
  1. Logical AND ( && )
  2. Logical OR ( || )
  3. Logical NOT ( ! )
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

In which cases does the logical AND operator return true

A

Only when both operands being evaluated result in true

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

In which cases does the logical AND operator return false

A

If both operands result in false

Or if either operand results in false

17
Q

In which cases does the logical OR operator return true

A

If both operands result in true

Or if one of the operands result in true

18
Q

In which cases does the logical OR operator return false

A

Only when both operands being evaluated result in false

19
Q

Describe the use of the logical NOT operator

A

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

20
Q

Describe “short-circuit” evaluation

A

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

21
Q

Describe concatenation of String operators in JavaScript

A

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

22
Q

Describe the conditional (ternary) operator in JavaScript

A

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”

23
Q

Describe the comma operator

A

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

24
Q

Describe unary operators

A

A unary operation is an operation with only one operand

25
Q

List unary operators

A

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

26
Q

Describe relational operators

A

A relational operator compares its operands and returns a Boolean value based on whether the comparison is true

27
Q

List relational operators

A

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

28
Q

Describe truthy and falsy values in JavaScript

A

Due to type coercion (or conversion), every value in JavaScript can be treated as if it were true or false

29
Q

List JavaScript falsy values

A
  1. The traditional Boolean false
  2. The number zero
  3. An empty string
  4. NaN or not a number
  5. A variable that has not be assigned a value
30
Q

List JavaScript truthy values

A
  1. The traditional Boolean true
  2. Numbers other than zero
  3. Strings with content
  4. Number calculations
  5. True written as a string
  6. Zero written as a string
  7. False written as a string
31
Q

What is an expression in JavaScript

A

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

32
Q

List the JavaScript expression categories

A
  1. Arithmetic - evaluates to a number
  2. String - evaluates to a character string
  3. Logical - evaluates to true or false
  4. Primary expressions - basic keywords and general expressions in JavaScript
  5. Left-hand-side expressions - left values are the destination of an assignment
33
Q

Describe primary expressions in JavaScript

A

this - a JavaScript keyword used to refer to the current object

( ) - the grouping operator used to control the precedence of evaluation in expressions

34
Q

Describe left-hand-side expressions in JavaScript

A

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