JavaScript Flashcards

1
Q

What is the purpose of variables?

A

To store bits of data that the computer needs in order to do its tasks

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 the keyword “var” followed by the identifier or variable name

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

with the assignment operator (=)

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

letters of the alphabet, dollar sign ($), underscore, numbers (can’t start with number)

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

e.g. the variables “test” and “Test” are different.

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

sequence of characters that represent text

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

represents a numeric data type that allows us to perform mathematical operations on it

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

logical data type that only returns true or false. Useful for conditionals. Lets the computers ultimately decide what to do or not do.

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 the value of the right to whatever is on the left.

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

the assignment operator can update the value of a variable. var keyword not necessary to update value

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: JavaScript’s method of saying “empty”
null: developer’s way of saying “empty”; assigned intentionally

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

Shows the user which logs represent which values. Logs without labels can cause confusion

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

Give five examples of JavaScript primitives

A

string, number, bigint, boolean, undefined, symbol, null

primitive: data that is not an object and has no methods. primitives can NOT be altered

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

number

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

What is string concatenation?

A

using + to join together multiple things into a single string

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

What purposes does the plus (+) operator serve in JavaScript?

A
  • addition

- string concatenation

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 += “plus-equals” operator do?

A

adds/concatenates the value on the right with the value on the left and then assigns it to the variable on the left.

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

What are objects used for?

A

to group relevant data together

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

What are object properties?

A

variables that live within an object

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

Describe object literal notation

A

declare a variable and assign it a value of a curly brace block. The contents of the curly brace block can be empty or consist of key, value pairs

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

Using the delete operator

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?

A

dot notation (object.property = value)

bracket notation (object[‘property’] = value)

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

What are arrays used for?

A

Useful when working with lists or groups of similar data

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

Describe array literal notation

A

var varName = [list contents (separated by comma)]

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

values assigned to index numbers rather than keys

order is preserved in arrays

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

returns how many things are stored in the list

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 block of code that has a specific purpose and can be reused as many times as needed

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

keyword ‘function’ followed by optional name, zero to however many parameters, code block, optional return statement

function name(parameters,..) {
     return (optional)
}
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

functionName(arguments);

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

a function call doesn’t need the code block, just function name followed by any arguments

function definition needs the initial code block and has parameters

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

Parameter is for function definition

argument is for function calls

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

Why are function parameters useful?

A

Allows the function to be dynamic

Can achieve different results depending on the data passed in

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

exits the code block immediately after executing a return line. (ends the function)

causes function to produce a value we can use in the program

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

Why do we log things to console?

A

In order to reduce confusion by keeping track of variables and making sure the data output is what we expected

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

What is a method?

A

function that is 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

They are properties of an object so they must be accessed as if they were a property (dot 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 from an array?

A

array.pop( ); returns the removed element from the array

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

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( ); Returns a number from 0 (inclusive) to 1 (not inclusive)

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

array.splice(n, x, y,…);

n: starting index
x: number of elements to remove (optional)
y: elements to add to the array (optional)

returns an array of the deleted elements

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

array. unshift( ); adds to beginning of array

array. push( ); adds to end of an array

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

string.split(separator);

separator describes where the split should occur

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 to check if not sure?

A

No, can test in console by trying to change a string then console log the original string

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

Roughly how many string methods are there according to MDN Web docs?

A

37

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

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 the MDN Web docs?

A

34

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

What 3 letter acronym should you always include in your Google search about a JavaScript method or CSS property?

A

MDN

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

Give 6 examples of comparison operators

A

, <=, >=, ===, !==

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
52
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
53
Q

What is the purpose of an if statement

A

Lets us choose what to do

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

Is else required in order to use an if statement?

A

no

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

Describe the syntax of if statement

A

if (conditional) {
code block
}

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

What are the three logical operators?

A

&& (and)
|| (or)
! (Logical not; inverts boolean)

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

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

A

with logical operators (&& or ||)

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

What is the purpose of a loop?

A

When you want to repeat a chunk of code while certain conditions aren’t met

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

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

A

It makes it so loops have an exit point (not infinite loop)

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

What does “iteration” mean in the context of loops?

A

Each time the computer passes through the loop’s code block

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

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

A

before each iteration

62
Q

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

A

before everything (first thing that happens in a for loop)

63
Q

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

A

before each iteration

64
Q

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

A

after each iteration

65
Q

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

A

break

66
Q

What does the ++ increment operator do?

A

increments the variable by 1

67
Q

How do you iterate through the keys of an object

A

with the for.. in loop

for key in object { }

68
Q

What is the event.target?

A

The element that the user directly interacted with

69
Q

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

A

It is taken out of the document flow

70
Q

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

A

element.matches(selector)

returns boolean

71
Q

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

A

element.getAttribute(attributeName)

72
Q

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

A

Every step

73
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

Would have to add an event listener for each tab/view element.

74
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

have to query each element and set their className manually

75
Q

What is JSON?

A

JavaScript Object Notation
Takes an object/array/data and converts it into a string which can then be easily transmitted through network and later deserialized in order to access the data in its original state

76
Q

What are serialization and deserialization?

A

serialization: take data and turn it into a stream of bytes which can be easily transferred
deserialization: take a stream of bytes and turn it into a data (allows us to use data)

77
Q

Why are serialization and deserialization useful?

A

Can easily transfer data through network

Data is stored in memory locations so can’t easily transfer data from system to system.

78
Q

How do you serialize a data structure into a JSON string using JavaScript?

A

using JSON.stringify(data)

79
Q

How do you deserialize a JSON string into a data structure using JavaScript?

A

JSON.parse(JSON-string)

80
Q

How do you store data in localStorage?

A

with storage.setItem(‘key’, ‘value’)

81
Q

How do you retrieve data from localStorage?

A

storage.getItem(‘key’)

Returns value of key

82
Q

What data type can localStorage save in the browser?

A

Strings

83
Q

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

A

Right before the page is refreshed/closed

84
Q

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

A

method definition: inside an object, name is listed as a normal property, but the value being assigned to it is an anonymous function definition

method call: called as the property of an object, but with parentheses

85
Q

Describe method definition syntax

A

defined inside an object as a normal property, but value assigned is an anonymous function definition

86
Q

Describe method call syntax

A

similar to property of an object, but with parentheses containing any arguments

87
Q

How is a method different from any other function?

A

They can only be called on an object which contains the method

88
Q

What is the defining characteristic of Object-Oriented Programming?

A

It relies on the procedures and methods within an object to modify the properties and data within itself.

groups data and behavior (methods) together

89
Q

What are the four “principles” of Object-Oriented Programming?

A

Abstraction
Encapsulation
Inheritance
Polymorphism

90
Q

What is “abstraction”?

A

simplifying a complex thing

91
Q

What does API stand for?

A

Application Programming Interface

92
Q

What is the purpose of an API?

A

Used as tools that do specific things which simplify the process

93
Q

What is “this” in JavaScript?

A

an implicit parameter whose value is determined by whenever the function is called

94
Q

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

A

It is available in a function’s code block as a parameter despite not being explicitly given

95
Q

When is the value of “this” determined in a function?

A

whenever the function is called

96
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 at the moment

97
Q

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

character.greet();

A

“It’s-a-me, Mario!”

this refers to the character object and it will get the firstName property of the character object

98
Q

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

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

“It’s-a-me, undefined!”

hello stores the character.greet method, but when it is called, ‘this’ refers to the window object as there is no dot to the left of hello. Since there is no firstName property of window, it is undefined.

99
Q

How can you tell what the value of ‘this’ will be for a particular function or method definition?

A

It is unknown on function definition

100
Q

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

A

this refers to whatever object or function calls it

101
Q

What kind of inheritance does the JavaScript programming language use?

A

prototype-based

102
Q

What is a prototype in JavaScript?

A

an object that contains the methods which we can use through inheritance

103
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

They all acquire their methods through the prototype object via inheritance

104
Q

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

A

in the prototype object

105
Q

What does the new operator do?

A

lets developers create an instance of a user-defined object type or one of the built-in types that has a constructor function

1) Creates a blank, plain JS Object
2) Adds __proto__ property that points to constructor function’s prototype object
3) all references to ‘this’ refer to the newly created object
4) returns this if function doesn’t return an object

106
Q

What property of JavaScript functions can store shared behavior for instances created with new?

A

an object’s prototype property

107
Q

What does the instanceof operator do?

A

tests to see if the prototype property of a constructor appears anywhere in the prototype chain of an object.

object instanceof constructor

AKA was this object created by this constructor function

108
Q

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

A

setTimeout( )

setInterval( )

109
Q

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

A

setInterval( )

110
Q

What is the default time delay if you omit the delay parameter from setTimeout( ) or setInterval( )?

A

default time delay is 0

111
Q

What do setTimeout( ) and setInterval( ) return?

A

a positive integer ID which identifies the setTimeout( ) or setInterval( ) call

112
Q

What is AJAX?

A

Asynchronous JavaScript and XML

Tools for making network requests in code.
Allows us to update parts of the page without having to refresh the entire page.

113
Q

What does AJAX acronym stand for?

A

Asynchronous JavaScript and XML

114
Q

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

A

XMLHttpRequest object

115
Q

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

A

‘load’ event

116
Q

An XMLHttpRequest object has an addEventListener( ) method just like DOM elements. How is it possible that they both share this functionality?

A

They are both event target objects so they both get the method through inheritance

117
Q

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

A

chunks of code within curly braces

if, for, function, while, etc

118
Q

What does block scope mean?

A

data within a block can only be accessed within the same block

119
Q

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

A

block-scope

120
Q

What is the difference between let and const?

A

let is mutable, const is immutable

const requires a value to be assigned to it during initialization

121
Q

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

A

The const variable just holds a reference to the array so you are able to change the array itself, but not assign a new array to the variable

122
Q

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

A

var: if it needs to be global
let: if can be defined within a block and needs to be mutable
const: defined within a block and needs to be immutable

Try to use const, unless you can’t

123
Q

What is the syntax for writing a template literal?

A

text surrounded by backticks

variables to be substituted are ${variable_name} within the backticks

124
Q

What is “string interpolation”?

A

ability to substitute parts of a string with values of variables or expressions

125
Q

What is destructuring, conceptually?

A

Taking the values/properties of an array/object and assigning them to variables in a shorthand expression

126
Q

What is the syntax for object destructuring?

A

const/let { property: alias,… } = objectName;

alias is optional, if not included, property name will be the variable name

can also set default values with assignment

127
Q

What is the syntax for Array destructuring?

A

const/let [ varName1, varName2,… ] = arrayName

can also set default values

128
Q

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

A

destructuring will have the object or array literal syntax on the left side of the assignment operator

129
Q

What is the syntax for defining an arrow function?

A

(parameters) => expression

(parameters) => {statement}

130
Q

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

A

It will automatically return the expression

131
Q

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

A

arrow function captures the this value of the enclosing context (parent blocks) instead of creating its own this context

DEFINITION TIME

132
Q

What is the JavaScript Event loop?

A

Its purpose is to take processes in the task queue and place them into the stack only if the stack is clear.

133
Q

What is different between “blocking” and “non-blocking” with respect to how code is executed?

A

blocking code: code that occupies the stack (synchronous)

non-blocking: taken out of the stack and placed into the task queue (asynchronous)

134
Q

What are the three states a Promise can be in?

A

pending,
fulfilled,
rejected

135
Q

How do you handle the fulfillment of a Promise?

A

then( )

136
Q

How do you handle the rejection of a Promise?

A

catch( )

.then( ) but with 2 arguments (fulfilled, rejected) callback functions

137
Q

What is Array.prototype.filter( ) useful for?

A

Taking an array and getting a new array with only the elements that satisfy the given condition

138
Q

What is Array.prototype.map( ) useful for?

A

Take a given array and create a new one containing all the elements from the previous array with a transformation applied to it

139
Q

What is Array.prototype.reduce( ) useful for?

A

Collapse/combine a bunch of data into one

140
Q

What is “syntactic sugar”?

A

syntax within a programming language that is designed to make things easier to read or express.

141
Q

What is the typeof an ES6 class?

A

function

142
Q

Describe ES6 class syntax

A
class ClassName {
    constructor(params) {
         this.whatever = params
    }
methods }
143
Q

What is “refactoring”?

A

restructuring existing code (usually in order to make it easier to read, improve efficiency, reduce complexity) without changing the behavior

144
Q

How are ES modules different from CommonJS modules?

A

CommonJS: require( )
module.exports = …;
Part of Node

ES Modules: import { } from ‘path’
attach export keyword to anything you want to export
Official JS

145
Q

What kind of modules can Webpack support?

A

CommonJS, ES Modules, AMD modules, Assets, WebAssembly modules

146
Q

What does fetch( ) return?

A

A promise resolving to the response received from the server

147
Q

What is the default request method used by fetch( )?

A

GET

148
Q

How do you specify the request method (GET, POST, etc) when calling fetch?

A

By providing an optional object containing the method property as the second argument

149
Q

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

myFunction( )( );

A

another function

150
Q

What does this code do?

const wrap = value => ( ) => value;

A

if you call wrap(value) it will return a function that if called on will return value.

151
Q

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

A

closures;

In JS, functions remember their lexical environment