Expressions & Operators Flashcards
What are two special values that denote the absence of a meaningful value?
null & undefined
What’s the difference between null and undefined?
Undefined typically means a variable has been declared but not defined.
Null is an empty or non-existent value. Null must be assigned.
What’s the special behavior of the || logical operator when the left hand side is not a boolean?
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';
What’s the special behavior of the && logical operator when the left hand side is not a boolean?
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;
Describe equality comparison for primitives like strings and numbers
Primitives like string and numbers are compared by their value.
Describe equality comparison for objects like arrays, dates, and plain objects.
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.
What does it mean to compare by reference?
Checks to see if the objects refer to the same location in memory.
What is this type of syntax called?
const [red, yellow, green] = foo;
Array destructuring
What is the purpose of array destructuring?
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 can you swap two variables in one expression?
You can use array destructuring:
let a = 1; let b = 2; [a, b] = [b, a]; console.log(a); // 2 console.log(b); // 1
What is this type of syntax called?
const a = { b: 7, c: true }; const {b, c} = a;
Object desctructuring
When unpacking a variable with object destructuring, how do you also assign it to a variable with a different name?
const a = { b: 7, c: true }; const {b: newName, c: anotherNew } = a; console.log(newName); // 7
What is the nullish coalescing operator?
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
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.
Nullish coalescing operator
Which operator allows an expression to be expanded into multiple items?
The spread operator, which is three dots.
const foo = ['b', 'c']; const bar = ['a', ...foo, 'd']; // ["a", "b", "c", "d"]