JavaScript Flashcards

1
Q

What is the purpose of variables?

A

to store or remember bits of information for the script.

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

How do you declare a variable?

A

variable keyword plus variable name (var, let, const)

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, numbers, dollar sign or underscore

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

that uppercase/lowercase letters are distinguished. score is not same as Score.

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 represent or manipulate 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

to represent or manipulate numbers for 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

they allow us to make decisions in code. to determine if something is true or false.

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

variable name, equal sign, new 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 purposeful emptiness; undefined is accidental emptiness.

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

to make it clear which variables are 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

joining two or more strings together to create a single value

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

addition for arithmetic and string concatenation for string.

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

The addition assignment operator (+=) adds the value of the right operand to a variable and assigns the 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

to group together a set of variables and function to create a model. to store multiple pieces of information that are related to each other.

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

What are object properties?

A

it’s the variable of the object. tells us about the object (name, number of rooms)

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

Describe object literal notation.

A

keyword var, object variable name, property key and 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

use the operator ‘delete’ followed by dot or bracket notation.

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 or square 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

to store a list or set of values that are related to each other

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

Describe array literal notation.

A

keyword var, name of array, equal sign, open square bracket, values separated by a comma, closing square bracket.

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

the property (key) for each value is its index number. values in arrays are usually the same data type. there’s a length property.

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 holds the number of items 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 minus 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

set of statements that performs a task or calculates a value.

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, function name with parameters, code block in curly braces.

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

The function’s name.
A comma-separated list of zero or more arguments surrounded by () parentheses.

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

function definition is to declare a function;
function call is to invoke the function (to run the 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

parameter acts as a placeholder (declared during function definition); argument is the actual value we want to run through the function. (passed through during function call).

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

Why are function parameters useful?

A

used as a placeholder so we can use it to run multiple arguments.

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 spot check that our code works and debug it, verification, landmark to see code execute.

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

What is a method?

A

function stored as the property of a 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

there’s a dot notation for method. (object.method)

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

pop() method ( 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() method. ( Math.floor(x) )

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() method. ( Math.random() )

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

splice() method

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

push() and unshift()

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

split() method.

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 if you weren’t sure?

A

No. You can check by console.logging the original variable for the string.

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

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

A

No

48
Q

Give 6 examples of comparison operators.

A

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

49
Q

What is the purpose of an if statement?

A

to evaluate a condition and make a decision to execute code block or not.

50
Q

Is else required in order to use an if statement?

A

no.

51
Q

Describe the syntax (structure) of an if statement.

A

keyword if, condition, code to execute in code block

52
Q

What are the three logical operators?

A

and (&&) , or (||), logical not (!)

53
Q

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

A

&& and ||

54
Q

What is the purpose of a loop?

A

Loops check a condition to see if the code block will run again.

55
Q

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

A

to check if we should keep going.

56
Q

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

A

An iteration of a loop happens each time the condition is evaluated to be true and the statement executes.

57
Q

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

A

before each loop iteration

58
Q

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

A

once before the loop begins

59
Q

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

A

before each loop iteration

60
Q

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

A

after each loop iteration, but before the next condition evaluation.

61
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

62
Q

What does the ++ increment operator do?

A

it adds one to the counter

63
Q

How do you iterate through the keys of an object?

A

with a for…in loops

64
Q

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

A

blur

65
Q

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

A

focus

66
Q

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

A

input

67
Q

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

A

submit

68
Q

What does the event.preventDefault() method do?

A

prevents browser from reloading with the form’s values in the URL

69
Q

What does submitting a form without event.preventDefault() do?

A

it reloads the browser and it loads the form’s property keys into the URL bar.

70
Q

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

A

elements property

71
Q

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

A

value property

72
Q

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

A

a lot of bugs and hard to trace where it’s coming from.

73
Q

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

A

you can catch errors while they’re happening.

74
Q

What is JSON?

A

JavaScript Object Notation. data interchange format used to send and store information in computer systems.

75
Q

What are serialization and deserialization?

A

deserialization: converting a string to a native object

serialization: converting from a native object to a string

76
Q

Why are serialization and deserialization useful?

A

it turns data/objects into storable and transmittable formats that you can later reconstruct

77
Q

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

A

JSON.stringify()

Accepts an object as a parameter, and returns the equivalent JSON string.

78
Q

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

A

JSON.parse()

Accepts a JSON string as a parameter, and returns the corresponding JavaScript object.

79
Q

How do you store data in localStorage?

A

.setItem(keyName, keyValue)

80
Q

How do you retrieve data from localStorage?

A

getItem(key)

81
Q

What data type can localStorage save in the browser?

A

string

82
Q

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

A

The beforeunload event is fired when the window, the document and its resources are about to be unloaded.

83
Q

What is a method?

A

a function which is a property of an object.

84
Q

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

A

definition you will define function and assign it to a property and method call will have actual value/argument and dot notation.

85
Q

Describe method definition syntax (structure).

A

declare object literal. method name. colon. function key word with parameters. code block.

86
Q

Describe method call syntax (structure).

A

object.method(arguments)

87
Q

How is a method different from any other function?

A

there’s a dot notation for method. (object.method)

88
Q

What is the defining characteristic of Object-Oriented Programming?

A

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

89
Q

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

A

Abstraction, Encapsulation, Inheritance, and Polymorphism

90
Q

What is “abstraction”?

A

being able to work with (possibly) complex things in simple ways

91
Q

What does API stand for?

A

Application Programming Interface

92
Q

What is the purpose of an API?

A

For two or more computer programs to communicate with each other.

93
Q

What is this in JavaScript?

A

it’s an implicit parameter of all JavaScript functions

94
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 included in the function’s parameter list or declared with var

95
Q

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

A

The value of this is determined at call time.

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. it’s not being called yet.

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 !” because this refers to the object to the left of the dot of the function call

98
Q

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

A

undefined. function is not being called as method. so you do not know what the value of this is.

99
Q

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

A

you can’t determine value of this without the function being called.

100
Q

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

A

find where the function is called and look for an object to the left of the dot.

101
Q

What kind of inheritance does the JavaScript programming language use?

A

prototype-based (prototypal) inheritance.

102
Q

What is a prototype in JavaScript?

A

an object that contains properties and methods that can be used by other objects.

103
Q

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

A

They are defined in a prototype object and the strings/arrays/numbers are simply borrowing those methods when needed.

104
Q

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

A

window

105
Q

What does the new operator do?

A

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

creates an empty object, points the object to the constructor function, executes function with the given argument and binding it to “this”. it returns “this” value if it’s primitive.

106
Q

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

A

prototype property

107
Q

What does the 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. The return value is a boolean value

108
Q

What is a “callback” function?

A

A callback function 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.

109
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(function, delay)

110
Q

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

A

setInterval(function, delay)

111
Q

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

A

0

112
Q

What do setTimeout() and setInterval() return?

A

an interval ID which is a numeric, non-zero value to identify the timer.

113
Q

What is AJAX?

A

A technique for loading data into part of a page without having to refresh the entire page.

114
Q

What does the AJAX acronym stand for?

A

Asynchronous JavaScript and XML

115
Q

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

A

XMLHttpRequest

116
Q

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

A

load

117
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 a shared prototype object