Javascript Flashcards

1
Q

What is the purpose of variables?

A

to store data/information

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

How do you declare a variable?

A

with a variable’s keyword and a variable name (ex. var fullName)

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

How do you initialize (assign a value to) a variable?

A

by giving the variable a value using an assignment operator (usually =)

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

What characters are allowed in variable names?

A

Variable names cannot contain space
names must begin with a letter, underscore, or dollar sign
variable names can only contain letters, numbers, underscores, or dollar signs
variable names are case sensitive

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

What does it mean to say that variable names are case sensitive?

A

variables can store different values if using different casing even with the same word(s).

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

What is the purpose of a string?

A

store text data

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

What is the purpose of a number?

A

store numeric data

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

What is the purpose of a boolean?

A

act as an on/off switch to determine whether a script should run
aka makes decisions

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

What does the “=” operator mean in Javascript?

A

assignment operator

assigns a value to a variable

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

How do you update the value of a variable?

A

variableName = new value

you don’t need the variable keyword.

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

What is the difference between “null” and “undefined”?

A

undefined is the computer saying “nothing”

null is the programmer/human saying “nothing”

vacant lot versus parking lot

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

Why is it a good habit to include “labels” when you log values to the browser console?

A

provides clarity to other programmers as to where the values are coming from
good reminder for yourself as well

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

Give 7 examples of javascript primitives:

A
strings
numbers 
booleans
null 
undefined
bigint
symbols
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What data type is returned by an arithmetic operation?

A

numeric

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

What is string concatenation?

A

combining two or more strings together to make a new longer string
strings are immutable

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

What purpose(s) does the “+” operator serve in Javascript?

A

concatenate strings

add numbers together

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

What data type is returned by comparing two values (, ===, etc)?

A

boolean

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

what does the += operator do?

A

variable = variable + value

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

What are objects used for?

A

Helps us to store and collect data in pairs or groupings.

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

What are object properties?

A

key:value pairs

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

Describe object literal notation:

A

{}

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

How do you remove a property from an object?

A

use the delete keyword

delete object.property;

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

What are the two ways to get or update the value of a property of an object?

A

Dot notation object.property = “new value”;

Square bracket notation object[“property”] = “new value”

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

What are arrays used for?

A

storing list data

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

Describe array literal notation:

A

[ ]

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

How are arrays different from ‘plain’ objects?

A

arrays don’t have individually named indexes, only numeric indexes.

arrays have a property length that is constantly changing based on the number of values in an array

changing data in arrays is different than objects due to the multiple methods/functions we are able to use.

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

What number represents the first index of an array?

A

0

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

What is the length property of an array?

A

It stores the total number values in an array.

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

How do you calculate the last index of an array?

A

array.length - 1

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

what is a function in javascript?

A

a set of code that performs a task and is reusable.

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

Describe the parts of a function definition:

A
  1. keyword function
  2. name (optional)
  3. a comma separated list of parameters (optional), between ( )
  4. { indicating start of code block
  5. return statement (optional)
  6. } end of the code block for the function
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
32
Q

Describe the parts of a function call:

A
  1. functions name

2. comma separated list of arguments (optional) inside ( ).

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

When comparing them side-by-side, what are the differences between a function call and a function definition?

A

In a function call there is no code block or function keyword. There is also no return statement in a function call.

functionName( ); is a function call

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

What is the difference between a parameter and an argument?

A

we declare parameters

we pass arguments

parameters are essentially placeholders for arguments

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

Why are function parameters useful?

A

We can vary the results of the function by using parameters. Without parameters the function will return the same result each time it is called.

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

What two effects does a return statement have on the behavior of a function?

A
  1. Causes the function to produce a value we can use in our program
  2. Prevents any more code in the function’s code block from being run
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
37
Q

Why do we log things to the console?

A

to check to see if our output is correct. It is a debugging tool.

It is also a way to inspect our variables

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

What is a method?

A

A method is a function, which is technically a property of an object.

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

How is a method different from any other function?

A

a function can be called directly by its name

a method consists of a code that can be called by the name of its object and its method name using dot notation or square bracket notation

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

How do you remove the last element of an array?

A

the pop( ) method allows you to remove the last element from an array and returns that element

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

How do you round a number down to the nearest integer?

A

Math.floor( ) function returns the largest integer less than or equal to a given number.

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

How do you generate a random number?

A

Math.random( ) function returns a floating-point, pseudo-random number in the range between 0 to less than 1. (it can be 0 but it can’t be 1)

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

How do you delete an element from an array?

A

The splice ( ) method changes the contents of an array by removing or replacing existing elements.

arr.splice(0, 1, ‘feb’)

in the example above, we remove 1 index starting at the 0 index and replace it with the string ‘feb’.

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

How do you append an element to an array?

A

You can use the push ( ) method.

Append = add to the end

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

How do you break a string up into an array?

A

The split ( ) method divides a string into an ordered list of substrings and puts the substrings into an array, and returns the array

var.split(‘ ‘);

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

Do string methods change the original string? How would you check?

A

strings are immutable so you cannot modify the original string
inspect your values to see if it has changed

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

How many string methods are there roughly according to MDN?

A

45-50

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

Is the return value of a function or method useful in every situation?

A

No, a good example would be the push ( ) method as it returns the new length of the array. Not always is that useful information.

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

Roughly how many array methods are there according to MDN?

A

35 ish

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

Give 6 examples of comparison operators:

A
><
===
!==
>=
<=
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
51
Q

What data type do comparison expressions evaluate to?

A

Boolean

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

What is the purpose of an if statement?

A

to help make decisions when conditions are either met or not met

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

Is else required to use an if statement?

A

no

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

Describe the syntax structure of an if statement:

A

if (condition) {
statement1
}

condition = an expression that is considered to either be truthy or falsy
statement1 = statement that is executed if the condition is truthy

can have more than one statement

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

What are three logical operators?

A
logical AND (&&)
logical OR (||)
logical NOT (!)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
56
Q

How do you compare two different expressions in the same condition?

A

Using the logical AND or OR operators

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

What is the purpose of a loop?

A

it is a way to repeat something more than once without having to type it out again and again

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

What is the purpose of the condition expression in a loop?

A

it tells the loop when to stop when the condition is no longer true

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

What does iteration mean in the context of loops?

A

each iteration is a single time that the code block runs

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

When does the condition expression of the while loop get evaluated?

A

before each iteration to decide whether the loop should run again

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

When does the initialization expression of the for loop get evaluated?

A

at the beginning of the first iteration and not after. first step in the loop.

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

When does the condition expression of a for loop get evaluated?

A

before each iteration of the loop and after the initialization is evaluated

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

When does the final expression of a for loop get executed?

A

after the code block runs and before the condition for the next iteration is evaluated

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

Besides a return statement, which keyword exits a loop before its condition evaluates to false?

A

break statement

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

What does the ++ increment operator do?

A

adds one to its operand and returns a value

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

How do we iterate through the keys of an object?

A

use a for in loop

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

What is a “model”?

A

a replica of something

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

Which “document” is being referred to in the phrase Document Object Model?

A

the html document

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

What is the word “object” referring to in the phrase Document Object Model?

A

the data type object in javascript

javascript object that is modeling the html document

the DOM is not literally the html doc

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

What is the DOM tree?

A

a relationship tree of a parent element (node) and all of it child nodes and content within

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

Give 2 examples of document methods that retrieve a single element from the dom:

A

getElementById

querySelector

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

Give 1 example of a document method that retrieves multiple elements from the DOM at once:

A

querySelectorAll

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

Why might you want to assign the return value of a DOM query to a variable?

A

so you can access the value quickly later on if needed

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

What console method allows you to inspect the properties of a DOM element object?

A

console.dir() method

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

Why would the script tag need to be placed at the bottom of the HTML content instead of the top?

A

So we can let the html document load first

the elements need to exist before we apply and JS code to the content

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

What does document.querySelector() take as its argument and what does it return?

A

a css selector and returns only the first of the matching elements

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

What does document.querySelectorAll() take as its argument and what does it return?

A

a css selector in string form and returns a node list of all elements with the CSS selector that matches

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

Why do people use the “$” in javascript variables with DOM?

A

It is not a rule but is common to store dom values with a $ at the beginning of the variable.

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

What is the purpose of events and event handling?

A

to update the webpage and help the web page feel more interactive for the user

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

Are all possible parameters required to use a Javascript method or function?

A

no, depends on what you are trying to accomplish

81
Q

What method of element objects let you set up a function to be called when a specific type of event occurs?

A

addEventListener( )

82
Q

What is a callback function?

A

is a function passed into another function as an argument, which is then invoked inside the outer function to complete some kind of routine or action

83
Q

What object is passed into an event listener callback when the event fires?

A

the event object with numerous properties and values describing the event that took place.

84
Q

What is the “event.target”? If you weren’t sure, how would you check? Where could you get more information about it?

A

MDN for source

the target property is a property of the event object that points towards where the event took place

85
Q

What is the difference between:

element.addEventListener(‘click’, handleclick)

and

element.addEventListener(‘click’, handleclick( ))

A

The second one would execute the function as the page loads rather than when the event fires.

the first’s function will fire only when the event occurs

86
Q

What is the className property of element objects?

A

the className property of the element interface gets and sets the value of the class attribute of the specified element.

87
Q

How do you update the CSS class attribute of an element using Javascript?

A

using the dom queried element and the className property

$variable.className = ‘new-value’

88
Q

What is the textContent property of element objects?

A

represents the text content of the node and its descendants

89
Q

How do you update the text within an element using Javascript?

A

using the dom queried element and the textContent property

$variable.textContent = ‘new-value’

90
Q

Is the event parameter of an event listener callback always useful?

A

no, but you do need to use the event parameter so that you know you don’t need the developer to call the function.

91
Q

Would this assignment be simpler or more complicated if we didn’t use a variable to keep track of the number of clicks?

A

more complicated.

Try not to store data on the dom elements and instead in a javascript variable

92
Q

Why is storing information about a program in variables better than only storing it in the DOM?

A

It is easier and more efficient to use javascript to store data

93
Q

What event is fired when a user places their cursor in a form control?

A

focus

94
Q

What event is fired when a user’s cursor leaves a form control?

A

blur

95
Q

What event is fired when a user changes the value of a form control?

A

input

96
Q

What event is fired when a user clicks the “submit” button within a form?

A

submit

97
Q

What does the event.preventDefault() method do?

A

prevents the default event from occurring, which can change from event to event

98
Q

What property of a form element object contains all of the form’s controls?

A

the “elements” property

99
Q

What property of a form control object gets and sets its value?

A

value property (used .value)

100
Q

What is one risk of writing a lot of code without checking to see if it works so far?

A

Spend more time writing code that doesn’t work and is now more confusing to know where your bug(s) may be.

101
Q

What is an advantage of having your console open when writing a javascript program?

A

able to check your work and debug as you go

102
Q

does the document.createElement() method insert a new element into the page?

A

no

103
Q

How do you add an element as a child to another element?

A

Use the appendChild( ) method

anotherElement.appendChild(currentElement)

104
Q

What do you pass as the arguments to the element.setAttribute( ) method?

A

the first argument is the javascript string of the name of the attribute
the second is the javascript string value of the attribute

105
Q

What steps do you need to take in order to insert a new element into the page?

A

create the new element and store it in a variable
create a text node and store it in a variable
attach the new text node to the new element
select the parent element from the DOM with querySelector()
insert the new element into its position (parent.appendChild(newElement)

106
Q

What is the textContent property of an element object for?

A

allows programmers to access the text within an element to see it or modify the text content.

107
Q

Name two ways to set the class attribute of a DOM element:

A

element. className

element. setAttribute(‘class’, ‘value’)

108
Q

What are two advantages of defining a function to create something (like the work of creating a DOM tree)?

A

it is easier to test

re-use the code more easily

109
Q

What is the event.target?

A

is a reference to the object onto which the event was dispatched.

110
Q

Why is it possible to listen for events on one element that actually happen to its descendent elements?

A

Due to event flow

Event bubbling is the most widely accepted event flow and it goes from most specific node outwards

111
Q

What DOM element property tells you what type of element it is?

A

tagName property

112
Q

What does element.closest() take as its argument?

What does it return?

A

string version of a CSS selector that identifies the element

returns the closest ancestor of the selected element

113
Q

How can you remove an element from the DOM?

A

element.remove( ) method

114
Q

If you wanted to insert aa new clickable DOM element into the page using Javascript, how could you avoid adding an event listener to every new element individually?

A

use an event listener on the parent element

115
Q

What is the event.target?

A

the element that triggered the event

116
Q

What is the affect of setting an element to display: none?

A

No longer visible and is removed from the document flow

117
Q

What does the element.matches( ) method take as its argument and what does it return?

A

CSS selector

boolean value saying whether or not the element “is” the selector

118
Q

How can you retrieve the value of an element’s attribute?

A

element.getAttribute(‘name of attr’)

119
Q

At what steps of the solution would it be helpful to log things to the console?

A

all steps of the solution

120
Q

If you were to add another tab and view to your html, but you didn’t use event delegation, how would your javascript code be written instead?

A

you would have to write event handler functions for each individual tabs

121
Q

If you didn’t use a loop to conditionally show or hide the views in the page, how would your javascript code be written instead?

A

We would have to add a new if statement for every new tab.

122
Q

How do you store data in local storage?

A

using localStorage.setItem(‘keyname’, ‘keyvalue’)

123
Q

How do you retrieve data from localStorage?

A

localStorage.getItem(‘keyname’)

124
Q

What data type can localStorage save in the browser?

A

string only

Which is why JSON is important

125
Q

When does ‘beforeunload’ event fire on the window object?

A

the event is fired when the window, the document, and its resources are about to be unloaded (close the web page or refresh the web page).

The document is still visible and the event is still cancelable at this point.

126
Q

What is a method?

A

Is a function, which is a property of an object.

127
Q

How can you tell the difference between method definition and method call?

A

Definition:
function assigned to property
function keyword
code block

Method Call:
dot notation
arguments

128
Q

Method definition syntax structure:

A

methodName: function ( ) {
code block
}

129
Q

method call syntax:

A

object.method(arguments)

130
Q

How is a method different from any other function?

A

You cannot call a method function without the object it is part of using dot notation.

functions can be called by just using the function name.

131
Q

What is the defining characteristic of Object Oriented Programming (OOP)?

A

objects can contain both data (as properties) and behavior (as methods).

132
Q

What are the 4 principles of Object Oriented Programming?

A

Abstraction
Encapsulation
Inheritance
Polymorphism

133
Q

What is abstraction?

A

Being able to work with complex things in simple ways.

example = light switch, simple action with complex process

Knowing what something does without knowing how it does it.

134
Q

What does API stand for?

A

Application Programming Interface

135
Q

What is the purpose of an API?

A

an API acts to connect computers or pieces of software to each other.

It is essentially software abstraction

136
Q

What is ‘this’ in JS?

A

reference to an object that the behavior being executed is within

137
Q

What does it mean to say that ‘this’ is an “implicit parameter”?

A

this is available in a function’s code block even though it was never included in the function’s parameter list or declared with var.

138
Q

When is the value of ‘this’ determined in a function? Call time or definition time?

A

Call time

139
Q

What does this refer to in the following code snippet?

var character = {
  firstName: 'Mario',
  greet: function () {
    var message = 'It\'s-a-me, ' + this.firstName + '!';
    console.log(message);
  }
};
A

Nothing, because character.greet( ) has not been called yet

140
Q

Given the above character object, what is the result of the following code snippet? Why?
character.greet();

A

This = character object

Why? Because the method was called and so the code will be executed.

141
Q

Given the above character object, what is the result of the following code snippet? Why?

var hello = character.greet;
hello();
A

hello has now been assigned the value of the greet method of the character object.

this will be undefined and return: It’s a me, undefined.

a variable is actually a property of the window. window.hello( )

window doesn’t have a firstName property so the value will be undefined

142
Q

How can you tell what the value of ‘this’ is for a particular function or method call?

A

The value of ‘this’ is whatever is on the left side of the dot

object.method( )

143
Q

What kind of inheritance does the JS programming language use?

A

Prototype-based inheritance

style of OOP in which behavior reuse is performed via a process of reusing existing objects that serve as prototypes

JS objects give certain behaviors (methods) and/or data (properties) to other objects

144
Q

What is a prototype in JS?

A

an original object on which other objects are patterned

145
Q

How is it possible to call methods on strings, arrays, and numbers even though those methods don’t actually exist on objects, arrays, and numbers?

A

thanks to the prototype!

146
Q

If an object does not have it’s own property or method by a given key, where does JS look for it?

A

On the prototype object

147
Q

What does the ‘new’ operator do?

A

creates an instance of an object that has a constructor function.

148
Q

What property of a Javascript function can store shared behavior for instances created with new?

A

prototype

149
Q

What does ‘instanceof’ operator do?

A

The instanceof operator tests to see if the prototype property of a constructor appears anywhere in the prototype chain of an object. Then it returns a boolean value.

150
Q

What steps are executed when using the ‘new’ keyword?

A
  1. Creates a blank, plain JS object
  2. Adds a property to the new object (__proto__) that links to the constructor function’s prototype object
  3. Binds the newly created object instance as the ‘this’ context (all references to ‘this’ in the constructor function now refer to the object created in the first step)
  4. returns ‘this’ if the function doesn’t return an object
151
Q

What is a ‘callback’ function?

A

It is a function that passes another function as an argument

152
Q

Besides adding an event listener callback function to an element or the document, what is one way to delay the execution of a JS function until some point in the future?

A

setTimeout ( ) function

setInterval ( ) function

153
Q

How can you set up a function to be called repeatedly without using a loop?

A

setInterval( ) function

154
Q

What is the default time delay if you omit the delay parameter from either setTimeout or setInterval methods?

A

0, it executes immediately

155
Q

What do setTimeout () and setInterval () return?

A

timeoutID

intervalID

156
Q

What is AJAX?

A

Ajax is a technique used for loading data into part of a page without having to refresh the entire page. It loads data asynchronously.

Data is often sent in the JSON format.

157
Q

What does AJAX stand for?

A

Asynchronous Javascript And XML

However, it is now used to refer to a group of technologies that offer asynchronous functionality in the browser.

158
Q

Which object is built into the browser for making HTTP requests in JS?

A

XMLHttpRequest

159
Q

What event is fired by XMLHttpRequest objects when they are finished loading the data from the server?

A

load

160
Q

An XMLHttpRequest object has an addEventListener( ) method just like DOM elements.

How is it possible that they both share this functionality?

A

prototype inheritance

Both have the same prototype object

161
Q

What is a code block? What are some examples of code block?

A

the block is delimited by a pair of curly braces

a structure of source code that is grouped together

Blocks consist of one or more declarations and statements

162
Q

What does the block scope mean?

A

It means variables introduced within a block are scoped to the containing function or script. They don’t persist beyond the block itself.

163
Q

What is the scope of a variable declared with const or let?

A

Block-scope instead of global or function-scope

164
Q

What is the difference between const and let?

A

const variables CANNOT be reassigned or redeclared.

Must initialize a const variable at the same time as when it is declared.

165
Q

Why is it possible to .push( ) a new value into a const variable that points to an array?

A

due to the fact that the actual value to which the const variable is referencing is not immutable.

we can use a method to change the value of the array, however, we cannot change the value like this:

colors = [‘red’, ‘green’]

we can use:

colors.push(‘green’)

166
Q

How should you decide on which type of declaration to use?

A

use const unless you can’t (prefer const)
simple code doesn’t use reassignment.
Reassignment can make code difficult to understand

167
Q

What is the syntax for writing a template literal?

A

string —–> those are called backticks

168
Q

What is “string interpolation”?

A

substituting part of the string in a template literal for values of variables or expressions.

To instruct JS to substitute a variable or expression use: ${variable}

169
Q

What is destructuring? (conceptually)

A

Extracting data from arrays, objects, and maps and setting them into their own variables.

170
Q

What is the syntax for Object destructuring?

A

let or const {property1: variable1, property2: variable2} = object;

if the variables have the same names as the properties you can simplify this further
let {firstName, lastName} = person

171
Q

What is the syntax for Array destructuring?

A

let/const [x, y] = arrayName

comma separated list of elements

172
Q

How can you tell the difference between destructuring and creating Object/Array literals?

A

curly braces or square brackets will be on the left side of the assignment operator instead of the right side when destructuring

173
Q

What is the syntax for defining an arrow function?

A

(parameter(s)) => either a return expression or a code block

parameters are optional
1 parameter doesn’t require parentheses
2 or 0 parameters require curly braces

if curly brace are used in the code block then you need to use a return statement as well

174
Q

When an arrow function’s body is left without curly braces, what changes in its functionality?

A

You don’t have to use return in order to return the result of the expression
With curly braces you need to use a return statement.

175
Q

How is the value of “this” determined within an arrow function?

A

Arrow functions capture the “this” value of the enclosing context instead of creating its own “this” context

Kind if like inheriting it

176
Q

When is the value of “this” determined with an arrow function?

A

When the arrow function is defined instead of when it is being called.

177
Q

What is a promise?

A

The “promise” object represents the eventual completion (or failure) of an asynchronous operation and its resulting value

178
Q

What are the three states a Promise can be in?

A
  1. pending = initial state, neither fulfilled or rejected
  2. fulfilled = meaning that the operation was completed successfully
  3. rejected = meaning the operation failed
179
Q

How do you handle the fulfillment of a Promise?

A

Promise.prototype.then( )

180
Q

How do you handle the rejection of a Promise?

A

Promise.prototype.catch ( )

181
Q

What is Array.prototype.filter useful for?

A

creating a new array of values that pass some condition/test from an old array

182
Q

What is Array.prototype.map useful for?

A

create a new array from transforming the original array

183
Q

What is Array.prototype.reduce useful for?

A

Get a single value from a group of data

184
Q

What is ‘syntactic sugar’?

A

syntax that is designed to make things easier to read or to express.

185
Q

What is the typeof an ES6 class?

A

function

186
Q

Describe ES6 class syntax:

A

class className { }

187
Q

What is refactoring?

A

process of restructuring existing computer code without changing its external behavior (preserve functionality).

188
Q

do class functions have code blocks?

A

No, they have class bodies

189
Q

How are ES modules different from CommonJS modules?

A

ES is part of the JS language officially and CommonJS is not
- require will not work in the browser
use import instead of require
use export instead of export.module

190
Q

What kind of modules can Webpack support?

A

ECMAScript modules
CommonJS modules
AMD modules

191
Q

What is a JavaScript closure?

A

it is a combination of a function bundled together (enclosed) with references to its surrounding state (lexical environment).

A closure gives you access to an outer function’s scope from an inner function.

192
Q

What must the return value of myFunction be if the following expression is possible?
myFunction()();

A

function

193
Q
What does this code do?
const wrap = value => () => value;
A

returns a function

194
Q

In JS, when is a function’s scope determined? When it is called or at definition?

A

when it is defined

195
Q

What allows JS functions to “remember” values from their surroundings?

A

closures

196
Q

Create a random number between 0 and 100:

A

const randomNum = Math.floor(Math.random() * 100)

197
Q

Create a random number between 2 and 10, including 2 and 10

A
const randomNum = Math.Floor(Math.random() * (10 - 2 + 1)) + 2
const randomNum = Math.Floor(Math.random() * (max - min + 1)) + min
198
Q

sort an array of numbers from smallest to largest

A
const sortedArr = array.sort(function (a, b) { 
return a - b})