Operators Flashcards

1
Q

What is returned from:
false || ‘’ || true
‘’ || false || 0

How might this be used?

A

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.

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

What is returned from:
true && 0 && true
‘1’ && ‘2’ && ‘3’

A

0

‘3’

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

Does && or || have higher precedence

A

&& has higher precedence

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

What happens with:

console.log(( console.log(1) || console.log(2) || console.log(3) ));

A

1
2
3
undefined

The expressions in && and || are run even if they are not returned.

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

What happens with:

console.log(( console.log(1)));

A

1

undefined

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

What happens with

alert( alert(1) && alert(2) );

A

1 (alert(1) returns undefined, so the comparison stops and alert(1) is evaluated)
undefined

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

Will this alert true?

alert( null || 2 && 3 || 4 );

A

No
It will alert 3.
within an if statement or ? operator, it will evaluate true or false. But does not on it’s own.

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

Do || and && return Boolean?

A

no

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

What is ?? called?

what’s does it do?

A

Nullish coalescing operator ‘??’
Returns the first non-null and non-undefined operand.

Or returns the last processed value

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

What’s returned from
false ?? true
false ?? null
null ?? false

A

false
false
false

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

What is the main use case for Nullish coalescing operator ‘??’ ?

A

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

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

Difference between || and ??

A

|| 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.

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

What is alerted
let height = 0;
alert(height || 100);
alert(height ?? 100);

A
// 100
 // 0
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q
Put brackets around the precedence
let area = height ?? 100 * width ?? 50;
A

let area = (height) ?? (100 * width) ?? (50);

When in doubt (about effect or readability), use parenthesis

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

What happens with:
let x = 3;
console.log( (x = 2) && 7 );
console.log(x);

A

7
2

The expressions in && and || are run even if they are not returned.

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

What has higher precedence (evalutated sooner):
a ||
or a &&

(‘a’ just to prevent html evaluation of this card)

A

a &&
1 && 1 || 1 && 1
is like:
(1 && 1) || (1 && 1)

17
Q

What does a higher precedence number ( 15 higher than 2) denote?

A

The higher the number, the sooner it is evaluated.

18
Q

What is logged to console from this:

console. log( 1 || 2 && 3)
console. log( 1 || (2 && 3))
console. log( (1 || 2) && 3)

A

1
1
3

19
Q

Is it ok to call this true? ‘string’

A

no. It is truthy or it evaluates as true

20
Q

Are truthy things true?

console.log(‘true’ == true);

A

false

truthy does not mean it’s true.

21
Q

Escape a quotation mark or mark a newline in a string

A

Backslash \ before the character

“this is a "fine" day”

\n

22
Q

Difference between .concat() and +

A

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

23
Q

What is an operand?

A

The thing operators are applied to

1 / 2

/ is the operator

1 and 2 are operands (or arguments)

24
Q

What is a unary operator?

What is a binary operator

A

An operator with 1 operand

  • 2 negation operator

An operator with 2 operands:

2 - 3 subtraction operator

25
Q

What are the following operators?

/

*

**

%

A

division

multiplication

exponentiation

remainder

26
Q

Does the = operator in:
x = 21;
return anything?
Do all operators return something?

A

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

27
Q

What are these:

&

|

~

«

> >

> > >

A

Bitwise operators, act on bits

AND

OR

XOR

NOT

LEFT SHIFT

RIGHT SHIFT

ZERO-FILL RIGHT SHIFT

28
Q

what does the comma operator (,) do?

A

Separates expressions and allows them all to evaluate. But only 1 will be returned. (returns the last one)

29
Q

What does this return?

let b = 4, let t = 5;

A

an error

30
Q

All comparison operators return a boolean value

t or f

A

True

31
Q

What has higher precidence (evaluated sooner)

&& or ||

A

&&

32
Q

What does the ~ do?

How does it behave with base 10 numbers?

A

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)

33
Q

How can the bitwise not ~

be used?

A

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
}

34
Q

~4294967295

A

This specific number converts to 0.

It’s 11111111111111111111111111111111 in decimal

35
Q

Higher precedence operators are evaluated later

t or f

A

false

sooner

36
Q

What does this return:
…[1, 2, 3]
How can it be used?

A

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)

  1. Unpacking
    you can use it to input array elements where you just want to input the elements:
    let result = compare(…[1, 2])
37
Q

What does the … spread operator do?

What does the … rest operator do?

A

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.

38
Q
What happens with this code?
function f(arg1, ...rest, arg2) {  
}
A

error

Rest parameter has to come at the end.

39
Q
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?

A

gives error

afd [ ‘b’, ‘e’, ‘d’ ] cdd

[…rest] is actually spread syntax. It’s splitting ‘bed’ into an array.