Section 3: Types and Operators Flashcards

1
Q

Dynamic Typing

A

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

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

What keyword exist to tell the engine what kind of data you intend to put inside a variable?

A

There is NO keyword

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

What is a primitive Type?

A

A type of data that represents a single value

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

An object is a primitive type?, and why?

A

not, because an object is a collection of name / value pairs

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

How many primitive types exist in JS

A

6 primitive types

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

Name the 6 primitive types in JS

A

undefined

null

boolean

number

string

symbol

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

Describe the primitive type: Undefined

A

Represents lack of existence, its what the JS engine sets variables to initially and it will stay undefined unless you set the variable to have a value.

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

Describe the primitive type: Null

and when its useful

A

represents lack of existence (you can set a variable to this).

its useful when you want to represent that something doesn´t exist, that the variable has no value(set a variable to nothing).

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

Describe the primitive type: Boolean

A

Either true or false, just one of two values

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

Describe the primitive type: Number

Exist other numeric types in JS?

A

There’s only one numeric type.

Its a floating poing number (theres always some decimals)

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

Describe the primitive type: String

What we use to define a string?

A

A sequence of characters (both ‘’ and “” can be used)

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

Describe the primitive type: Symbol

A

It`s defined in ES6

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

What is an operator?

A

It`s a special function that is syntactically (written) differently. Generally operators take 2 parameters and return one result.

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

How did the JS engine that this was my intent to add 3 and 4?

var a = 3 + 4;

console.log(a); // result: 7

A

The syntax parser saw the plus(+) sign and added these 2 numbers.The plus sign is an operator, it’s actually a function.

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

What is the name of the notation that the JS engine provide to deal with operators.

A

Inflix notation

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

What it means “inflix notation”

A

Inflix mean that the function name, the operator, sits between the 2 parameters.

17
Q

When we type these operators, +, -, >, <, what is happening?

How these operators works?

A

These operators are a special type of functions, that the parameters between the operators are being passed to those functions and a value is being returned

Inside those functions is prewritten code, for you that the JS language provides to do or run, or invoke these functions.

18
Q

What is operators precedence?

A

It´s which operator function gets called first, functions are called in order of precedence (HIGHER precedence wins)

19
Q

What is operator associativity?

A

What order operator functiosn get called in: left-to-right or right-to-left, and this means when the operator functions have the same precedence

20
Q

What is the purpose of operator precedence and associativity?

A

help us determine which function run first and so does associativity.

21
Q

When comes into play operator associativity?

A

Associativity comes into play when two or more operators have the same precedence

22
Q

What is coercion?

A

It´s converting a value from one type to another.

23
Q

Why coercion happens in JS?

A

Because it´s dynamically typed

24
Q

How coercion and operators relate, and what we need to understand from this?

A

We understand that operators are functions, so they`re running code, and coercion is part of that process of calling that function, coercion happens because JS it´s dynamically typed.

25
Q

Explain why this code returns True

console.log( 3 < 2 < 1 );

A

Thanks to operators precedence and associativity, we now that the “< operator” is called 2 times so it has the same precedence, so associativity comes into place. The associativity is left-to-right.

“3 < 2” is called first, and the result is a boolean of “false”. Thanks to coercion, “false” is converted to the number of 0, so “0 < 1” is TRUE

26
Q

When coercion reprensents a problema that might be hard to debug? and why?

A

When we are comparing two values with the “==” operator, and one of the values is not from the same type

The double equal operator “==” will try to coerce one of the values to the other type

27
Q

How can we solve, the comparision problem with the “==” double equal operator, knowing that coercion could happend

A

We can use the “===” triple equal operator “strict equality”, that compares if they are the same type and the value. If they are not the same type, it will return FALSE

28
Q

Why this is all false

Boolean(undefined) // false

Boolean(null) // false

Boolean(“”) // false

A

Because of coercion, all things that imply the lack of existence, they convert to false.

29
Q

Describe one way to use coercion to our advantage?

A

Using an “if” statement

30
Q

Why the use of an “if” statement, can helps us to use coercion to our advantange?

A

Because whatever we put inside the parentheses of an if statement, it will attempt to convert or coerce to a Boolean

31
Q

When can we face a problem using an “if” statement and coercion to our advantange…

A

When we are going to coerce the value of 0, because

Boolean(0) –> False

0 is not lack of existence, maybe it’s a valid value

32
Q

How can we solve the problem using coercion, in an “if” statement, if we know that there is any chance that the value to coerce is going to be a 0?

A

doing:

var a; // it goes to the interner and returns something

if(a || a === 0) { }

Thanks to operator precedence and associativity, we know that it’s going to compare first if “a”, so if a has a value it will coerce to TRUE, either if is 0 and is FALSE, the || operator will do “a === 0”, and result in TRU

33
Q

How can we use coercion if we want to have a default value in a function and we don´t have passed any parameters to it?

and when it could be a problem, with what value?

A

We can use the operator ||

name = name || “default name”

thanks to operator precedence “||” is going to be run before “=”, and “||” will return the first value that is TRUE, so if name has not existence it means it`s FALSE, then it will return “default name” and will be assigned to “name”

We still need to be careful if name = 0;