JavaScript Flashcards

1
Q

What is the purpose of variables?

A

Variables are meant for storing values.

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

How do you declare a variable?

A

var variableName;

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

variableName = value;

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

Alphabet characters, underscores, dollar signs, numbers (but not in the first char).

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

name and Name are separate variables.

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

To store lists of characters, useful for communicating information to humans or computers.

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

To store numerical values for record keeping or calculation.

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

Storing true/false values, used in control flow.

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

The right value is being assigned to the left 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

varName = 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

Null is intentional, undefined is usually not.

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

So you know what’s being logged.

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, boolean, undefined, null

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

Putting two or more string values together to form a greater string

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

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

A

Arithmetic addition and 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?

A

Booleans.

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

Add what’s on the right to current value of variable then assign result to the variable.

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

What are objects used for?

A

Storing variables and functions associated with a particular entity.

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

What are object properties?

A

Values stored in an object.

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

Describe object literal notation.

A

var objNew = { prop1: value, prop2: value }

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

delete obj.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 and bracket notation.

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

What are arrays used for?

A

Storing ordered lists of values.

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

Describe array literal notation.

A

var array = [0, 1, 2, 3];

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

Entries inside the array are ordered and indexed by number instead of property names, new entries are added by pushing.

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

Property of the array that contains how many entries it currently holds.

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 special type of object that can be called, containing reusable code.

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

function keyword, optional name, zero or more parameters, code-block, and a return statement.

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

Function name, parentheses, arguments if any.

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

Call passes arguments and executes the code, definitions only specify parameters and code.

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

Parameters are placeholders, arguments contain actual values.

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

Why are function parameters useful?

A

They can act as placeholder variables for the expressions you want to evaluate so you can reuse functions with different variables.

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

It produces a value and ends the function.

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 their values.

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

What is a method?

A

A function contained in 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 method usually involves manipulating or retrieving data from within the object and is called as a property of the object.

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

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

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.floor(Math.random * (max - min + 1) + min)

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.pop() or array.shift()

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

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)

46
Q

Do string methods change the original string? How would you check if you weren’t sure?

A

No, log the string again to make sure.

47
Q

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

A

39 not including inheritance.

48
Q

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

A

No.

49
Q

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

A

36 not including inheritance.

50
Q

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

A

MDN!

51
Q

Give 6 examples of comparison operators.

A

Sloppy equality, strict equality, not sloppy equal, not strictly equal, greater than, greater than or equal to, less than, less than or equal to

52
Q

What data type do comparison expressions evaluate to?

A

Booleans.

53
Q

What is the purpose of an if statement?

A

To evaluate a condition and execute a code block if true.

54
Q

Is else required in order to use an if statement?

A

No.

55
Q

Describe the syntax (structure) of an if statement.

A

if (condition) {
code block
}

56
Q

What are the three logical operators?

A

AND, OR, NEGATION

57
Q

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

A

Put a comparison operator between them.

58
Q

What is the purpose of a loop?

A

Loops run the same block of code a certain number of times.

59
Q

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

A

Condition evaluates whether or not the code block is run again.

60
Q

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

A

An iteration is an instance of the code block being run within the loop.

61
Q

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

A

The condition expression is evaluated before executing the statement.

62
Q

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

A

Initialization happens before anything else.

63
Q

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

A

After the initialization/final expression and before the statement is executed.

64
Q

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

A

After the code block is executed.

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

for…in loops.

68
Q

What is JSON?

A

JSON is a format for converting JS objects into strings of text.

69
Q

What are serialization and deserialization?

A

Serialization is the process of converting an object in memory into a stream of bytes, deserialization is converting the bytes to an object in memory.

70
Q

Why are serialization and deserialization useful?

A

They’re useful for transmitting JS objects and the data they contain to other platforms that may not support JS but can read JSON strings.

71
Q

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

A

JSON.stringify(object)

72
Q

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

A

JSON.parse(stringJSON)

73
Q

How to you store data in localStorage?

A

localStorage.setItem(‘name’, ‘value’)

74
Q

How to you retrieve data from localStorage?

A

localStorage.getItem(‘name’)

75
Q

What data type can localStorage save in the browser?

A

Strings. Other data types are converted to Strings and Objects must be stringified first.

76
Q

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

A

Before the page gets unloaded, ie. closed, clicked away from, or refreshed.

77
Q

What is a method?

A

A function that exists as a property of an object.

78
Q

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

A

A method definition specifies what the method will do, a method call executes the code block within the method.

79
Q

Describe method definition syntax (structure).

A

object.method = function () { codeblock };

80
Q

Describe method call syntax (structure).

A

object.method();

81
Q

How is a method different from any other function?

A

Methods are associated with objects.

82
Q

What is the defining characteristic of Object-Oriented Programming?

A

The use of Objects that model entities and their interactions with each other as an organizing principle of code.

83
Q

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

A

Abstraction, encapsulation, inheritance, polymorphism.

84
Q

What is “abstraction”?

A

Simplification of complex code by hiding unnecessary data to make it friendlier for users.

85
Q

What does API stand for?

A

Application programming interface.

86
Q

What is the purpose of an API?

A

APIs “communicate” between different pieces of software.

87
Q

What is this in JavaScript?

A

this is an implicit parameter that returns the value of the object from which the function was called.

88
Q

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

A

It is available in a function’s code block even though it was never explicitly included in the parameter list.

89
Q

When is the value of this determined in a function; call time or definition time?

A

Call time!

90
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 yet.

91
Q

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

A

You can’t as it has no value yet and you don’t know what object it’ll be called off when executed.

92
Q

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

A

See what object the function is being called off.

93
Q

What kind of inheritance does the JavaScript programming language use?

A

Prototypal inheritance.

94
Q

What is a prototype in JavaScript?

A

An object corresponding to a given type from which every instance of the object type will inherit its properties.

95
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 inherit them from their respective object prototypes.

96
Q

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

A

It will go up the prototype chain looking for the nearest matching property.

97
Q

What does the new operator do?

A

It 1) creates a JS object 2) adds __proto__ property that links to the constructor function’s prototype object 3) binds the newly created object instance as the this context 4) returns this if the function doesn’t return an object.

98
Q

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

A

.prototype property

99
Q

What does the instanceof operator do?

A

Checks if the item on the left is an instance of the object on the right.

100
Q

What is a “callback” function?

A

A function that gets passed as an argument for another function.

101
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

Passing the function to the setTimeout() or setInterval() functions.

102
Q

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

A

Use the setInterval() function.

103
Q

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

A

0

104
Q

What do setTimeout() and setInterval() return?

A

A positive integer ID unique to the timeout or interval.

105
Q

What is AJAX?

A

A technique that allows developers to build asynchronous web apps (can retrieve and update information on page without reloading it).

106
Q

What does the AJAX acronym stand for?

A

Asynchronous JavaScript And XML

107
Q

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

A

XMLHTTPRequest

108
Q

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

A

The ‘load’ event

109
Q

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

A

They have the prototype of EventTarget in their prototype chains.

110
Q

What is Array.prototype.filter useful for?

A

Returning an array that filters out all elements of the initial array that did not match the predicate.

111
Q

What is Array.prototype.map useful for?

A

Returning an array that applies the same function to every element in the input array.

112
Q

What is Array.prototype.reduce useful for?

A

Returning a value that is obtained by reducing the array to a single result with a “reducer” callback function (e.g. summing all elements).