JS Operators Flashcards

1
Q

new

A

The new operator lets developers create an instance of a user-defined object type or of one of the built-in object types that has a constructor function.

const lambo = new Car(‘lamborghini’, ‘huracan’, 50000);

const array = new Array(30);

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

*
/

A

the addition operator (+):
sums numbers and concatenates strings

The subtraction operator (-) subtracts the two operands, producing their difference.

The multiplication operator (*):
produces the product of the operands.

The division operator (/):
produces the quotient of its operands where the left operand is the dividend and the right operand is the divisor.

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

+=

A

The addition assignment operator (+=) adds the value of the right operand to a variable and assigns the result to the variable.

The types of the two operands determine the behavior of the addition assignment operator. Addition or concatenation is possible.

let a = 2;
let b = 'hello';
console.log(a += 3); // addition
// expected output: 5
console.log(b += ' world'); // concatenation
// expected output: "hello world"
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

=

A

The simple assignment operator (=) is used to assign a value to a variable.

Chaining the assignment operator is possible in order to assign a single value to multiple variables.

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

++

A

The decrement operator (–) decrements (subtracts one from) its operand and returns a value.
The increment operator (++) increments (adds one to) its operand and returns a value.

prefix –x happens immediately
postfix x– happens after the row is executed

Postfix decrement
let x = 3;
y = x--;     // y = 3, x = 2
Prefix decrement
let a = 2;
b = --a;     // a = 1, b = 1
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Destructuring assignment

A

The destructuring assignment syntax is a JavaScript expression that makes it possible to unpack values from arrays, or properties from objects, into distinct variables.

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

/=

  • =
  • =
  • *=
A

The division assignment operator (/=) divides a variable by the value of the right operand and assigns the result to the variable.

same for the other arithmetic assignment operators

i.e the exponentiation assignment operator (**=) raises the value of a variable to the power of the right operand.

a = 3

a **= 2 
// a is assigned 9
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

==

!=

A

The equality operator (==) checks whether its two operands are equal, returning a Boolean result.

The inequality operator (!=) checks whether its two operands are not equal, returning a Boolean result

Unlike the strict equality operators, these attempt to convert and compare operands of different types.

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

**

A

The exponentiation operator (**) returns the result of raising the first operand to the power of the second operand. It is equivalent to Math.pow, except it also accepts BigInts as operands.

3 ** 4 //81

(2 ** 3) ** 2 //64

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

>

> =

<

<=

A

greater than

greater than or equal to

less than

less than or equal to

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

( )

A

The grouping operator ( ) controls the precedence of evaluation in expressions.

console.log((1 + 2) * 3); 
// 3 * 3
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

&&

!

||

A

Logical AND

Logical NOT

Logical OR

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

Object initializer

A

Objects can be initialized using new Object(), Object.create(), or using the literal notation (initializer notation).

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

object initializer literal notation

A

An object initializer literal notation is a comma-delimited list of zero or more pairs of property names and associated values of an object, enclosed in curly braces ({}).

const object1 = { a: ‘foo’, b: 42, c: {} };

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

does JavaScript have operator precedence?

A

YES, JavaScript does.

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

%

A

The modulo || remainder operator:
performs modular arithmetic with the left and right operand. returns the remainder

integer % modulus;

console.log(13 % 5);
// steps through the numbers inclusive 0 - exclusive 5
// 13 times, expected output: 3
// 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2, 3
console.log(4 % 2);
// expected output: 0
17
Q

%=

A

remainder assignment will assign a variable the remainder of modular arithmetic.

let a = 3
a %= 2
// 1, 0, 1
// a is now assigned 1

a %= 0 //NaN
a %= ‘string’ //NaN

18
Q

Unary Negation

A

The unary negation operator (-) precedes its operand and negates it.

const x = 4;
const y = -x;
console.log(y);
// expected output: -4
19
Q

Unary Plus

A

The unary plus operator (+) precedes its operand and evaluates to its operand but attempts to convert it into a number, if it isn’t already.

const y = -1;
console.log(+y);
// expected output: -1
console.log(+'');
// expected output: 0
console.log(+true);
// expected output: 1
console.log(+false);
// expected output: 0
console.log(+'hello');
// expected output: NaN
20
Q

&&=

A

The logical AND assignment (x &&= y) operator only assigns if x is truthy.

let a = 1;
let b = 0;

a &&= 2;
console.log(a); // expected output: 2

b &&= 2;
console.log(b); // expected output: 0

21
Q

??=

A

The logical nullish assignment (x ??= y) operator only assigns if x is nullish (null or undefined).

const a = { duration: 50 };

a. duration ??= 10;
console. log(a.duration); // expected output: 50

a. speed ??= 25;
console. log(a.speed); // expected output: 25

22
Q

||=

A

The logical OR assignment (x ||= y) operator only assigns if x is falsy.

const a = { duration: 50, title: ‘’ };

a. duration ||= 10;
console. log(a.duration); // expected output: 50

a. title ||= ‘title is empty.’;
console. log(a.title); // expected output: “title is empty”

23
Q

??

A

The nullish coalescing operator (??) is a logical operator that returns its right-hand side operand when its left-hand side operand is null or undefined, and otherwise returns its left-hand side operand.

const nullValue = null;
const emptyText = "";
const someNumber = 42;
const valA = nullValue ?? "default for A";
const valB = emptyText ?? "default for B";
const valC = someNumber ?? 0;

console. log(valA); // “default for A”
console. log(valB); // “” (as the empty string is not null or undefined)
console. log(valC); // 42

24
Q

?.

A

The optional chaining operator (?.) enables you to read the value of a property located deep within a chain of connected objects without having to check that each reference in the chain is valid.

The ?. operator is like the . chaining operator, except that instead of causing an error if a reference is nullish (null or undefined), the expression short-circuits with a return value of undefined. When used with function calls, it returns undefined if the given function does not exist.

25
Q

optional chaining operator examples

A
const potentiallyNullObj = null;
let x = 0;
const prop = potentiallyNullObj?.[x++];

console.log(x); // 0 as x was not incremented

onst customer = {
  name: "Carl",
  details: {
    age: 82,
    location: "Paradise Falls" // detailed address is unknown
  }
};
const customerCity = customer.details?.address?.city;
// … this also works with optional chaining function call
const customerName = customer.name?.getName?.(); // method does not exist, customerName is undefined
const customer = {
  name: "Carl",
  details: { age: 82 }
};
const customerCity = customer?.city ?? "Unknown city";
console.log(customerCity); // Unknown city
26
Q

…spread

A

‘…’ the spread operator

strings, arrays, objects, arguments it expands/spreads them

Allows an iterable to be expanded in places where zero or more
function calls/arguments/elements are expected.
OR allows an object expression to be expanded in places where
zero or more key-value pairs are expected

it can be used to:
pass array elements as arguments
create a new copy of an array
copy object properties by name

27
Q

…rest

A

rest is the inverse of spread, creates an array of parameters

function (x, y, …z) {}