Section 3: Types and Operators Flashcards
Dynamic Typing
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
What keyword exist to tell the engine what kind of data you intend to put inside a variable?
There is NO keyword
What is a primitive Type?
A type of data that represents a single value
An object is a primitive type?, and why?
not, because an object is a collection of name / value pairs
How many primitive types exist in JS
6 primitive types
Name the 6 primitive types in JS
undefined
null
boolean
number
string
symbol
Describe the primitive type: Undefined
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.
Describe the primitive type: Null
and when its useful
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).
Describe the primitive type: Boolean
Either true or false, just one of two values
Describe the primitive type: Number
Exist other numeric types in JS?
There’s only one numeric type.
Its a floating poing number (there
s always some decimals)
Describe the primitive type: String
What we use to define a string?
A sequence of characters (both ‘’ and “” can be used)
Describe the primitive type: Symbol
It`s defined in ES6
What is an operator?
It`s a special function that is syntactically (written) differently. Generally operators take 2 parameters and return one result.
How did the JS engine that this was my intent to add 3 and 4?
var a = 3 + 4;
console.log(a); // result: 7
The syntax parser saw the plus(+) sign and added these 2 numbers.The plus sign is an operator, it’s actually a function.
What is the name of the notation that the JS engine provide to deal with operators.
Inflix notation
What it means “inflix notation”
Inflix mean that the function name, the operator, sits between the 2 parameters.
When we type these operators, +, -, >, <, what is happening?
How these operators works?
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.
What is operators precedence?
It´s which operator function gets called first, functions are called in order of precedence (HIGHER precedence wins)
What is operator associativity?
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
What is the purpose of operator precedence and associativity?
help us determine which function run first and so does associativity.
When comes into play operator associativity?
Associativity comes into play when two or more operators have the same precedence
What is coercion?
It´s converting a value from one type to another.
Why coercion happens in JS?
Because it´s dynamically typed
How coercion and operators relate, and what we need to understand from this?
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.
Explain why this code returns True
console.log( 3 < 2 < 1 );
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
When coercion reprensents a problema that might be hard to debug? and why?
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
How can we solve, the comparision problem with the “==” double equal operator, knowing that coercion could happend
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
Why this is all false
Boolean(undefined) // false
Boolean(null) // false
Boolean(“”) // false
Because of coercion, all things that imply the lack of existence, they convert to false.
Describe one way to use coercion to our advantage?
Using an “if” statement
Why the use of an “if” statement, can helps us to use coercion to our advantange?
Because whatever we put inside the parentheses of an if statement, it will attempt to convert or coerce to a Boolean
When can we face a problem using an “if” statement and coercion to our advantange…
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
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?
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
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?
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;