Week 1 Flashcards

1
Q

What does console.log(…) do in the Node.js environment?

A

Prints the given argument to the standard output, or terminal
It outputs to the stream known as STDOUT

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

What is the difference to fork and clone a code repository?

A

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

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

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);

A

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.

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

What will be logged to the console?
x = 12;
if(true) {
x + 2;
x = 4;
}
console.log(x);

A

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.

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

What will the following code output to the console?

A

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.”.

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

The functions createUser, or sendUserData are examples of which naming convention?

A

Camel Case

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

What is meant by type coercion?

A

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

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

What is meant by type coercion?

A

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

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

What would the following JavaScript code evaluate to? Why?

‘0’ == false

A

true

‘0’ and false would be coerced to a common type and evaluate to be equal (or true)

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

Would the following JavaScript code evaluate to true or false? Why?

‘1’ === 1

A

false because the variable types are different

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

Which of the following is not a primitive data type in JavaScript? Boolean, Array, Number, String?

A

arrays are a type of Object in JavaScript, not a primitive

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

Name a valid way to access an object value?

A

myObject[‘key’]
you can access object values using square bracket notation

myObject.key
you can access values using .key notation

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

Are primitive data types the same across most programming languages?

A

No, primitives are a defining feature of a programming language

Primitives are similar, but often unique to a given programming language

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

Is the following code a valid Object in JavaScript?

var myObjected = {
‘key-1’: 42,
keyB: ‘value B’,
‘keyC’: [1, 2, 3]
};

A

Correct, the object would work despite inconsistent key definitions

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

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 }
}

A

nyc[‘boroughs’][0]

This would return the first element of the boroughs array

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

In programming, what is meant by the term First-class citizen (or First-class object)?

A

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

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

Can functions act as objects in JavaScript?

A

Correct, functions are instances of the Object type in JavaScript and can have properties and methods just like any other object

18
Q

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;

A

It is possible because JavaScript functions are also Objects

Functions can be assigned attributes like any other object

19
Q

Which of the following is the best definition of a Higher-order function?

A

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

20
Q

What is the rationale behind using Higher-order functions?

A

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

21
Q

define an anonymous function

A

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)

22
Q

Given the following JavaScript code, what will be the resulting value of url?

let PORT = 8080;
let url = http://localhost:${PORT}/path;

A

http://localhost:8080/path

the port will be interpolated into the template literal

23
Q

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();

A

ReferenceError

the block-scoping of let y = 2 makes it available only within the if statement block

24
Q

Consider the following JavaScript code, what would be the console output?

const names = [‘Graham’];
names = [];
console.log(names)

A

Console would show a TypeError

Assignment to constant variable is not possible due to the declaration using const

25
Q

Given the following JavaScript code, what would be the console output?
const names = [];
names.push(‘Jordan’);
console.log( names );

A

Console would show [‘Jordan’]

it still allows us to change the value of the referenced variable

26
Q

How are Arrow Functions () => {} different than traditional function expressions?

A

Arrow Functions do not declare their own scope
arrow functions inherit parent scope

27
Q

What are recursive functions?

A

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.

28
Q

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?

A

number <= 12 is the recursive case.

When this is true, the function will call itself again.

29
Q

What purpose does the require() method serve in JavaScript?

A

It imports functionality (or data) from another module

it allows us to import functions, variables, and objects that are exported by other modules

30
Q

To which directory does NPM install dependencies?

A

./node_modules

Correct, each dependency has its own subdirectory in the node_modules directory

31
Q

What are the differences between packages, modules, and libraries?

A

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

32
Q

Should the node_modules directory be committed to your project’s git repo?

A

no

33
Q

Should the package.json file be committed to a project’s git repo?

A

yes

34
Q

How can you empty an array in JavaScript?

A

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);

35
Q

Explain the difference between == and ===?

A

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 !=.

36
Q

What are the six JavaScript data types?

A

Boolean
Number
String
Null
Undefined
Symbol

37
Q

What would be the result of 3+2+”7”?

A

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”.

38
Q

Explain JavaScript’s for..in loop.

A

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.

39
Q

In this example, what does this evaluate to when we run thing.daadaa()?

const thing = {
doodoo: “Boo”,
daadaa: function() {
console.log(this);
}
};

A

The thing object

When you use this inside a method, this refers to the object that the method was called on.

40
Q

const dog = {
sound: “woof”,
speak: function() {
console.log(this.sound);
}
};

dog.sound = “🐶”
dog.speak();

A

🐶

The sound property is what is being logged to the console. sound also gets changed to 🐶 before being logged out.