type conversions & math Flashcards

1
Q

alert( “6” / “2” ); //returns?

A

3, strings are converted to numbers

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

let str = “123”;
alert(typeof str); // Returns

A

String

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

let str = “123”;
let num = Number(str);
alert(typeof num); // returns and why

A

number,
It becomes a number 123

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

let age = Number(“an arbitrary string instead of a number”);
alert(age); // returns?

A

NaN, conversion failed

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

Numeric conversion rules:
undefined becomes?

A

NAN

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

Numeric conversion rules:
Null becomes?

A

0

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

Numeric conversion rules:
true and false

A

1 & 0

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

Numeric conversion rules:
If the remaining string is empty, the result is _____

A

0

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

Numeric conversion rules:
String with characters// returns?

A

the number is “read” from the string. An error gives NaN.

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

alert( Number(“123z”) ); // returns?

A

NaN (error reading a number at “z”)

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

Numeric conversion rules:

Values that are intuitively “empty”, like 0, an empty string, null, undefined, and NaN, become _____.

A

false

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

False numeric conversion

A

0
empty string
null
undefined
NAN

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

alert( Boolean(“hello”) ); // return

A

true

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

alert( Boolean(“”) ); // returns

A

false

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

alert( Boolean(“0”) ); // returns

A

0
in JavaScript, a non-empty string is always true.

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

& alert( Boolean(“ “) ); // return?

A

spaces, also true (any non-empty string is true)

17
Q

let x = 1;
x = -x;
alert( x ); // returns?

A

-1, unary negation was applied

18
Q

alert( ‘1’ + 2 ); // returns?

A

“12”
contacination

19
Q

alert( 2 + ‘1’ ); // returns?

20
Q

alert(2 + 2 + ‘1’ ); // returns?

A

Here, operators work one after another. The first + sums two numbers, so it returns 4, then the next + adds the string 1 to it, so it’s like 4 + ‘1’ = ‘41’.

21
Q

alert(‘1’ + 2 + 2); // “

A

Here, the first operand is a string, the compiler treats the other two operands as strings too. The 2 gets concatenated to ‘1’, so it’s like ‘1’ + 2 = “12” and “12” + 2 = “122”.

22
Q

alert( +true ); // returns and why?

A

1
But if the operand is not a number, the unary plus converts it into a number.

23
Q

alert( +”” ); // returns

A

0
if the operand is not a number, the unary plus converts it into a number.

24
Q

let apples = “2”;
alert( +apples);

write the longer variant

A

alert( Number(apples) );

25
let a = 1; let b = 2; let c = 3 - (a = b + 1); alert( a ); // returns? alert( c ); // returns?
3 0
26
let a, b, c; a = b = c = 2 + 2; alert( a ); // alert( b ); // alert( c ); //
all return 4 chaining
27
let n = 2; n = n + 5; n = n * 2; write shorthand
n+=5 n*=2
28
Increment/decrement can only be applied to _______
variables
29
let counter = 1; let a = ++counter; // (*) alert(a); // returns
2 In the line (*), the prefix form ++counter increments counter and returns the new value, 2. So, the alert shows 2.
30
let counter = 1; let a = counter++; alert(a); // returns
1 In the line (*), the postfix form counter++ also increments counter but returns the old value (prior to increment). So, the alert shows 1.
31
If we’d like to increase a value and immediately use the result of the operator, we need the prefix form: let counter = 0; alert( _______ ); // 1
++counter
32
If we’d like to increment a value but use its previous value, we need the postfix form: let counter = 0; alert( ______); // 0
counter++
33
let counter = 1; alert( 2 * ++counter ); // return
4
34
let counter = 1; alert( 2 * counter++ ); //
2, because counter++ returns the "old" value
35
let a = (1 + 2, 3 + 4); alert( a ); //
7 (the result of 3 + 4) The comma operator allows us to evaluate several expressions, dividing them with a comma ,. Each of them is evaluated but only the result of the last one is returned.
36
For maths and other comparisons null becomes ____
0
37
For maths and other comparisons undefined becomes _____.
NaN
38
let a = 1, b = 1; let c = ++a; // ? let d = b++; // ?
2 1