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.