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

variable keyword, variable name, assignment operator, and value
ex: var name = ‘Ashley’;

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

use the assignment operator (=)
variable keyword variable name = value
var greeting = ‘hello’;

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, $ or underscore
** however, variables cannot 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

Capitalization is taken into consideration when naming or calling variables
*means that two variables with the same name but different capitalization would be considered two variables
ex: var potato; vs var Potato;

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/manipulate text that js won’t interpret as code

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

represent and manipulate quantities/mathematical operations

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

represents true/false, for decision making

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

a value is being assigned 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

use the variable name with assignment operator (=) and the 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 indicates an intentional absence of a value, like a placeholder
*when you see null, that tells you that someone wanted it to be empty on purpose
undefined means there hasn’t been anything assigned to the variable
*JS way of communicating that there’s nothing there.

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

it provides you with more clarity and can help with debugging

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

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

Numbers

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

What is string concatenation?

A

joins two or more strings together

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

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

A

adds values together
*arithmetically if numbers
*concatenation if 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 += operator do?

A

takes the value to the right of the operator and adds/concatenates it to the variable on the left and assigns the result of that expression 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 variables and functions to create a model of something
Gives you ability to store multiple pieces of data under 1 name

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

What are object properties?

A

variables and their values

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

describe object literal notation

A

variable keyword name of object assignment operator
curly brace followed by properties and methods and their values
end curly brace
ex: var person {
name: ‘ashley’,
age: 29,
ethnicity: ‘vietnamese’
};

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 operator followed by objectname.propertyname;
or
delete operator followed by objectname[‘property.name’];

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

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

A

dot notation
ex: objectname.propertyname;
bracket notation
ex: objectname.[‘propertyname];

if you want to update the value, follow it with an assignment operator and the new value

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

pair of brackets, inside the brackets are values 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

objects store properties and their values while arrays have indexes and values
to add to an object, use dot or bracket notation
to add to an array, use the push method
arrays also have length property while objects do not

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 tells you the numbers 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 the 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

A block of code designed to do a specific task

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
  1. Function keyword
  2. Optional name
  3. Parameters ( 0 or more separated by commas)
  4. Code block
  5. Optional return statement
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
32
Q

Describe the parts of a function’s call.

A
  1. Name of function
  2. Any arguments being passed in between 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 definitions are longer and can have parameters and a keyword
Function calls are much simpler and can pass arguments

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

Parameters are like placeholders in the function definition, arguments are what’s being passed when you call a function

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

Why are function parameters useful?

A

They act as placeholders that allow us to pass arguments when you call 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

exits the code block/prevents any more code in the block from being run
allows the function to produce a value that you can utilize

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

Why do we log things to our console?

A

to observe and communicate with our code
to check our work

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

What is a method?

A

a function that is associated/a property of an 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

you need to state the object you’re calling it off on.
ex: object.method() vs functionName()

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

How do you remove the last element of an array?

A

pop() method

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

floor() method

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

random() method –> gives you a value between 0 and 1 which you multiply by the string length

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
parameters are (start, deletecount, item1, itemN)

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() method –> adds to the end of an array.

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

How do you preppend(add at the beginning) an element from an array?

A

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(separator, limit) method –> takes pattern, divides string into ordered list of substrings, puts strings into array, returns array

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

No bc strings are immutable in JS
Console.log the var the string is assigned to

48
Q

How many string methods and array methods are there according to MDN?

A

Lots

49
Q

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

A

Yes… potentially but the return value is not always necessary so no

50
Q

What 3 letter acronym should you always include in your google search about a JS method or CSS property?

A

MDN

51
Q

What’s the purpose of a loop?

A

Checks a condition so a code block can run

52
Q

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

A

to see if the expression evaluated is true/false so it knows to continue or terminate the loop

53
Q

What does iteration mean in the context of loops?

A

It means to go a round, same steps being repeated multiple times

54
Q

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

A

at the beginning before executing the statement

55
Q

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

A

first and only once

56
Q

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

A

right before each loop iteration/before the final expression

57
Q

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

A

after the condition is found as true

58
Q

Besides a return statement, which exits the entire function block, which keyword exits a loop before its condition expression evaluates to false?

A

break

59
Q

What does the ++ increment operator do?

A

adds 1 to the operand and returns the value

60
Q

How do you iterate through the keys of an object?

A

for… in loop

61
Q

What is a “model”?

A

a blueprint/framework/display/reference

62
Q

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

A

HTML

63
Q

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

A

javascript elements

64
Q

What is a DOM tree?

A

a model of the HTML document and all of it’s parts recreated as javascript objects

65
Q

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

A

getElementByID(‘id’)
querySelector(‘css selector’)

66
Q

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

A

querySelectorAll(‘css selector’)

67
Q

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

A

It stores the location of an element in a variable – saves the browser time

68
Q

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

A

console.dir() –>
dir stands for directory

69
Q

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

A

html browsers load top to bottom
this allows the html content to be loaded prior to the script being downloaded and executed

70
Q

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

A

the name of a selector surrounded by strings/quotes
It returns the first element that matches the specified selector

71
Q

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

A

the name of the css selector
returns all elements that match the specified selector called a node list

72
Q

What is the purpose of events and event handling?

A

To create a dynamic webpage that users can interact with or respond to something that happens

73
Q

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

A

no

74
Q

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

A

addEventListener(‘event’, functionName)

75
Q

What is a callback function?

A

function passed into another function as an argument that will be called at some point

76
Q

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

A

The event object

77
Q

What is the event.target?

A

the place where the event occurred - first element most immediate to where the event occurred

78
Q

What is the difference between these two snippets of code?
element.addEventListener(‘click’, handleClick)
element.addEventListener(‘click’, handleClick())

A

The () indicates that the function is being called immediately rather than when the event fires

79
Q

What is the className property of element objects?

A

It pulls OR sets the value of the class attribute of a specified HTML element

80
Q

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

A

element.className= “name of class”

81
Q

What is the textContent property of element objects?

A

It returns the text content of the element

82
Q

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

A

element.textcontent = text

83
Q

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

A

no

84
Q

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

A

It’s about accessibility, it will be faster to access the variable than the DOM

85
Q

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

A

focus event

86
Q

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

A

Blur event

87
Q

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

A

Input event

88
Q

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

A

submit event

89
Q

What does the event.preventDefault() method do?

A

cancels the default action that belongs to the event (if its cancelable)

90
Q

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

A

it reloads/refreshes the page

91
Q

What property of a form element object contains all of the form controls?

A

element property

92
Q

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

A

value property

93
Q

Does the document.createElement() method insert a new element into the page?

A

No, it only creates it but doesn’t add it to the page

94
Q

How do you add an element as a child to another element?

A

element.appendChild()

95
Q

What do you pass as the arguments to the element.setAttribute() method?

A

name and value

96
Q

What steps do you need to take in order to insert a new element into the page?

A

create the element, create the text node, and append it

97
Q

Name two ways to set the class attribute of a DOM element

A

element.setAttribute()
element.className

98
Q

What are two advantages of defining a function to create something (like the work of creating a DOM tree?

A

allows you to use it over and over
allows you to use it in different locations/context

99
Q

Give two examples of media features that you can query in a @media rule

A

width, height, orientation

100
Q

Which HTML meta tag is used in mobile-responsive web pages?

A

viewport meta tag

101
Q

What is the event.target?

A

The target of the event // basically the element that was interacted with

102
Q

Why is it possible to listen for events on one element that actually happens with its descendent elements?

A

Because of event bubbling

103
Q

What DOM element property tells you what type of element it is?

A

element.target.tagName

104
Q

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

A

it takes a selector as the argument and returns the closest ancestor element or itself that matches the selectors

105
Q

How can you remove an element from the DOM?

A

element.remove() <— element is the one you want to remove

106
Q

If you wanted to insert new clickable DOM elements into the page using JS, how could you avoid adding an event listener to every new element individually?

A

DOM event delegation

107
Q

What is JSON?

A

text based format that represents structured data
* in the form of a string *

108
Q

What are serialization and deserialization?

A

Serialization takes an object in memory into a stream of bytes so you can do stuff with it or send it elsewhere
Deserialization is the opposite: turning the stream of bytes into an object in memory

109
Q

Why are serialization and deserialization useful?

A

It allows you to use data and recreate objects in different locations/platforms

110
Q

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

A

JSON.stringify()

111
Q

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

A

JSON.parse()

112
Q

How do you store data in localStorage?

A

localStorage.setItem(keyName, keyValue)
keyName is in strings

113
Q

How do you retrieve data from localStorage?

A

localStorage.getItem(keyName)
keyName is in strings

114
Q

What data type can localStorage save in the browser?

A

string data types

115
Q

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

A

Before the page unloads which can be refreshing the page or closing the tab
anything that would cause the application to reload of close