LS 101 Flashcards
LS101 Methods, Terminology, and Concepts
What is the difference between Git and Github?
Git is a distributed version control system designed to be used locally without a central repository.
Github is a cloud based remote repository which acts as a centralized repo for git to enable collaboration.
When should you nest Git repositories?
You should never nest Git repositories
What are the built-in (primitive )JavaScript data types?
String, Number, Boolean, Undefined, Null
Does Javascript have Integer and Float specific Numeric data types?
No. Every numeric value has the primitive type of Number and is represented using a floating point number system.
Are all primitive values immutable?
Yes. String, Number, Boolean, Undefined, and Null are all immutable.
Is NaN a primitive value?
Yes. NaN is technically a number so it is a primitive.
Does this contain a statement, an expression, or both?
Both.A variable declaration with an optional assignment is an expression.Anything valid to the right of = is an expression, so 3 is an expression.
The code to the right of the assignment operator =
is an ??????????.
an expression
Is the reassignment of myVariable from a String to a Number valid since they are different data types?Is the original value (‘Hello, World’) changed by the reassignment?
No, JavaScript variables are not type specific.No, that is a primitive so the reassignment either creates a new value or assigns the variable to refer to an object.
When using == to compare a number and a string, what happens as far as coercion?
The string is always coerced to a number regardless of which side the number/string is on and then the comparison is made using === with the coerced value.
What is the return value in JavaScript of coercing a non numeric string (“blue”) to a number?
NaN
What does typeof NaN return?
number, NaN is an instance of the primitive type number
Return Values:
The return values are different since theletstatement always returnsundefined, but assignment expressions that aren’t part of a statement return the new value for the variable.
What is always the return value of a let/const statement?
undefined
What is the return value of this snippet:
undefined. an assignment using the let keyword will always return undefined.
What is the return value of this snippet:
false, an assignment not using the let/const/var keyword always returns the value on the right side of the =.
How do you create a single line comment in JavaScript?
use // to make the entire line a comment
What is best practice for max line length in JS?
80 characters
What formatting is used for variable names and function names?
camelCase
What is different about the naming convention for a constructor function?
You use PascalCase instead of camelCase.
What is different about the naming convention for a constant?
You use SCREAMING_SNAKE_CASE to name a constant
What are the only type of characters that should be used when naming variables, constants, and functions in JavaScript?
Alphanumeric characters (a-z, A-Z without accents) and numeric characters.When naming a variable the first character should ALWAYS be alphabetic.Constants may use underscores$ is allowed in names but only in specific cases, they are legal but should not be used in general.
let 5Miles = 5280 * 5;is this a valid variable declaration?
The declaration will work but you shouldn’t ever start a variable/function name with a numeric character.
Does this snippet adhere to code the JavaScript style guide:
Yes, there must be a space between the key word and condition check and it is best practice to put a space between the conditional and the opening curly bracket.
Should you always use a space between an operator and operand in Javascript?
Yes, while it will work correctly without the spaces it makes it harder to read with them.
When should you not end a line with “;”
Use semicolons to terminate each logical line of code unless the line ends with{,}, or:.
What is a short circuit operator?
In an && or || statement, JavaScrip stops evaluating sub expressions as soon as it can be 100% certain of the result of the final value.If the first expression evaluated in an && returns a falsy value, it doesn’t matter what the other side returns so it immediately evaluates to false.The opposite occurs with || since a truthy value on either side results in it evaluating to true.
What are the falsy values in JavaScript?
falseundefinednull”” (empty string)0NaN
How does the conditional in this code snippet use short circuit operator behavior to ensure it runs?
If name is null/undefined, there would be an error when the conditional executed if we didn’t leverage the behavior of && to prevent name.length from being evaluated if it doesn’t exist.In a comparison using &&, if the first value evaluates to false the second expression will never be evaluated, so even if name is null/undefined the code won’t stop executing because the comparison will immediately evaluate to false and the code in the else block will run.
Is it true that JavaScript never evaluates the right side of a short circuit operator when the expression short circuits?
Yes, the right side of an && / || comparison will never be evaluated if the result is settled by the first evaluation. (false on the left for &&, true on the left for ||).
What’s wrong with this snippet?
Even though it may work as intended, putting an assignment operator in a conditional is highly discouraged.
What is implicit type coercion?
Implicit type coercion occurs when you perform an operation on two different data types and JavaScript coerces one of them to match the other. It is not coercion explicitly performed by the developer.
What dictates how values get coerced?
The operator used in the expression, the most common operators are ==and+.
When does the non-strict equality operator (==) work identically to the strict equality operator?
When the operands have the same type.
What happens during a comparison of two operands with two different types using the non-strict equality operator?
One of the operands is implicitly converted.
When comparing operands of two different types with the strict-equality operator will the result ever be true?
No. You will always get a false result when comparing operands of 2 different types.
As far as implicit conversion, what is happening in this snippet?
When comparing a string to a number, the string is always coerced into a number. Then the comparison is done using the converted values.1 === 1> true
What happens when you compare a boolean with any other type using ==?
The boolean is converted to it’s numeric equivalent, 1 is true and 0 is false. Then the comparison is performed using the numerical value.
What is happening in this snippet as far as implicit coercion?
A boolean compared to any other value type using == is converted to a number (true is 1 false is 0).Now the comparison is ‘1’ == 1, when you compare a number to a string, the string is converted to it’s numerical equivalent or NaN.Now the operands have the same type and the comparison is 1 === 1.
What does this return?
true, the non-strict equality operator considers these equivalent.
What are we actually checking for in the comparison after the if in this snippet?
We’re checking if val is undefined or null since we are using the == to perform the comparison. This is not a best practice though and shouldn’t be used.
How does == compare to === when comparing objects instead of primitive values?
It works exactly the same in that it returns true only if the two objects are the exact same object.
What does this return?
True, When you compare a plain object {} to a string using ==, it converts the object into the string’[object Object]’ and then performs the comparison.
What does this return?
True, when comparing an empty array to a string the empty array is converted to an empty string (‘’).
When should == be used?
Most developers recommend to never use it. But the argument has been made that if you are comparing operands of different types in your code then something is wrong with the code.During Launch School we do not use it.
How does the unary plus (+) operator work?
It converts all string representations of numbers, booleans, and null to numbers. They can be integers, floats, hexadecimal, exponents, and Infinity.If it cannot return a number it will return NaN.
In this snippet what are the terms for.A. x and yB. + and =C. z
A. those are operandsB. those are operatorsC. that is the result
What is a unary operator?
A unary operator is one that takes a single operand/argument and performs an operation.
What is the general rule when using the binary + operator with a string and another type?
The non-string value will be converted to a string.
How does the binary + interact with operands that are numbers, booleans, undefined, null?
They’re converted to their equivalent numbers (0 / 1) and added together.
How does the binary + interact with the operands when one is an object? What kind of coercion is this?
They’re both converted to strings and concatenated together using implicit type coercion.
What are the relation operators? What two types are they used on?
> , =, <=They are defined for numbers and strings. There is no strict-equality version of these operators.
How do the relational operators interact with their operands when they are different types?
If one is a number, the other operand will be converted to a number as well.If they are both strings, they are compared lexicographically (alphabetically).
Should you alwaysuse explicit type coercion?
Yes.
When should you not use strict equality operators? (=== and !==)?
Never.
Is it acceptable to use the unary + to convert strings into numbers?
Yes!
Should you avoid using String() and toString() in template literals ${...}
?
Yes, don’t do that.
What is explicit type coercion?
Explicit type coercion occurs when a programmer intentionally uses one of the built-in functions and operators to coerce one type of value to another.
What does the Number() function do?
It attempts to convert the argument to a number. If it can’t convert the string to a number it returns the value NaN (which has the type of number).
How do you check the data type of a value?
use typeof, it doesn’t use any parentheses or camelCase which is weird.
What does this snippet return?
It returns 0. Number() returns 0 for both empty strings and strings containingonlywhitespace.
How does Number() interact with other types and objects?
Inconsistently, make sure you are accounting for the behavior in your program.
What do parseInt() and parseFloat() do? Do they work with multiple data types?
They convert strings to Integers / Floats. They exclusively work with strings.
How does parseInt() work?
It takes a string and attempts to parse an integer from it. The string must start with a numeric character for it to parse anything. If it receives a floating point (decimal) number it will return the integer to the left of the decimal.It can convert strings when there are non-numeric characters as well, as long as the string starts with a digit, optionally preceded by - or + :You can also pass parseInt a second argument called the radix. It specifies the base of the number contained in the string.10101 is 21 in binary, you’re setting the radix to 2 which means you’re using a base 2 number system (binary) to convert the string.
How does parseFloat() work?
It coerces a string to a number but it works with floating point numbers. It parses the numeric part of the string and stops once it hits a character that can’t be part of a number. It can return an integer.Like parseInt() the number in the string can optionally be preceded by a + or -.
How does the unary + operator work?
It works exactly the same as Number() but is more concise. Use Number() for clarity but it is acceptable to use the +.
How does toString() work? What data types does it not work on? What data type can you not call it directly on and why?
It returns a string representation of a value.You cannot use it on undefined or null.You cannot call it directly on a number type because the . that is required to call the method makes the interpreter think it is dealing with a floating point number.To work around this you can assign the number to a variable, surround the number with (), or use .. when calling the method.
When called on a boolean value, what does toString() return?
‘true’ / ‘false’
What happens when you call toString() on an array?
it returns a string version of the array with each element separated by commas.Any elements that are undefined or null within the array will be represented as empty values.
What value is returned when you call .toString() on an object?
‘[object Object]’
Does String() work the same as .toString()? How do they differ?
It works the same for the most part, you can however pass undefined/null to String() without causing the program to halt.
Why shouldn’t you use String() or toString() inside a template literal? {String(name)}
because the when you use a template literal it automatically returns a string so converting the string to a string is redundant.
When do you not use camelCase for names?
Only when you are naming a constant or a constructor function. Otherwise always use camelCase.
What is an idiomatic variable name?
A name that follows the commonly accepted conventions and standards.
What are non-idiomatic names for variables?
Names that, while syntactically correct, do not follow the conventions and standards typically followed by engineers. Examples:
In programming, what is a magic number and why should you avoid them?
It’s a number in your program that doesn’t have any information about why it’s there. It could be using a 5 for the amount of cards to deal or any number you use without context or info.You should use a constant whenever possible for these numbers.const CARDS_TO_DEAL = 5;
What is an example of using the expression assigned to a constant to be more explicit about what the number represents?
vs.
What do you use to indent code?
Always two spaces, never tabs.
When should you use semicolons?
Semicolon should terminate every line that is not a brace delimited block.
in if, for, and while statements why do you always put a space between the keyword and the open parenthesis that follows it?
To prevent confusion between built in statements and function calls.You should also always put a space between the closing parenthesis and opening brace.
When should you use one let to declare multiple variables?
Never, it is syntactically correct but the accepted standard for declaring variables is using one let per variable.
Can you ever mutate a constant? Should you?
Yes you can mutate a constant if you assign a pass-by-reference value to a constant.You shouldn’t ever mutate a constant though, that defeats the purpose of creating a constant.
What is the best practice for the length of a function?
Keep to around 10 lines, if it’s more than 15 lines or more, split it into multiple functions.
What is returned from a function if no return value is specified?
undefined
Is it best practice to log something significant (necessary for the program) and return a value from the same function?
No, the return value should be a separate function.
Should a function perform side effects and return a value?
No, the function should either have a side effect or return a value with no side effects but it should not do both.
What’s a major consideration you should make when naming a function?
whether the function has side effects or not, make sure the name gives insight into what the function is changing if it has a side effect.
why is returnTotal not a great function name?
If the function has no side effect, it must return something. We would just name it total as the fact that it is created to return a total is implied.
Which of these is a poor function name:
iterateThroughCards(), it is at a different level of abstraction than the other functions and deals with the actual nuts and bolts of the function (iterate).The name should just deal with what the function does in the context of the game, shuffle(), checkForWinner(), dealTwoCard() would all be fine.You shouldn’t include implementation details (programming jargon) in the function name.
why would we not use the above snippet with the snippet below?
We expect updateTotal() to mutate a value/values but not return anything. You need to split a function into multiple functions if it both mutates and returns a value.
What are convoluted functions with complex logic a sign of?
You don’t understand the problem well enough to break it down into well-compartmentalized pieces. As your understanding grows, your code should be easier to read and the intent should be easy to discern.
What’s wrong with this code?
it will throw an exception of ReferenceError: answer is not defined. We’re referencing answer before it exists.
What is the difference between a global variable and a local variable?
A global variable is available throughout a program whereas a local variable confined to a function.
What two things determine the scope of a variable?
The keyword you use to declare the variable and the location where you declare it.
What is a local variable?
Any variable declared inside a function or block is local. Everything else is global.
will the following code reassign greetingMessage?
Yes. You can reassign global variables from a local scope. If you had used let within the function it would have referenced the local greetingMessage and the global greetingMessage would have been unchanged.
Should your programs contain lots of global variables?
No, you should constrain the scope of your variables as much as possible. A smaller scope reduces the likelihood of of an outer scope misusing the varaible and makes bugs easier to track down.
What is a local variable? What determines its scope?
A variable declared within a function, they cannot be accessed outside the function they are declared in.A variables scope is determined by where it is declared.
Is greetingMessage a variable? What kind?
greetingMessage is a local variable initialized when an argument is passed into the function.Parameters have local scope within a function.
Do local variables persist through a programs life-cycle?
No, they go away when the function they are constrained to stops running. Every time a function is invoked a new scope is created, if the code within that scope declares a new variable, the variable belongs only to that scope. After the function finishes running the scope and any local variables is discarded.
Scope-wise, what is the difference between JavaScript in the browser to JavaScript in Node.js?
In Node.js a global variable is only available in the file/module you declare it in. To make global variables available across files/modules, you’ll have to explicitly import them in modules.
What is a global variable?
A variable that is available anywhere in a program, either globally or inside a function or block.
What are the two forms of local scope?
function scope and block scope
What is function scope?
Functions define a new scope for local variables. Think of a scope declared by a function as an inner scope. You can nest functions to create nested scopes. The scope of a variable is determined by where it is declared.
True / False : Outer scope variables can be accessed by the inner scope?
True! Outer scope variables can be accessed AND mutated/reassigned by inner scope variables.
True / False: Inner scope variable can be accessed by outer scope variables?
False, the outer scope has no access to inner scope variables.
Does a local variable exist before the function it is local to is invoked?
No, unless the function is called/invoked, the local variable will never be created. However, the scope is defined when the function declaration is created regardless of whether the function is called or not.
What level of scoping do functions at the same level of scoping have? Can they conflict with each other? What will calling funcB result in?Ex:
They are said to have peer scopes, they do not conflict with each other, funcB has no access to a so it will return a ReferenceError: a is not defined.
Can function reference variables created within other functions?
Only if they are nested within the function in which the variable is declared.
Do nested functions create their own variable scope?
Yes, they create another scope within the function. They can access variables outside of their scope as well.