JAVASCRIPT Flashcards

(116 cards)

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
How are arrays different from "plain" objects?
arrays are numerically indexed, whereas object properties are alphanumerically indexed
26
What is the length property of an array?
checks how many items are in an array var numColors = array.length
27
What is the length property of an array?
checks how many items are in an array, numerically var numColors = array.length
28
What is a function in JS?
a series of statements we can call to perform a task
29
Why do we log things to the console?
- to help debug | - visualize the data
30
What is a method?
a function, which is a property of an object
31
How is a method different from any other function?
object that already contains a function definition
32
What is the difference between a parameter and an argument?
A parameter is the variable local to the function The argument is the actual value itself that you are inputting into the function call
33
Why are function parameters useful?
parameters are placeholders and already hold the value of the argument
34
What two effects does a return statement have on the behavior of a function?
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.
35
How do you calculate the last index of an array?
last index = array.length - 1
36
What is a method?
a function, which is a property of an object an object reference to a function?
37
How do you break a string up into an array?
string.split(separator) ex: string.split(' ') if you
38
How do you remove the last element from an array?
array.pop();
39
How do you round a number down to the nearest integer?
Math.floor();
40
Is the return value of a function or method useful in every situation?
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
41
Roughly how many array methods are there according to the MDN Web docs?
40-ish
42
What three-letter acronym should you always include in your Google search about a JavaScript method or CSS property?
MDN
43
How do you break a string up into an array?
string. split(separator) | ex: string.split(' ')
44
Do string methods change the original string? How would you check if you weren't sure?
no because the value is assigned to a different variable you can console log the original string to make sure
45
Roughly how many string methods are there according to the MDN Web docs?
50
46
Is the return value of a function or method useful in every situation?
no
47
Roughly how many array methods are there according to the MDN Web docs?
50?
48
What three-letter acronym should you always include in your Google search about a JavaScript method or CSS property?
MDN?
49
Give 6 examples of comparison operators.
``` > < >= <= != is not equal to == (preferable to strict method) ```
50
What data type do comparison expressions evaluate to?
booleans or true or false
51
What is the purpose of a condition expression in a loop?
to let the loop know when to stop running
52
Is else required in order to use an if statement?
no. nothing will run
53
Describe the syntax (structure) of an if statement.
``` keyword if (condition - operand, logical expression) { code block }; ```
54
What are the three logical operators?
&& logical and || logical or ! logical not
55
How do you compare two different expressions in the same condition?
else if | or logical comparison operators
56
What is the purpose of a loop?
to run a code block various times, so long as the conditions are true useful for iterating through an array
57
What is the purpose of a condition expression in a loop?
it instructs the code to run a specified number of times
58
What does "iteration" mean in the context of loops?
how many time the loop has run
59
When does the condition expression of a while loop get evaluated?
before each pass through the loop
60
When does the initialization expression of a for loop get evaluated?
once before the loop begins
61
When does the condition expression of a for loop get evaluated?
before each loop iteration
62
When does the final expression of a for loop get evaluated?
at the end of each loop iteration occurs before the next evaluation of condition generally used to update or increment counter
63
Besides a return statement, which exits its entire function block, which keyword exits a loop before its condition expression evaluates to false?
break
64
What does the ++ increment operator do?
increases the counter by 1
65
How do you iterate through the keys of an object?
using a for... in loop for (let key in object) { }
66
Why do we log things to the console?
to make sure our code is working properly to inspect and debug
67
What is a "model"?
a representation or replica of something a data structure
68
Which "document" is being referred to in the phrase Document Object Model?
the web page being loaded
69
What is the word "object" referring to in the phrase Document Object Model?
different part of the page loaded in the browser window
70
What is a DOM Tree?
the model of the web page being loaded made of objects
71
Give two examples of document methods that retrieve a single element from the DOM.
getElementById() - uses the value of an element's id attribute (should be unique) querySelector() - uses CSS selector and returns the first matching element
72
Give one example of a document method that retrieves multiple elements from the DOM at once.
``` getElementByClassName() - specific to class attribute - returns in live node list ``` querySelectorAll() - uses CSS selector to select all matching elements
73
Why might you want to assign the return value of a DOM query to a variable?
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
What console method allows you to inspect the properties of a DOM element object?
console.dir() displays interactive list of all properties of specified JS object
75
Why would a tag need to be placed at the bottom of the HTML content instead of at the top?
so that the browser loads all html data first page loads to render all dom elements to work with
76
What does document.querySelector() take as its argument and what does it return?
argument - css selector ex: querySelectorAll('li.hot') returns - first element that matches this
77
What does document.querySelectorAll() take as its argument and what does it return?
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
What is the purpose of events and event handling?
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
Are all possible parameters required to use a JavaScript method or function?
no?
80
What method of element objects lets you set up a function to be called when a specific type of event occurs?
element.addEventListener()
81
What is a callback function?
a function passed into another function as an argument
82
What is the event.target? If you weren't sure, how would you check? Where could you get more information about it?
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
What is the difference between these two snippets of code? element. addEventListener('click', handleClick) element. addEventListener('click', handleClick())
The second will not work because although it is the function name, there should not be parenthesis there (that will call the function)
84
What is the className property of element objects?
accesses the name of the class of the element
85
How do you update the CSS class attribute of an element using JavaScript?
$element.className = 'hot-button cool'
86
What is the textContent property of element objects?
accesses the text content of the element
87
How do you update the text within an element using JavaScript?
document.querySelector('.click-count').textContent = ' '
88
Is the event parameter of an event listener callback always useful?
No
89
Would this assignment be simpler or more complicated if we didn't use a variable to keep track of the number of clicks?
More complicated
90
Why is storing information about a program in variables better than only storing it in the DOM?
it's easier for programmers to access and work with
91
What event is fired when a user places their cursor in a form control?
focus
92
What event is fired when a user's cursor leaves a form control?
blur
93
What event is fired as a user changes the value of a form control?
input
94
What event is fired when a user clicks the "submit" button within a ?
submit
95
What does the event.preventDefault() method do?
cancels the event if it's cancelable / the default action will not occur ex: clicking on submit will not submit the form
96
What does submitting a form without event.preventDefault() do?
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
What property of a form element object contains all of the form's controls.
HTMLFormElement.elements
98
What property of a form control object gets and sets its value?
$form.elements.name.value
99
What is one risk of writing a lot of code without checking to see if it works so far?
you have more to backtrack, console log, and look through
100
What is an advantage of having your console open when writing a JavaScript program?
you can test your code as you are writing it
101
What is the event.target?
returns the element/object that triggered the event | remember DOM -- all elements are objects
102
What does the element.matches() method take as an argument and what does it return?
takes - a string containing CSS selectors | returns - true or false
103
How can you retrieve the value of an element's attribute?
getAttribute() method of the element $element.getAttribute(attributeName) returns: a string containing the value of the attribute name
104
At what steps of the solution would it be helpful to log things to the console?
every step
105
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?
add event listeners to each node, instead of just the one on the parent node -- which bubbles events to child elements
106
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?
create event listeners for each tab and change class names
107
display: none
it'll hide the element without deleting it | removes it from flow of the page
108
What is JSON?
JS Object Notation | text-based format for representing data
109
What are serialization and deserialization?
serialization - converting object into a string | deserialization - converting back into its native object
110
Why are serialization and deserialization useful?
- so it can be transmitted across the network easily - stored as its own txt file - sending data from server to client
111
How do you serialize a data structure into a JSON string using JavaScript?
JSON.stringify() - converts JS object into a JSON string
112
How do you deserialize a JSON string into a data structure using JavaScript?
JSON.parse() - converts JSON string back into a JS object
113
How do you store data in localStorage?
localStorage.setItem()
114
How do you retrieve data from localStorage?
localStorage.getItem()
115
What data type can localStorage save in the browser?
json strings
116
When does the 'beforeunload' event fire on the window object?
when the window, the document and its resources are about to be unloaded (like refreshing or leaving the page)