Week 1 Flashcards
What does console.log(…) do in the Node.js environment?
Prints the given argument to the standard output, or terminal
It outputs to the stream known as STDOUT
What is the difference to fork and clone a code repository?
Fork copies the repository on the server, and clone copies it to the local disk
Fork is an action used on GitHub to copy a repo to your account
What will be logged to the console?
var globalScope = ‘I'm all up in your codes!’;
if (true) {
let globalScope = ‘Not anymore, you're not!’;
}
console.log(globalScope);
I’m all up in your codes!
The let declaration is scoped to it’s containing block, which means that our console.log() ignores it completely.
What will be logged to the console?
x = 12;
if(true) {
x + 2;
x = 4;
}
console.log(x);
4
x is set as a global variable, and is not contained inside any function or block scope. It is overwritten inside the conditional statement, and retains it’s new value when the block closes.
What will the following code output to the console?
undefined
E.T.
friend is hoisted to the top but given no value, therefore the first console.log will say undefined. Then, the value “E.T.” is set to friend before the second console.log which will output “E.T.”.
The functions createUser, or sendUserData are examples of which naming convention?
Camel Case
What is meant by type coercion?
When the operands of an operator are different types, one of them will be converted to an equivalent value of the other operand’s type
an example is the comparison operator == in JavaScript
What is meant by type coercion?
When the operands of an operator are different types, one of them will be converted to an equivalent value of the other operand’s type
an example is the comparison operator == in JavaScript
What would the following JavaScript code evaluate to? Why?
‘0’ == false
true
‘0’ and false would be coerced to a common type and evaluate to be equal (or true)
Would the following JavaScript code evaluate to true or false? Why?
‘1’ === 1
false because the variable types are different
Which of the following is not a primitive data type in JavaScript? Boolean, Array, Number, String?
arrays are a type of Object in JavaScript, not a primitive
Name a valid way to access an object value?
myObject[‘key’]
you can access object values using square bracket notation
myObject.key
you can access values using .key notation
Are primitive data types the same across most programming languages?
No, primitives are a defining feature of a programming language
Primitives are similar, but often unique to a given programming language
Is the following code a valid Object in JavaScript?
var myObjected = {
‘key-1’: 42,
keyB: ‘value B’,
‘keyC’: [1, 2, 3]
};
Correct, the object would work despite inconsistent key definitions
Given the following object, how would you access the first borough, Manhattan (using JavaScript)?
var nyc = {
name: ‘New York City’,
boroughs: [
‘Manhattan’,
‘Queens’,
‘Brooklyn’,
‘The Bronx’,
‘Staten Island’],
population: 8491079,
area_codes: [212, 347, 646, 718, 917, 929],
position: { lat: 40.7127, lng: -74.0059 }
}
nyc[‘boroughs’][0]
This would return the first element of the boroughs array
In programming, what is meant by the term First-class citizen (or First-class object)?
An object with no restrictions on its creation, destruction, or usage
This includes the ability to be passed as an argument, returned from a function, and assigned to a variable
Can functions act as objects in JavaScript?
Correct, functions are instances of the Object type in JavaScript and can have properties and methods just like any other object
Considering the following JavaScript code, is it possible to assign attributes to a function?
const funObject = function() {
console.log(‘I am function.’);
}
funObject.someAttribute = 42;
It is possible because JavaScript functions are also Objects
Functions can be assigned attributes like any other object
Which of the following is the best definition of a Higher-order function?
A function that takes a function as an argument, or returns a function as a result
Higher-order functions make use of other functions either as arguments or by returning them
What is the rationale behind using Higher-order functions?
They allow for creation of more powerful and generalized functions
A Higher-order function’s job is reduced in scope when you allow it to interact with other functions
define an anonymous function
A nameless function defined inline or assigned to a variable. It may or may not be used as a callback function
Anonymous functions can be assigned to variables or simply passed in to another function directly (inline)
Given the following JavaScript code, what will be the resulting value of url?
let PORT = 8080;
let url = http://localhost:${PORT}/path
;
http://localhost:8080/path
the port will be interpolated into the template literal
Considering the following JavaScript code, what will the console output be and why?
const foo = function() {
var x = 1;
if (x === 1) {
let y = 2;
}
console.log(‘Value of y is ‘ + y);
}
foo();
ReferenceError
the block-scoping of let y = 2 makes it available only within the if statement block
Consider the following JavaScript code, what would be the console output?
const names = [‘Graham’];
names = [];
console.log(names)
Console would show a TypeError
Assignment to constant variable is not possible due to the declaration using const
Given the following JavaScript code, what would be the console output?
const names = [];
names.push(‘Jordan’);
console.log( names );
Console would show [‘Jordan’]
it still allows us to change the value of the referenced variable
How are Arrow Functions () => {} different than traditional function expressions?
Arrow Functions do not declare their own scope
arrow functions inherit parent scope
What are recursive functions?
A recursive function can stop calling itself if it doesn’t have a base case.
Every recursive function must stop calling itself at some point using a base case, otherwise it will just continue to call itself forever, like an infinite loop.
Take a look at the following recursive function:
function countEvenToTwelve(number) {
if (number <= 12) {
console.log(number);
countEvenToTwelve(number+2);
}
}
what is the recursive case?
number <= 12 is the recursive case.
When this is true, the function will call itself again.
What purpose does the require() method serve in JavaScript?
It imports functionality (or data) from another module
it allows us to import functions, variables, and objects that are exported by other modules
To which directory does NPM install dependencies?
./node_modules
Correct, each dependency has its own subdirectory in the node_modules directory
What are the differences between packages, modules, and libraries?
Library: an independent collection of code that can be used by programs (not JS specific) - Module: JS code in a separate file, that can be required by other JS programs - Package: a collection of JS modules, with a package.json, usually published on NPM
Should the node_modules directory be committed to your project’s git repo?
no
Should the package.json file be committed to a project’s git repo?
yes
How can you empty an array in JavaScript?
There are actually a few methods to do this. Lets say you have the following array let array = [1, 2, 3, 4], you can empty it by:
// creates a brand new empty array
array = [];
// clears the array without creating a copy
array.length = 0;
// modifies the array and returns the removed entries
array.splice(0, array.length);
Explain the difference between == and ===?
In JavaScript, there are two sets of equality operators. The triple-equal operator === behaves like any traditional equality operator would: evaluates to true if the two expressions on either of its sides have the same type and the same value. The double-equal operator, however, tries to coerce the values before comparing them. It is therefore generally good practice to use the === rather than ==. The same holds true for !== vs !=.
What are the six JavaScript data types?
Boolean
Number
String
Null
Undefined
Symbol
What would be the result of 3+2+”7”?
Since 3 and 2 are integers, they will be added numerically. And since 7 is a string, it will resort to string concatenation, resulting in a string type. The result would be “57”.
Explain JavaScript’s for..in loop.
The for-in loop is used to loop through the properties of an object. More specifically, it loops through each key.
The syntax for the for-in loop is:
for (let key in object) {
// statement or block to execute for each key
}
In each repetition, one property from the object is associated to the variable (named key above), and the loop is continued until all of the properties of the object are depleted.
In this example, what does this evaluate to when we run thing.daadaa()?
const thing = {
doodoo: “Boo”,
daadaa: function() {
console.log(this);
}
};
The thing object
When you use this inside a method, this refers to the object that the method was called on.
const dog = {
sound: “woof”,
speak: function() {
console.log(this.sound);
}
};
dog.sound = “🐶”
dog.speak();
🐶
The sound property is what is being logged to the console. sound also gets changed to 🐶 before being logged out.