Operators Flashcards
What is returned from:
false || ‘’ || true
‘’ || false || 0
How might this be used?
true
0
alert( firstName || lastName || nickName || “Anonymous”);
Alerts the name if a name is there, otherwise alerts “Anonymous”
OR
true || alert(“not printed”);
false || alert(“printed”);
Alerts depending if the printed-condition is true.
What is returned from:
true && 0 && true
‘1’ && ‘2’ && ‘3’
0
‘3’
Does && or || have higher precedence
&& has higher precedence
What happens with:
console.log(( console.log(1) || console.log(2) || console.log(3) ));
1
2
3
undefined
The expressions in && and || are run even if they are not returned.
What happens with:
console.log(( console.log(1)));
1
undefined
What happens with
alert( alert(1) && alert(2) );
1 (alert(1) returns undefined, so the comparison stops and alert(1) is evaluated)
undefined
Will this alert true?
alert( null || 2 && 3 || 4 );
No
It will alert 3.
within an if statement or ? operator, it will evaluate true or false. But does not on it’s own.
Do || and && return Boolean?
no
What is ?? called?
what’s does it do?
Nullish coalescing operator ‘??’
Returns the first non-null and non-undefined operand.
Or returns the last processed value
What’s returned from
false ?? true
false ?? null
null ?? false
false
false
false
What is the main use case for Nullish coalescing operator ‘??’ ?
The common use case for ?? is to provide a default value for a potentially undefined variable.
alert(user ?? “Anonymous”);
|| will also skip false and other falsey things. ?? only skips null and undefined
Difference between || and ??
|| returns the first truthy value.
?? returns the first defined value. It doesn’t treat 0, ‘’, or false as undefined.
|| doesn’t distinguish between false, 0, an empty string “” and null/undefined.
What is alerted
let height = 0;
alert(height || 100);
alert(height ?? 100);
// 100 // 0
Put brackets around the precedence let area = height ?? 100 * width ?? 50;
let area = (height) ?? (100 * width) ?? (50);
When in doubt (about effect or readability), use parenthesis
What happens with:
let x = 3;
console.log( (x = 2) && 7 );
console.log(x);
7
2
The expressions in && and || are run even if they are not returned.
What has higher precedence (evalutated sooner):
a ||
or a &&
(‘a’ just to prevent html evaluation of this card)
a &&
1 && 1 || 1 && 1
is like:
(1 && 1) || (1 && 1)
What does a higher precedence number ( 15 higher than 2) denote?
The higher the number, the sooner it is evaluated.
What is logged to console from this:
console. log( 1 || 2 && 3)
console. log( 1 || (2 && 3))
console. log( (1 || 2) && 3)
1
1
3
Is it ok to call this true? ‘string’
no. It is truthy or it evaluates as true
Are truthy things true?
console.log(‘true’ == true);
false
truthy does not mean it’s true.
Escape a quotation mark or mark a newline in a string
Backslash \ before the character
“this is a "fine" day”
\n
Difference between .concat() and +
For strings, no difference
But + will convert arrays to strings before concatenating. It can be used with numbers.
.concat() is a method on strings and arrays. When used on a number will throw an error
What is an operand?
The thing operators are applied to
1 / 2
/ is the operator
1 and 2 are operands (or arguments)
What is a unary operator?
What is a binary operator
An operator with 1 operand
- 2 negation operator
An operator with 2 operands:
2 - 3 subtraction operator
What are the following operators?
/
*
**
%
division
multiplication
exponentiation
remainder
Does the = operator in:
x = 21;
return anything?
Do all operators return something?
It FIRST writes value 21 to variable x
But THEN alsoreturns something. All operators return something
let a = 1; let b = 2;
let c = 3 - (a = b + 1);
alert( a ); // 3
alert( c ); // 0
What are these:
&
|
~
«
> >
> > >
Bitwise operators, act on bits
AND
OR
XOR
NOT
LEFT SHIFT
RIGHT SHIFT
ZERO-FILL RIGHT SHIFT
what does the comma operator (,) do?
Separates expressions and allows them all to evaluate. But only 1 will be returned. (returns the last one)
What does this return?
let b = 4, let t = 5;
an error
All comparison operators return a boolean value
t or f
True
What has higher precidence (evaluated sooner)
&& or ||
&&
What does the ~ do?
How does it behave with base 10 numbers?
bitwise not
takes the not of all the bits
In decimal, it basically does this: -(n+1) except in some cases
alert( ~2 ); // -3, the same as -(2+1)
alert( ~1 ); // -2, the same as -(1+1)
alert( ~0 ); // -1, the same as -(0+1)
alert( ~-1 ); // 0, the same as -(-1+1)
How can the bitwise not ~
be used?
alert( ~2 ); // -3, the same as -(2+1)
alert( ~1 ); // -2, the same as -(1+1)
alert( ~0 ); // -1, the same as -(0+1)
alert( ~-1 ); // 0, the same as -(-1+1)
because ~n is only going to be 0 (falsy) when n = -1, it can be used as an automatic check if an index searcher found it (they return -1 for not existent indexes).
let str = “Widget”;
if (~str.indexOf(“Widget”)) {
alert( ‘Found it!’ ); // works
}
~4294967295
This specific number converts to 0.
It’s 11111111111111111111111111111111 in decimal
Higher precedence operators are evaluated later
t or f
false
sooner
What does this return:
…[1, 2, 3]
How can it be used?
it returns 1, 2, 3
Return a series of elements as arguments.
Return a series of elements into another array or object.
1. Cloning
You can use it to shallow copy an array or object
let arr = [1,2,3];
let arr2 = [4,5];
arr = […arr,…arr2];
(slower than .concat)
- Unpacking
you can use it to input array elements where you just want to input the elements:
let result = compare(…[1, 2])
What does the … spread operator do?
What does the … rest operator do?
Spread syntax (…):
allows an iterable such as an array expression or string to be expanded in places where zero or more arguments (for function calls) or elements (for array literals) are expected,
or an object expression to be expanded in places where zero or more key-value pairs (for object literals) are expected.
Rest parameter syntax (…):
The rest parameter syntax allows a function to accept an indefinite number of arguments as an array, providing a way to represent variadic functions in JavaScript.
What happens with this code? function f(arg1, ...rest, arg2) { }
error
Rest parameter has to come at the end.
What happens with this code: function apple(arg1, [...rest], arg2) { console.log(arg1, rest, arg2); } apple(1, 2, 3, 4);
What happens with this code: function apple(arg1, [...rest], arg2) { console.log(arg1, rest, arg2); } apple('afd', 'bed', 'cdd', 'dgd');
Why?
gives error
afd [ ‘b’, ‘e’, ‘d’ ] cdd
[…rest] is actually spread syntax. It’s splitting ‘bed’ into an array.