type conversions & math Flashcards
alert( “6” / “2” ); //returns?
3, strings are converted to numbers
let str = “123”;
alert(typeof str); // Returns
String
let str = “123”;
let num = Number(str);
alert(typeof num); // returns and why
number,
It becomes a number 123
let age = Number(“an arbitrary string instead of a number”);
alert(age); // returns?
NaN, conversion failed
Numeric conversion rules:
undefined becomes?
NAN
Numeric conversion rules:
Null becomes?
0
Numeric conversion rules:
true and false
1 & 0
Numeric conversion rules:
If the remaining string is empty, the result is _____
0
Numeric conversion rules:
String with characters// returns?
the number is “read” from the string. An error gives NaN.
alert( Number(“123z”) ); // returns?
NaN (error reading a number at “z”)
Numeric conversion rules:
Values that are intuitively “empty”, like 0, an empty string, null, undefined, and NaN, become _____.
false
False numeric conversion
0
empty string
null
undefined
NAN
alert( Boolean(“hello”) ); // return
true
alert( Boolean(“”) ); // returns
false
alert( Boolean(“0”) ); // returns
0
in JavaScript, a non-empty string is always true.
& alert( Boolean(“ “) ); // return?
spaces, also true (any non-empty string is true)
let x = 1;
x = -x;
alert( x ); // returns?
-1, unary negation was applied
alert( ‘1’ + 2 ); // returns?
“12”
contacination
alert( 2 + ‘1’ ); // returns?
“21”
alert(2 + 2 + ‘1’ ); // returns?
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’.
alert(‘1’ + 2 + 2); // “
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”.
alert( +true ); // returns and why?
1
But if the operand is not a number, the unary plus converts it into a number.
alert( +”” ); // returns
0
if the operand is not a number, the unary plus converts it into a number.
let apples = “2”;
alert( +apples);
write the longer variant
alert( Number(apples) );
let a = 1;
let b = 2;
let c = 3 - (a = b + 1);
alert( a ); // returns?
alert( c ); // returns?
3
0
let a, b, c;
a = b = c = 2 + 2;
alert( a ); //
alert( b ); //
alert( c ); //
all return 4
chaining
let n = 2;
n = n + 5;
n = n * 2;
write shorthand
n+=5
n*=2
Increment/decrement can only be applied to _______
variables
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.
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.
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
If we’d like to increment a value but use its previous value, we need the postfix form:
let counter = 0;
alert( ______); // 0
counter++
let counter = 1;
alert( 2 * ++counter ); // return
4
let counter = 1;
alert( 2 * counter++ ); //
2, because counter++ returns the “old” value
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.
For maths and other comparisons null becomes ____
0
For maths and other comparisons
undefined becomes _____.
NaN
let a = 1, b = 1;
let c = ++a; // ?
let d = b++; // ?
2
1