JavaScript Flashcards

1
Q

What is the purpose of variables?

A

They allow us to store values or data to use for functions and other things

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 = value, need var

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

Set the variable = to a value 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, $, _ and numbers, but they can’t start with a 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

Varname is a different variable than varname

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 and interact with text content

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 and interact with numbers

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

To store and interact with boolean values. Often used as a switch or to make 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

It means ‘assigned the value of’

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

You reassign it a 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 an object with no value, undefined has no value or type.
Undefined comes from javascript engine, it helps js tell us if something isn’t there, null has to be defined by the human.

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 know what you are logging

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

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

A number

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

What is string concatenation?

A

It is when you use the + symbol to add two strings together, called concatenation

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

It can and numbers and concatenate strings

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

It adds the value with the variable and assigns that value to the variable

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

What is an expression

A

It is a chuck of code in JS.

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

What are objects used for?

A

Used to store related data of all different data types (variables and functions)

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

What are object properties?

A

They are the individual data types within an object literal

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

Describe object literal notation.

A

Object literal notation declares a variable as an object with the assignment operator with the property, value pairs within curly brackets separated by commas.

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

How do you remove a property from an object?

A

Use the delete key word and then the property you want to delete in dot notation

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

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

A

Dot notation, or bracket notation

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

What are arrays used for?

A

To store lists of data, generally better for large sums of data

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

Describe array literal notation.

A

Var arrayName = [‘index1’, ‘index2’, ‘index3’]

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

How are arrays different from “plain” objects?

A

They have index numbers paired with each value instead of property, arrays have a set order, objects don’t have a set order

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

What is the length property of an array?

A

The length property of an array is the number of index values in the array

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

How do you calculate the last index of an array?

A

You can use the length property to find the length of the array, and then subtract that number by one since the first index is 0.
lastIndex = array.length - 1;

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

What is a function in JavaScript?

A

Is a block of code that executes when called. Arguments can be imputed and values can be outputted by 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 definition.

A

Function keyword, function name with parenthesis (ex. name()) (name is optional), arguments in parenthesis, curly braces that hold the code to be executed, and an optional return key word with the value you want to be returned
Function fxnName(parameter1, parameter2) { code here; return outputValue };

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

Describe the parts of a function call.

A

fxnName(argument1, argument2);

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

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

A

Function definition has the definition block and the function keyword, calling it just requires the name and necessary arguments/parameters

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

What is the difference between a parameter and an argument?

A

Parameters are only used as variables to store the inputted arguments, arguments are what are inputted and are already defined data types.

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

Why are function parameters useful?

A

They are useful by making it easier for a function to be repeatable and to take values from other data in our program

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

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

A

It returns a value from the function and also ends the function’s execution.

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

Why do we log things to the console?

A

To check or code to see if functions and methods work properly, arrays and variables are filled and assigned.

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

What is a method?

A

A method is a function that is a property of an object.

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

How is a method different from any other function?

A

Methods are attached to objects, functions aren’t necessarily properties of objects.

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

How do you remove the last element from an array?

A

.pop() method

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

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

A

.floor() method of the Math object

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

How do you generate a random number?

A

.random() method of the Math object

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

How do you delete an element from an array?

A

.splice(starting index, number of indexes to delete) method

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

How do you append an element to an array?

A

Push or unshift method

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

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

A

They don’t, console.log would check it, you can also check documentation

48
Q

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

A

Over 20

49
Q

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

A

No, some are just useful for changing an object, the return value is useful in some cases.

50
Q

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

A

Over 20

51
Q

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

A

mdn

52
Q

Give 6 examples of comparison operators.

A

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

53
Q

What data type do comparison expressions evaluate to?

A

Boolean true or false

54
Q

What is the purpose of an if statement?

A

To help make decisions based on the comparison expression.

55
Q

Is else required in order to use an if statement?

A

no

56
Q

Describe the syntax (structure) of an if statement.

A

If (operand1 > operand 2) {
Return something;
}

57
Q

What are the three logical operators?

A

&&, ||, !

58
Q

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

A

Separate the expressions with a logical operator

59
Q

What is the purpose of a loop?

A

To repeat a junk of code, or a process.

60
Q

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

A

To give an end to a loop.

61
Q

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

A

The loop be executed once

62
Q

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

A

Before the conditional block is executed, and before each iteration after that

63
Q

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

A

Before anything, only once.

64
Q

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

A

Before each iteration

65
Q

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

A

At the end of the code block before the condition expression is evaluated. And for each iteration

66
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

67
Q

What does the ++ increment operator do?

A

It increments and changes the variable by 1. 1++ is equal to 2.

68
Q

How do you iterate through the keys of an object?

A

By using a “for in” loop
For (var keyParameter in objectParameter) {}

69
Q

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

A

‘focus’

70
Q

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

A

‘blur’

71
Q

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

A

‘input’

72
Q

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

A

‘submit’

73
Q

What does the event.preventDefault() method do?

A

The event interface’s preventDefault() method tells the user agent that if the event does not get explicitly handled, its default action should not be taken as it normally would be

74
Q

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

A

It would refresh the page after the form is submitted

75
Q

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

A

The ‘elements’ property

76
Q

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

A

Value property

77
Q

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

A

The more code you write without checking, when you get a bug, you’ll have more code to check to find the bug.

78
Q

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

A

One advantage is being able to use console.log to see what values are in real time. Another one is being able to check the contents of functions and variables

79
Q

What is the event.target?

A

The targeted element that triggered the event listener

80
Q

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

A

The document doesn’t display it as if it doesn’t exist. Removes it from the document flow

81
Q

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

A

The matches() method checks to see if the Element would be selected by the provided selectorString – in other words – checks if the element “is” the selector.

82
Q

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

A

.getAttribute(‘attribute name’)

83
Q

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

A

As many steps as possible

84
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

Have an event listener on each element

85
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

You would have to write an if statement for each option.

86
Q

What is JSON?

A

The JSON object contains methods for parsing JavaScript Object Notation (JSON) and converting values to JSON. It can’t be called or constructed, and aside from its two method properties, it has no interesting functionality of its own.

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

88
Q

Why are serialization and deserialization useful?

A

It allows for the web application to save data without relying on the host computer.

89
Q

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

A

JSON.stringify()

90
Q

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

A

JSON.parse()

91
Q

How to you store data in localStorage?

A

localstorage.setItem

92
Q

How do you retrieve data from localStorage?

A

localStorage.getItem

93
Q

What data type can localStorage save in the browser?

A

string

94
Q

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

A

It happens before a screen unloads.

95
Q

What is a method?

A

A function that is a property of an object

96
Q

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

A

Method definition is defined with the object definition with a function key word, and a method call is written as object.method()

97
Q

Describe method definition syntax (structure).

A

Object {
propertyName: function (parameters) {
Function definition…
}
}

98
Q

Describe method call syntax (structure).

A

object.method()

99
Q

How is a method different from any other function (trick question)?

A

It’s not. methods and functions do the same thing.

100
Q

What is the defining characteristic of Object-Oriented Programming?

A

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

101
Q

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

A

Abstraction, encapsulation, inheritance, polymorphism

102
Q

What is “abstraction”?

A

Allows to execute (possibly) complex things in simple ways.

103
Q

What does API stand for?

A

application programming interface

104
Q

What is the purpose of an API?

A

is to give programmers a way to interact with a system in a simplified, consistent fashion

105
Q

What is “this” in JavaScript?

A

this is an implicit parameter of all JavaScript functions

106
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

107
Q

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

A

Call time

108
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 this gets a value when it is called

109
Q

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

A

‘It’s-a-me, Mario! Is logged to the console.
Because the greet method of the character object logs a string to the console with a value of this.firstName which is the same as character.firstName

110
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! Because hello is not a method of an object, it is just a function. And this.firstName is the same as window.firstName and window does not have a firstName property so it is undefined

111
Q

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

A

You can’t

112
Q

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

A

the object to the left of the dot when the function is called (as a method).

113
Q

What kind of inheritance does the JavaScript programming language use?

A

prototype-based (or prototypal) inheritance.

114
Q

What is a prototype in JavaScript?

A

A new object produced by cloning another object

115
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

prototype-based (or prototypal) inheritance allows for javascript objects to give certain behaviors (methods) or data (properties) to other objects.

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