Fundamentals 2: data types, conditionals, operators Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

What are the 8 data types in JavaScript?

A

(1) numbers
- integers
- floating
- NaN
- ±infinity
(2) BigInt
(3) strings
(4) boolean
(5) null
(6) undefined
(7) symbol
(8) objects

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

What are integers limited by i.e. the highest number integers can go to?

A

±(2^53-1)

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

What is the relationship between null and undefined?

A

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

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

What does primitive mean? Sort the 8 data types into primitives and non-primitives.

A

Primitive means it only has one value. A non=primitive can store many. The only non-primitive is objects

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

What are the 3 different ways to write conditionals?

A

(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;

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

when might we favour using switch over if...else?

A

Using switch can sometimes be more readable and more “descriptive” … (not too sure what “descriptive” means tho)

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

What are the 4 logical operators and what do they stand for?

A

(1) II OR
(2) && AND
(3) ! NOT
(4) ?? nullish coalescing

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

What is a method?

A

A built-in functionality in a language or specific data types.

e.g. slice() is a method that can only be applied to strings

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

Recall the algorithm to compare 2 strings

e.g. ‘Glow’and’Glee

A
  • 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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What happens when values of different types are compared?

A

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

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

What’s something to watch out for when using == operator?

A

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.

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

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

A

”” + 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)

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