Types and Operators Flashcards

1
Q

What is dynamic typing?

A

You don’t tell the JS engine what type of data a variable holds. Instead, it figures it out while your code is running. Variables can hold different types of values because it’s all figured out during execution.

Other languages often use Static Typing, which makes you define the data type when you declare the variable. For example, this would cause an error:

bool isNew = ‘hello’; //error

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

What is a primitive type?

A

A type of data that represents a single value, ie not an Object.

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

Name and define the six primitive types.

A
  1. Undefined - represents a lack of existence. This is what JS sets variables to by default initially and so you should never set your variables to undefined to avoid confusion.
  2. Null - also represents lack of existence. You should use this when you want to set them equal to nothing.
  3. Boolean - true or false.
  4. Number - Floating point number (there’s always some decimals). Unlike other programming languages, there’s only one ‘number’ type…and it can make math weird.
  5. String - A sequence of characters (both single and double quotes can be used).
  6. Symbol - Used in ES6, provide a way to attach properties to an object with more privacy and can be used with unique values.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is an operator?

A

Operators are actually functions that are built into the JS engine by default, they just appear syntactically different.

Generally, operators take two parameters and return one result. JS operators use infix notation, which means that the operator sits between the two parameters and makes it more human-readable, but it’s still behaving otherwise like a function.

infix notation: 3 + 4;

prefix notation: +3 4;

postfix notation: 3 4+

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

What is operator precedence?

A

This defines the order in which operator functions get called. Funcations with higher precedence are called first.

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

What is associativity?

A

This defines the order operator functions get called in(left-right or right-left) when functions have the same precedence.

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

What will this output to the console?

A

They are all equal to 4 because of associativity. The assignment operator works right-left and so the process looks like this to the JS engine:

a = b = c; (b = c is like the assignment function is running and returns 4)

a = b(4);

a = 4;

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

What is coercion?

A

Converting a value from one type to another.

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

What does this code output?

A

This happened because the first paramer, 1, was coerced to a string.

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

What should this output and why?

A

It should output true. The reason is that the < operator has left-to-right associativity. Also, the < operator is built to return a boolean. So, to the JS engine, parsing 3 < 2 < 1 would look like this:

  1. 3 < 2 < 1 - 3 is not less than 2, so it returns false for that comparison.
  2. false < 1, now it will need to coerce false to a number in order to complete the comparison and false becomes 0.
  3. 0 < 1, now this will return true.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What will Number(undefined) return?

A

NaN, or not a number. This is not a primitive type, but you can treat it as such - it’s essentially saying this cannot be converted into a number.

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

What will Number(null) return?

A

It will return 0. So, while null and undefined are pointing to the same value, to the javascript engine null can be converted to a number, unlike undefined which returns NaN.

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

What will this code return?

false == 0

A

True

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

What will this code return?

null == 0

A

False

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

What will this code return?

null < 1

A

True

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

What will “” == 0 return?

A

True

17
Q

What will “” == false return?

A

True

18
Q

What will “3” === “3” return?

A

True

19
Q

What will “3” === 3 return?

A

False

20
Q

What will this code return and why?

A

It returns nothing. The reason is that since a is declared, but not defined the javascript engine sets it to undefined. Any value inside and if statement will be coerced by the JS engine to a boolean. So, this means that it will need to coerce undefined, in this example to a boolean, which will be coerced to false. It would be the same situation were a defined as null or ““(empty string).

So, if a is set to any value, it will be coerced to true and return the log statement. The one exception is a = 0, which will also return false because 0 when coerced becomes false.

21
Q

What will this code return and why?

A

It will return ‘Hello undefined’.

This is because name is declared, but not defined and is set to undefined by JS engine, which is then coerced to a string in the log statement.

22
Q

What will this return?

A

It will return ‘Hello <your>'</your>

Because || returns either the true value, or the first true value if they’re both true, it will give the second value since name was never defined.