Todo 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
undefinednullbooleannumberstringsymbol
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: Nulland 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: NumberExist 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: StringWhat 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, +, -, >, 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 returnedInside 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 Trueconsole.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 valueswith the “==” operator, and one of the values is not from the same typeThe 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 falseBoolean(undefined) // falseBoolean(null) // falseBoolean(“”) // 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, becauseBoolean(0) –> False0 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 somethingif(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;
What values can hold an Object ? and how they are called?
A primitivethat is called a propertyAn object that is called a property alsoFunctions that are called methods
An object have reference in memory to their peorperties and methods?
YES
How is important to think about an Object as…(When we talk about how it´s an object in memory)
As sitting in memory, and then having references to other things sitting in memory that are connected to it, so it knows where its different properties and methods are, that is: primitives, objects and functions. (properties and methods)
How can we access those slots in memory, those properties and methods of an object?and how are their names(of the operators)?
With 2 operators:the dot operator . also called member accessthe computed memeber access operator [ ]
What does the computed member access operator [ ] ?and help me to remember what an operator is, under the hood?
Remember, an operator is a function.The computed member operator, takes two parameters and it’s associativity is left-to-rightto the left it takes the object, and inside the [ ] it takes the property or method
What does the dot . operator do?
It’s a fnction and when used after an object it takes two parameters, the object that you’re looking at and the name of the property.It takes the object(on the left) and on the right the string that represents the name of the value that you are looking for
What is the other name of the dot . operator?
It´s also called Member Access
Why the dot . operator is called Member Access
Because members being members of the object, These are looking for members of the object, the pieces, the methods, and the properties
How can we create an object with object literals
using curly braces { }
What is inside the curly braces { } how is it treated, how many lines of code?
It´s treated as one line of code
When it gets really powerful to use object literals?
Because we can create objects on the flyFor example, on passing parameters to a function, as an object literal
Let´s pass some object as a parameter to this function, how can we imagine this.function greet(person) { console.log(“Hi ” + person.firstname);}
We can call the greet funciton, and create an object on the flygreet({ firstname:‘Tony’, lastname:‘Garcia’});