Expressions & Operators Flashcards

1
Q

What are two special values that denote the absence of a meaningful value?

A

null & undefined

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

What’s the difference between null and undefined?

A

Undefined typically means a variable has been declared but not defined.

Null is an empty or non-existent value. Null must be assigned.

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

What’s the special behavior of the || logical operator when the left hand side is not a boolean?

A

When the left hand side is not boolean, it will convert it to a boolean to decide which side to take.

expr1 || expr2 : if expr1 can be converted to true, returns expr1. It can be used to fallback to a default value.

const car = {};
const color = car.color || 'green';
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What’s the special behavior of the && logical operator when the left hand side is not a boolean?

A

When the left hand side is not boolean, it will convert it to a boolean to decide which side to take.

expr1 && expr2 : if expr1 can be converted to true, returns expr2. Can be used to check if object is defined (or truthy) before using it.

const car = { color: 'green' };
const color = car && car.color;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Describe equality comparison for primitives like strings and numbers

A

Primitives like string and numbers are compared by their value.

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

Describe equality comparison for objects like arrays, dates, and plain objects.

A

Objects like arrays, dates, and plain objects are compared by their reference, which checks to see if the objects refer to the same location in memory.

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

What does it mean to compare by reference?

A

Checks to see if the objects refer to the same location in memory.

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

What is this type of syntax called?

const [red, yellow, green] = foo;

A

Array destructuring

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

What is the purpose of array destructuring?

A

Makes it possible to unpack values from arrays into distinct variables.

const foo = ['one', 'two', 'three'];
const [red, yellow, green] = foo;
console.log(red); // 'one'
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How can you swap two variables in one expression?

A

You can use array destructuring:

let a = 1;
let b = 2;
[a, b] = [b, a];
console.log(a); // 2
console.log(b); // 1
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is this type of syntax called?

const a = { b: 7, c: true };
const {b, c} = a;
A

Object desctructuring

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

When unpacking a variable with object destructuring, how do you also assign it to a variable with a different name?

A
const a = { b: 7, c: true };
const {b: newName, c: anotherNew } = a;
console.log(newName); // 7
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is the nullish coalescing operator?

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 foo = null ?? 'default';
console.log(foo); // default
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What’s the name of the 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.

A

Nullish coalescing operator

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

Which operator allows an expression to be expanded into multiple items?

A

The spread operator, which is three dots.

const foo = ['b', 'c'];
const bar = ['a', ...foo, 'd'];
// ["a", "b", "c", "d"]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Using the spread operator, what two places can you expand an iterable expression into? (Iterable expression being like an array or a string.)

A

1) Arguments for function calls

function foo(x, y, z) { }
const args = [1, 2, 3];
foo(...args);
// instead of foo.apply(null, args)

2) Elements for array literal

const foo = ['b', 'c'];
const bar = ['a', ...foo, 'd'];
// ["a", "b", "c", "d"]
17
Q

Using the spread operator, where can you expand an object?

A

You can expand an object into key-value pairs for object literals.

const obj1 = { foo: 'bar', x: 42 };
const clonedObj = { ...obj1 };
// Object { foo: "bar", x: 42 }
18
Q

How do you remove a property from an object?

A

The delete operator.

const employee = {
	firstname: “John”,
	lastname; “Doe”
};
console.log(employee.firstname);
// “John”
delete employee.firstname;
console.log(employee.firstname);
// undefined
19
Q

What does the delete operator do?

A

Removes a property from an object.

const employee = {
	firstname: “John”,
	lastname; “Doe”
};
console.log(employee.firstname);
// “John”
delete employee.firstname;
console.log(employee.firstname);
// undefined
20
Q

What does the ‘in’ operator do?

A

The ‘in’ operator returns true if the specified property is in the specified object or its prototype chain.

const car = {
	make: ‘Honda’,
	model: ‘Accord’
};
console.log(‘make’ in car);
// true
delete car.make;
console.log(‘make’ in car);
// false
21
Q

How do you check if a property is in an object?

A

The ‘in’ operator returns true if the specified property is in the specified object or its prototype chain.

const car = {
	make: ‘Honda’,
	model: ‘Accord’
};
console.log(‘make’ in car);
// true
delete car.make;
console.log(‘make’ in car);
// false
22
Q

What does the instanceof operator do?

A

The instanceof operator tests whether the prototype property of a constructor appears anywhere in the prototype chain of an object.

function Car(make, model) {
  this.make = make;
  this.model = model;
}
const myCar = new Car('Honda', 'Accord');
const a = myCar instanceof Car; // true
const b= myCar instanceof Object; //true
23
Q

What happens when the new operator executes?

const foo = new Foo(1, 2, 3);

A

When the code ‘new Foo(…)’ executes, the following things happen:

1) A new object is created, inheriting from the Foo.prototype.
2) The constructor function Foo is called with the specified arguments, and with ‘this’ bound to the newly created object.
3) The object returned by the constructor function becomes the result of the whole ‘new’ expression. If the constructor function doesn’t explicitly return an object, the object created in Step 1 is used instead. Normally constructors don’t return a value.

24
Q

What does the typeof operator return?

A

The typeof operator returns a string indicating the type of the operand.

console. log(typeof 42); // “number”
console. log(typeof ‘foo’); // “string”
console. log(typeof true); // “boolean”

25
Q

What is logged here?

const str = new String('foo');
console.log(typeof str);
A

It logs “object” because all constructor functions will be typeof “object” (except for Function constructors).

26
Q

What is the result of this?

typeof null

A

“object”

27
Q

What is the result of this?

typeof undefined

A

“undefined”

28
Q

Here are two ways to check if x has a value. What is the difference between them?

if (x !== undefined && x !== null) {

}

if (x) {

}

A
if (x !== undefined && x !== null) {
  // x is not undefined nor null
  // x could be false, 0, NaN, or ‘’
}
if (x) { // truthy
  // x is not undefined, null, 0, NaN, or ‘’
}
29
Q

Here are two ways to check if x is undefined or null. What is the difference between them?

if (x === undefined || x === null) {

}

if (!x) { // falsy

}

A
if (x === undefined || x === null) {
  // x is either undefined or null
  // x is not false, 0, NaN, or ‘’
}
if (!x) { // falsy
  // x is undefined, null, false, 0, NaN, or ‘’
}
30
Q

What is a falsy value?

A

A falsy value is a value that is considered false when encountered in a Boolean context.

31
Q

Give six examples of falsy values.

A
  • false
  • 0
  • ’’ or “” or ``
  • null
  • undefined
  • NaN
32
Q

What are the data types?

A

Primitives:
String, Number, Boolean, Null, Undefined, BigInt, Symbol

Non-primitive: Object