JavaScript Flashcards

1
Q

What is the purpose of variables?

A

To store data that needs to be referenced later

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

How do you declare a variable?

A

Using the keyword var (const and let)

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

var 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

Letters, numbers, $, _ (numbers cannot be first)

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

They are sensitive to uppercase and lowercase letters

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 a series of character, 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 a numeric value, counting, measurement, math

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

A data type that only has two possible values, 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

Assigning a value to something

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: empty value, must be assigned by you, it represents something that does not have a value assigned to it yet,
Undefined: empty value, assigned by JavaScript

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

Where is var scoped

A

Var is scoped to the function that it is inside of. If you leave out the var keyword when using a variable in a function, JavaScript will look for the next variable that is scoped closest

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

Why is it a good habit to include “labels” when you log values to the browser console?

A

So you know what is being logged to the console

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

What is string concatenation?

A

Combining a string with another value and makes the result of that expression into a new string

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

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

A

Adding numbers, concatenating strings

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

What does the += “plus-equals” operator do?

A

Adds the value of the variable on the right to the variable on left, then assigns the result of that expression to the variable on the left

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

What are objects used for?

A

Storing data in a group or collection that makes sense

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

What are object properties?

A

Pieces of data stored within an object

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

Describe object literal notation.

A

Var variableName = {};

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

The delete operator: delete object.property

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: object.propertyName

Bracket notation: object[‘propertyName’]

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

What are arrays used for?

A

To list data in a specific order

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

Describe array literal notation.

A

Var variableName = [];

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 are numerically indexed

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

How many elements are 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

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

A special type of object that can be called, a set of reusable code

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 functionName(parameter) {code block; return statement}
Name and return statement are optional
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

functionName(argument) argument is optional

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

A call will have arguments, a definition includes a code block

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

A parameter is a variable related to a function definition, an argument is a value related to a function call
Parameter is a placeholder for a variable that will eventually be passed into the function. It is included in the function definition
Argument is the actual variable that is passed into the function when it is called

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

Why are function parameters useful?

A

To pass additional information into a function for the function to use. It allows us to create more generalized, reusable functions

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

Causes the function to produce a value, ends the function

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 test and inspect your code and make sure your functions or methods are returning what you expect, or see what the values of variables are.

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

What is a method?

A

A property of an object that is a function

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

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

41
Q

How do you remove the last element from an array?

A

array.pop()

42
Q

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

A

Math.floor()

43
Q

How do you generate a random number?

A

Math.random() (generates a number from 0 to 1 not including 1)

44
Q

How do you delete an element from an array?

A

Array.splice(start, deleteCount)

45
Q

How do you append an element to an array?

A

Array.push()

46
Q

How do you prepend an element to an array?

A

Array.unshift()

47
Q

How do you break a string up into an array?

A

String.split()

48
Q

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

A

No, strings are immutable. Use console.log() to check

49
Q

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

A

no

50
Q

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

A

MDN, also include the name of the language

51
Q

Give 6 examples of comparison operators.

A

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

52
Q

What data type do comparison expressions evaluate to?

A

Boolean

53
Q

What is the purpose of an if statement?

A

To execute a block of code if a certain condition is truthy

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

&&, || , !

57
Q

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

A

Put each expression in parentheses

58
Q

What is the purpose of a loop?

A

To allow us to repeat a certain block of code

59
Q

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

A

To tell the loop whether or not to run

60
Q

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

A

It represents each time the code within the code block runs

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

Only once, before the loop starts

Initialization: before anything

63
Q

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

A

After initialization, before each iteration

Condition: before each

64
Q

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

A

After each iteration

Final: after each

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 a variable by 1

67
Q

How do you iterate through the keys of an object?

A

a for…in loop

68
Q

Which “document” is being referred to in the phrase Document Object Model?

A

The HTML document

69
Q

What is the word “object” referring to in the phrase Document Object Model?

A

JavaScript object

70
Q

What is a DOM Tree?

A

A tree representing each element within the DOM, and its child elements

71
Q

Give two examples of document methods that retrieve a single element from the DOM.

A

document.getElementById(), document.querySelector()

72
Q

Give one example of a document method that retrieves multiple elements from the DOM at once.

A

document.querySelectorAll()

73
Q

Why might you want to assign the return value of a DOM query to a variable?

A

So you can access it again more easily

74
Q

What console method allows you to inspect the properties of a DOM element object?

A

console.dir()

75
Q

Why would a tag need to be placed at the bottom of the HTML content instead of at the top?

A

So the browser can parse through the document before JavaScript has to access it

76
Q

What does document.querySelector() take as its argument and what does it return?

A

A string of a CSS selector, returns the first element that matches the selector

77
Q

What does document.querySelectorAll() take as its argument and what does it return?

A

A string of a CSS selector, returns a NodeList of all elements that match

78
Q

What is the purpose of events and event handling?

A

It allows us to interact with the webpage, to make our apps dynamic

79
Q

Are all possible parameters required to use a JavaScript method or function?

A

no

80
Q

What method of element objects lets you set up a function to be called when a specific type of event occurs?

A

.addEventListener()

81
Q

What is a callback function?

A

A function that is passed into another function

82
Q

What object is passed into an event listener callback when the event fires?

A

The event object, which is a report on the event

83
Q

What is the event.target? If you weren’t sure, how would you check? Where could you get more information about it?

A

The element where the event we are dealing with originated from. console.log() it. MDN

84
Q

What is the difference between these two snippets of code?

element. addEventListener(‘click’, handleClick)
element. addEventListener(‘click’, handleClick())

A

We are actually calling the function in the second one, which means that the function will run at that line of code, not when the event actually fires

85
Q

What is the className property of element objects?

A

Used to access or update the value of the class attribute on an element

86
Q

How do you update the CSS class attribute of an element using JavaScript?

A

Assign a new value to the className property of that element

87
Q

What is the textContent property of element objects?

A

Used to access or update text content of any DOM element

88
Q

How do you update the text within an element using JavaScript?

A

Assign a new value to the textContent property of the element

89
Q

Is the event parameter of an event listener callback always useful?

A

no

90
Q

Why is storing information about a program in variables better than only storing it in the DOM?

A

We don’t want to check outside of your JavaScript every time you need to access data

91
Q

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

A

focus

92
Q

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

A

blur

93
Q

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

A

input

94
Q

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

A

submit

95
Q

What does the event.preventDefault() method do?

A

Prevents the default behavior of an event from happening

96
Q

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

A

Refresh the page

97
Q

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

A

.elements

98
Q

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

A

.value