JS Operators Flashcards
new
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);
*
/
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.
+=
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"
=
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.
–
++
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
Destructuring assignment
The destructuring assignment syntax is a JavaScript expression that makes it possible to unpack values from arrays, or properties from objects, into distinct variables.
/=
- =
- =
- *=
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
==
!=
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.
**
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
>
> =
<
<=
greater than
greater than or equal to
less than
less than or equal to
( )
The grouping operator ( ) controls the precedence of evaluation in expressions.
console.log((1 + 2) * 3); // 3 * 3
&&
!
||
Logical AND
Logical NOT
Logical OR
Object initializer
Objects can be initialized using new Object(), Object.create(), or using the literal notation (initializer notation).
object initializer literal notation
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: {} };
does JavaScript have operator precedence?
YES, JavaScript does.
%
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
%=
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
Unary Negation
The unary negation operator (-) precedes its operand and negates it.
const x = 4; const y = -x;
console.log(y); // expected output: -4
Unary Plus
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
&&=
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
??=
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
||=
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”
??
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
?.
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.
optional chaining operator examples
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
…spread
‘…’ 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
…rest
rest is the inverse of spread, creates an array of parameters
function (x, y, …z) {}