Fundamentals 2: data types, conditionals, operators Flashcards
What are the 8 data types in JavaScript?
(1) numbers
- integers
- floating
- NaN
- ±infinity
(2) BigInt
(3) strings
(4) boolean
(5) null
(6) undefined
(7) symbol
(8) objects
What are integers limited by i.e. the highest number integers can go to?
±(2^53-1)
What is the relationship between null
and undefined
?
null is only equivalent to undefined with == operator, otherwise they were not equivalent to each other or themselves
null == undefined is true
null === undefined is false
null !== null
undefined !== undefined
What does primitive mean? Sort the 8 data types into primitives and non-primitives.
Primitive means it only has one value. A non=primitive can store many. The only non-primitive is objects
What are the 3 different ways to write conditionals?
(1) if (cond) … else …
(2) SWITCH
`switch (expr) {
case 1:
…code…
break;
case 2:
…code…
break;
default:
code
}
forgot what happens if default isn’t provided
(3) ternary operator
(cond) ?
code executed if true:
code executed if false;
when might we favour using switch
over if...else
?
Using switch can sometimes be more readable and more “descriptive” … (not too sure what “descriptive” means tho)
What are the 4 logical operators and what do they stand for?
(1) II OR
(2) && AND
(3) ! NOT
(4) ?? nullish coalescing
What is a method?
A built-in functionality in a language or specific data types.
e.g. slice() is a method that can only be applied to strings
Recall the algorithm to compare 2 strings
e.g. ‘Glow’and’Glee
- Compare the first character of both strings in Unicode order
- If the first character from the first string is greater (or less) than the other string’s, then the first string is greater (or less) than the second. We’re done.
- Otherwise, if both strings’ first characters are the same, compare the second characters the same way.
- Repeat until the end of either string.
- If both strings end at the same length, then they are equal. Otherwise, the longer string is greater.
What happens when values of different types are compared?
When values of different types are compared, they get converted to numbers (with the exclusion of a strict equality check).
e..g
alert( ‘2’ > 1 ); // true, string ‘2’ becomes a number 2
alert( ‘01’ == 1 ); // true, string ‘01’ becomes a number 1
alert( true == 1 ); // true
alert( false == 0 ); // true
What’s something to watch out for when using == operator?
0 == false; // true
‘’ == false; // true
This happens because operands of different types are converted to numbers by the equality operator==. An empty string, just likefalse, becomes a zero.
Complete exercises to test if you know what results we’ll get when different data types are combined
”” + 1 + 0
“” - 1 + 0
true + false
6 / “3”
“2” * “3”
4 + 5 + “px”
“$” + 4 + 5
“4” - 2
“4px” - 2
“ -9 “ + 5
“ -9 “ - 5
null + 1
undefined + 1
“ \t \n” - 2
”” + 1 + 0 = “10” // (1)
“” - 1 + 0 = -1 // (2)
true + false = 1
6 / “3” = 2
“2” * “3” = 6
4 + 5 + “px” = “9px”
“$” + 4 + 5 = “$45”
“4” - 2 = 2
“4px” - 2 = NaN
“ -9 “ + 5 = “ -9 5” // (3)
“ -9 “ - 5 = -14 // (4)
null + 1 = 1 // (5)
undefined + 1 = NaN // (6)
“ \t \n” - 2 = -2 // (7)