JAVASCRIPT Flashcards

1
Q

What is the purpose of variables?

A

to store data (short-term memory)

it can change or vary each time a script runs

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 keyword var name;
ex: var quantity;

Var (old school), const, and let (if it’ll change)

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

quantity = 3;

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

Upper or lowercase letter, dollar sign, or underscore

you can use numbers, but not as the 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

if it is capitalized, you need to capitalize it always in order for it to be recognized as the same variable

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 text (always within quotation marks) – letters and other 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

counting, math, representing quantities, etc

  • determining size of screen
  • moving position of element on a page
  • setting amount of time an element should take to fade in
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

true/false values

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’s an assignment operator, not an ‘equal to’

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

use the equal sign. make sure it comes after the initial declaration

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

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

A

it makes it clearer what variable is being logged and in what order

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

Give five examples of JavaScript primitives.

A
string
number
boolean
null
undefined
symbol
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
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
14
Q

What is string concatenation?

A

combines separate strings together to form one string

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

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

A

to add one value to another

concatenates

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

What data type is returned by comparing two values (, ===, etc)?

A

boolean (true or false)

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

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

A

it’s a shorthand for the addition assignment

if you know you are going to take the sum and add to it, you can use this

instead of x = x + y, it becomes x += y

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

What are objects used for?

A

group together a set of variables and functions to create a model of something

ex: object is hotel

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

What are object properties?

A

variables

ex:
- name:
- rooms:
- booked:

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

Describe object literal notation.

A
var object = {
key: value,
key: value,
method;
}

each is separated with a comma, except for the last one

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

How do you remove a property from an object?

A

delete object.property;

to clear the value:
object.property = ‘’;

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

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

A

dot notation
object.property = new value;

brackets notation
object[‘property’].= new value

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

What are arrays used for?

A

working with a list or set of values that are related to each other. especially when you don’t know how many items/values there will be

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

Describe array literal notation.

A

var arrayName;
arrayName = [‘value’, ‘value];

var color = arrayName[0]

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

How are arrays different from “plain” objects?

A

arrays are numerically indexed, whereas object properties are alphanumerically indexed

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

What is the length property of an array?

A

checks how many items are in an array

var numColors = array.length

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

What is the length property of an array?

A

checks how many items are in an array, numerically

var numColors = array.length

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

What is a function in JS?

A

a series of statements we can call to perform a task

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

Why do we log things to the console?

A
  • to help debug

- visualize the data

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

What is a method?

A

a function, which is a property of an object

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

How is a method different from any other function?

A

object that already contains a function definition

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

What is the difference between a parameter and an argument?

A

A parameter is the variable local to the function

The argument is the actual value itself that you are inputting into the function call

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

Why are function parameters useful?

A

parameters are placeholders and already hold the value of the argument

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

How do you calculate the last index of an array?

A

last index = array.length - 1

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

What is a method?

A

a function, which is a property of an object

an object reference to a function?

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

How do you break a string up into an array?

A

string.split(separator)

ex: string.split(‘ ‘)
if you

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

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

A

Math.floor();

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

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

A

yes but no

for example when you are using methods to change an array, you may want to double check what element you are deleting, but not necessarily

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

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

A

40-ish

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

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

A

MDN

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

How do you break a string up into an array?

A

string. split(separator)

ex: string.split(‘ ‘)

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

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

A

no because the value is assigned to a different variable

you can console log the original string to make sure

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

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

A

50

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

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

A

no

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

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

A

50?

48
Q

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

A

MDN?

49
Q

Give 6 examples of comparison operators.

A
>
<
>=
<=
!= is not equal to
== (preferable to strict method)
50
Q

What data type do comparison expressions evaluate to?

A

booleans or true or false

51
Q

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

A

to let the loop know when to stop running

52
Q

Is else required in order to use an if statement?

A

no. nothing will run

53
Q

Describe the syntax (structure) of an if statement.

A
keyword if (condition - operand, logical expression) {
code block
};
54
Q

What are the three logical operators?

A

&& logical and
|| logical or
! logical not

55
Q

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

A

else if

or logical comparison operators

56
Q

What is the purpose of a loop?

A

to run a code block various times, so long as the conditions are true

useful for iterating through an array

57
Q

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

A

it instructs the code to run a specified number of times

58
Q

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

A

how many time the loop has run

59
Q

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

A

before each pass through the loop

60
Q

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

A

once before the loop begins

61
Q

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

A

before each loop iteration

62
Q

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

A

at the end of each loop iteration

occurs before the next evaluation of condition

generally used to update or increment counter

63
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

64
Q

What does the ++ increment operator do?

A

increases the counter by 1

65
Q

How do you iterate through the keys of an object?

A

using a for… in loop

for (let key in object) {
}

66
Q

Why do we log things to the console?

A

to make sure our code is working properly

to inspect and debug

67
Q

What is a “model”?

A

a representation or replica of something

a data structure

68
Q

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

A

the web page being loaded

69
Q

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

A

different part of the page loaded in the browser window

70
Q

What is a DOM Tree?

A

the model of the web page being loaded

made of objects

71
Q

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

A

getElementById() - uses the value of an element’s id attribute (should be unique)

querySelector() - uses CSS selector and returns the first matching element

72
Q

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

A
getElementByClassName() - specific to class attribute
- returns in live node list

querySelectorAll() - uses CSS selector to select all matching elements

73
Q

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

A

easier for when you are working with an element more than once

saves the browser from looking through the DOM tree to find the same elements again (caching the selection)

74
Q

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

A

console.dir()

displays interactive list of all properties of specified JS object

75
Q

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

A

so that the browser loads all html data first

page loads to render all dom elements to work with

76
Q

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

A

argument - css selector
ex: querySelectorAll(‘li.hot’)

returns - first element that matches this

77
Q

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

A

argument - css selector
ex: querySelectorAll(‘li.hot’)

returns - all elements that match the css selector syntax

node list, which returns one object for each element

instead of null, it will also return an empty node list

static node list

78
Q

What is the purpose of events and event handling?

A

events = user interaction with web pages and sites

event handling allows us to design sites that are responsive, interactive, and give data about a user’s need

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

element.addEventListener()

81
Q

What is a callback function?

A

a function passed into another function as an argument

82
Q

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

A

it tells you what the target of the event is

if you weren’t sure, you could open the source –> console –> event dropdown –> read the target attribute

83
Q

What is the difference between these two snippets of code?

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

A

The second will not work because although it is the function name, there should not be parenthesis there (that will call the function)

84
Q

What is the className property of element objects?

A

accesses the name of the class of the element

85
Q

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

A

$element.className = ‘hot-button cool’

86
Q

What is the textContent property of element objects?

A

accesses the text content of the element

87
Q

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

A

document.querySelector(‘.click-count’).textContent = ‘ ‘

88
Q

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

A

No

89
Q

Would this assignment be simpler or more complicated if we didn’t use a variable to keep track of the number of clicks?

A

More complicated

90
Q

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

A

it’s easier for programmers to access and work with

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 ?

A

submit

95
Q

What does the event.preventDefault() method do?

A

cancels the event if it’s cancelable / the default action will not occur

ex: clicking on submit will not submit the form

96
Q

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

A

allows default behavior to occur

example: form values will be added to the end of the url

  • it will refresh the page and you lose the data
  • you will also be showing passwords in the URL, which is not secure
97
Q

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

A

HTMLFormElement.elements

98
Q

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

A

$form.elements.name.value

99
Q

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

A

you have more to backtrack, console log, and look through

100
Q

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

A

you can test your code as you are writing it

101
Q

What is the event.target?

A

returns the element/object that triggered the event

remember DOM – all elements are objects

102
Q

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

A

takes - a string containing CSS selectors

returns - true or false

103
Q

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

A

getAttribute() method of the element

$element.getAttribute(attributeName)
returns: a string containing the value of the attribute name

104
Q

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

A

every step

105
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

add event listeners to each node, instead of just the one on the parent node – which bubbles events to child elements

106
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

create event listeners for each tab and change class names

107
Q

display: none

A

it’ll hide the element without deleting it

removes it from flow of the page

108
Q

What is JSON?

A

JS Object Notation

text-based format for representing data

109
Q

What are serialization and deserialization?

A

serialization - converting object into a string

deserialization - converting back into its native object

110
Q

Why are serialization and deserialization useful?

A
  • so it can be transmitted across the network easily
  • stored as its own txt file
  • sending data from server to client
111
Q

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

A

JSON.stringify() - converts JS object into a JSON string

112
Q

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

A

JSON.parse() - converts JSON string back into a JS object

113
Q

How do you store data in localStorage?

A

localStorage.setItem()

114
Q

How do you retrieve data from localStorage?

A

localStorage.getItem()

115
Q

What data type can localStorage save in the browser?

A

json strings

116
Q

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

A

when the window, the document and its resources are about to be unloaded (like refreshing or leaving the page)