JavaScript Flashcards

1
Q

What is the purpose of variables?

A

To store data

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= variable value

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

=

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

Letter, $, or _, numbers after first character

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

Variable name can be different for capital or lowercase same name

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 a sequence of characters

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 a number, calculations

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

Store true or false values, render a sate of something is or isn’t

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, assigning a value to the 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 = newvalue

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: value can only be assigned, not generated, developer set variable = null purposely. placeholder empty for now
Undefined: empty, nothing here

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 understand which console log is being output

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

Give five examples of JavaScript primitives.

A

Number, String, Boolean, Undefined, Null and Symbol

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 together 2 or more strings, result will be a 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

cocatenate or add

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

Grouping together related variables

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

What are object properties?

A

Piece of data stored in the object, collection of variables
The key and value in the objects

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

Describe object literal notation.

A

Var object = {
Key: value,
Key: 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 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
Bracket notation object[property]

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

What are arrays used for?

A

Lists of data where either order is important or not important

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

Describe array literal notation.

A

Var arrayname;
Arrayname = [‘value1’, ‘value2’];
bracket separated by commas

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

has a length property and keeps updating
call a method to add data to the array

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

Number of items in the 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

Performs a task and returns the output

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(called keyword) functname (parameters) { Code block;
Return ;
}

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(argument1, argument2, etc);

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 call is running the code
Function definition is creating the object, has a codeblock { } and function keyword

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 a placeholder for a variable we don’t know until we call the function and pass an argument
When defining a function, we declare parameters.
When we call a function we pass it an argument

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

Why are function parameters useful?

A

So we can write the function once and insert any argument we want when we run the function

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

Causes the function to produce a value we can use in our program (get the output to wherever the function was called)
Prevents any more code in the function’s code block from being run (stops function from running)

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

Give 6 examples of comparison operators.

A

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

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

What is the purpose of an if statement?

A

To guide the computer to make a decision based on criteria

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

Is else required in order to use an if statement?

A

No If you require code to run only when the statement returns true

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

Describe the syntax (structure) of an if statement.

A

If (condition) {
code block -Return value;
} else { code;
}

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

What are the three logical operators?

A

&& || !

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

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

A

Using a && or ||

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

Why do we log things to the console?

A

To find errors and warnings as they occur in the JavaScript code.
Debugging, something is broken, understand what’s happening

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

What is a method?

A

a function which is a property of an object. Using an object then a . then method name, performs a function

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

How is a method different from any other function?

A

method is associated with an object, while a function is not.
method: object.method
function: state it

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

How do you remove the last element from an array?

A

arrayname.pop();, arrayname.shift() removes first element

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

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

A

Math.floor(numberhere);

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

How do you generate a random number?

A

Math.random() -random float value from 0 to 1 (is a percentage)

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

How do you delete an element from an array?

A

arrayname.splice();
splice method at the index array

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

How do you append an element to an array?

A

arrayname.push() adds to end/ arrayname.unshift() adds to beginning

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

How do you break a string up into an array?

A

stringname.split(“ “) use space as an argument (thing you want to break it up on)

53
Q

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

A

does not change it, console log it

54
Q

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

A

No

55
Q

What is the purpose of a loop?

A

To check a condition and repeat the code block until a condition is met

56
Q

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

A

Instruct the code when to stop looping

57
Q

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

A

A pass through the loop

58
Q

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

A

before each pass through the loop (before each iteration)

59
Q

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

A

evaluated once before the loop begins

60
Q

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

A

evaluated before each loop iteration

61
Q

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

A

When the condition evaluates to false

62
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

63
Q

What does the ++ increment operator do?

A

Adds 1

64
Q

How do you iterate through the keys of an object?

A

use a for in loop

65
Q

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

A

focus

66
Q

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

A

Blur

67
Q

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

A

input

68
Q

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

A

submit

69
Q

What does the event.preventDefault() method do?

A

prevent the browser from automatically reloading the page with the form’s values in the URL

70
Q

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

A

Prevent form from submitting

71
Q

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

A

HTMLFormElement.elements

72
Q

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

A

HTMLFormElement.elements.value

73
Q

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

A

Not knowing where to fix it , rewriting

74
Q

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

A

See errors as they occur

75
Q

What is the event.target?

A

Target of the event, element interacted width

76
Q

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

A

Removes it from the display and doesn’t take up space but still part of the page

77
Q

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

A

A string css selector and returns a boolean if it matches the selector

78
Q

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

A

Element.getAttribute(‘ attribute you want here‘);

79
Q

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

A

Every step, after every expression

80
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

conditional for each possible view or separate event handler for each view

81
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

individual conditional blocks for each view, repetition of effort same code

82
Q

What is JSON?

A

a text-based data format following JavaScript object syntax

83
Q

What are serialization and deserialization?

A

Serialization is the process of turning an object in memory into a stream of bytes so you can do stuff like store it on disk or send it over the network.
Deserialization is the reverse process: turning a stream of bytes into an object in memory.
Serialization converts it to a string
Deserialization does the opposite to objects and arrays

84
Q

Why are serialization and deserialization useful?

A

Serialization lets you send the data easily
Deserialization lets you convert back to an object that is easy to work with

85
Q

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

A

var jsonString = JSON.stringify(data structure here);

86
Q

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

A

var jsonParse = JSON.parse(jsonString);

87
Q

How do you store data in localStorage?

A

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

88
Q

How do you retrieve data from localStorage?

A

Var varstorage = localStorage.getItem(‘keyName’);

89
Q

What data type can localStorage save in the browser?

A

String only

90
Q

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

A

fired when the window, the document and its resources are about to be unloaded. before page closes

91
Q

What is a method?

A

a function which is a property of an object.

92
Q

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

A

A method definition has the code block
Method call is running the code block with a argument

93
Q

Describe method definition syntax (structure).

A

var obj = {
propertyName : function def(parameter) {
}
};

94
Q

Describe method call syntax (structure).

A

objectname.method(arguments);

95
Q

How is a method different from any other function?

A

Methods are associated with an object, exampleobject.methodname
Functions run on their own

96
Q

What is the defining characteristic of Object-Oriented Programming?

A

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

97
Q

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

A

Abstraction
Encapsulation
Inheritance
Polymorphism

98
Q

What is “abstraction”?

A

being able to work with (possibly) complex systems in simple ways. lightswitches

99
Q

What does API stand for?

A

application programming interface

100
Q

What is the purpose of an API?

A

Application that is gonna interface with another program to do things in a simple way

101
Q

What is this in JavaScript?

A

an implicit parameter of all JavaScript functions.

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

103
Q

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

A

call time

104
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, doesn’t exist yet bc its not called

105
Q

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

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

A

The variable message : “it’s a me mario’. Runs the greet property of the character variable which is a function.
greet method being invoked of he character object. the object being called off of is chjaracer and gets firstname Property

106
Q

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

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

A

no dot in hello() so becomes the window, has nofirstname, returns “it’s a me undefined”

107
Q

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

A

You can’t, undefined. only when being called

108
Q

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

A

the value of this can be recognized as “the object to the left of the dot” when the function is called (as a method).

“This” is “the object to the left of the dot”. if no dot, it is window

109
Q

What kind of inheritance does the JavaScript programming language use?

A

prototype-based (or prototypal) inheritance

110
Q

What is a prototype in JavaScript?

A

object that can allow JavaScript objects to inherit features from one another

111
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

JavaScript objects give certain behaviors (methods) or data (properties) to other objects.

within prototype object
each of those methods a re stored on the prototype object of that kind of data type

112
Q

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

A

Prototype

113
Q

What does the new operator do?

A

1 Creates a new blank object, attached to the prototype of the new object
2 Takes value of the prototype constructor
3 Makes the new object the value of “this” and runs code block
4 If No return statement, the object created gets returned automatically

114
Q

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

A

prototype property
.prototype

115
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. The return value is a boolean value.

116
Q

What is a “callback” function?

A

passing a function as an argument to another function

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

118
Q

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

A

setInterval()

119
Q

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

A

0 seconds, immediate

120
Q

What do setTimeout() and setInterval() return?

A

Return a intervalID a numeric, non-zero value which identifies the timer created by the call to
setInterval(); this value can be passed to clearInterval() to cancel the interval.

starts at 1, pos integer

121
Q

What is AJAX?

A

Technique for loading data into part of a page without having to reload the entire page

122
Q

What does the AJAX acronym stand for?

A

Asynchronous JavaScript And XML

123
Q

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

A

XMLHttpRequest

124
Q

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

A

load

125
Q

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

A

prototype, inherits

126
Q

What is Array.prototype.filter useful for?

A

array.filter copies, doesnt modify and only values you want

127
Q

What is Array.prototype.map useful for?

A

modifying all values in a copy

128
Q

What is Array.prototype.reduce useful for?

A

combining all the elements of a big array to one