JavaScript Flashcards

1
Q

What is the purpose of variables

A

Used to store (remember) data; short-term memory

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 and variable identifier (name)

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

What characters are allowed in variable names?

A

Numbers and letters, $, and underscores; numbers cannot be the first character

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

What does it mean to say that variable names are case sensitive?

A

Casing all needs to be the same or they are not the same

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

What is the purpose of a string

A

Store and manipulate text

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

What is the purpose of a number

A

To contain numeric data for calculations

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

What is the purpose of a Boolean

A

Represents true or false (binary values) useful for decision making

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

What does the = operator mean in JavaScript

A

Assignment operator; assigns a value to a variable

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

How do you update the value of a variable

A

Put the variable name again and using the assignment operator you just change the value

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

What is the difference between null and undefined

A

Null: represents nothing, something that doesn’t exist (intentional nothing put by the programmer)

Undefined: something isn’t there

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

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

A

It’s more clear to see what data is being logged to the console and is a helpful debugging tool

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

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

Two or more strings joined together to make a single 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

Addition or concatenation

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

A

Boolean

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

Addition assignment; adds a value then updates the variable

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

What are objects used for

A

Used to group together sets of variables and functions that have some relation to each other (can include functionality) to model or represent something

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

What are object properties

A

They are variables used within an object

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

Describe object literal notation

A

Curtly braces: sets of key-value pairs (property or method and it’s value separated by a colon) and the key-value pairs which are separated by commas 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 keyword followed by name of the object, member operator (.), and name of property or method you want to delete

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

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

A

Dot notation: object.property/method

Bracket notation: object[property/method]

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

What are arrays used for

A

Storing lists of values related to each other

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

Describe array literal notation

A

Data separated by commas within square brackets

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 have an order and their values are paired with their index (numeric index)

Objects have set properties (alpha-numeric indices) and are just collections of data with no set order

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

What is the length property of an array

A

Array.length

Amount of items in an array

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

What is a function in Javascript

A

A repeatable block of code designed to perform a specific task when it is invoked

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

Describe the parts of a function definition

A

Function keyword, optional function name, comma-separated parameter list of 0 or more parameters, open curly brace for the code block, optional return statement (return keyword followed by optional value), closing curly brace

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

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

A

The function call does not have the function keyword or code block within the curly braces

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

Parameters are placeholders are variables that we declare in the function definition and that have an unknown value until we call the function and pass in the argument(a)

Arguments are actual values that we pass in when calling the function

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

Why are function parameters useful?

A

Allows the function to be reusable

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

Why do we log things to the console?

A

to see what our code is doing, if our code is functioning properly, debugging tool

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

How is a method different from any other function?

A

methods are attached to objects; you need to say specifically where the method is coming from

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

How do you generate a random number?

A

Math.random()

gives you a decimal between 0 and 1 not including 1

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

How do you delete an element from an array?

A

.splice(index, #to delete, optional replacement)

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

How do you append an element to an array?

A

.push() and .unshift()

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

How do you break a string up into an array?

A

.split(separator)

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

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

A

no, because strings are immutable, but you can use the console log to check

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

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

A

~35

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

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

A

~37

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

Give 6 examples of comparison operators.

A

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

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

What data type do comparison expressions evaluate to?

A

booleans

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

What is the purpose of an if statement?

A

to make decisions, to tell the code what to do in certain situations

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

Is else required in order to use an if statement?

A

nope

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

Describe the syntax (structure) of an if statement.

A

if (condition) {
code block;
}

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

What are the three logical operators?

A

&& (and), || (or), ! (not, bang)

54
Q

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

A

using a logical operator (&& or ||) between the two expressions

55
Q

What is the purpose of a loop?

A

repeatable code

56
Q

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

A

to drive the loop to terminate

57
Q

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

A

each individual time the code block runs

58
Q

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

A

before each iteration

59
Q

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

A

BEFORE ANYTHING; evaluated once

60
Q

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

A

after the initialization the first time and then happens every time BEFORE EACH

61
Q

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

A

the very end of the loop, after the statement is executed

AFTER EACH

62
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

63
Q

What does the ++ increment operator do?

A

increments by 1, changes the value of the variable

64
Q

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

A

focus

65
Q

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

A

blur

66
Q

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

A

input

67
Q

What does the event.preventDefault() method do?

A

it prevents the page from reloading when the form is submitted

68
Q

What does the event.preventDefault() method do?

A

it prevents the default (if there is one) from happening

69
Q

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

A

it will reload the page and the data will be attached to the url (default of form is to send the information of that form to the url to store)

70
Q

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

A

value

71
Q

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

A

value

72
Q

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

A

you can see what is happening as you work

73
Q

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

A

you can see what is happening as you work

74
Q

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

A

no, we are just creating the element

75
Q

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

A

parent.appendChild(child)

76
Q

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

A

name, value

77
Q

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

A

createElement, add any additional info to it, append it to the parent (assuming that parent is already part of the page)

78
Q

What is the textContent property of an element object for?

A

to get, set, or update text withing that textNode

79
Q

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

A

setAttribute
className
classList
(be consistent)

80
Q

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

A
  1. allows it to be repeatable
  2. you can give a function a name that makes it easily understandable when reading code
  3. more efficient
81
Q

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

A

aspect ratio

height (of viewport)

82
Q

What is the event.target?

A

it’s the element that the event is firing on; holds the dom element that you most directly interacted with

83
Q

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

A

the element won’t appear on the page; acts as if the element doesn’t exist (removes the element from the document flow)

84
Q

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

A

argument: a string of the css selector you want to select
return: boolean true or false

85
Q

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

A

getAttribute

86
Q

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

A

after every step

87
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

you would have to put an event listener on every single tab

88
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

if statement for each tag

89
Q

What is JSON?

A

(JavaScript Object Notation)

a text-based data format following JavaScript object syntax and it can be used independently from JS

90
Q

What are serialization and deserialization?

A

serialization: converting a native object to a string so it can be transmitted across the network
deserialization: Converting a string to a native object

91
Q

Why are serialization and deserialization useful?

A

for data exchange and storing data without having to rely on it storing as a reference in your computer

it allows you to save the state of an object and convert it to 1s and 0s so it can be transferred over networks and then it can be translated back into an object through deserialization

92
Q

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

A

JSON.stringify( ) method

93
Q

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

A

JSON.parse( ) method

94
Q

How to you store data in localStorage?

A

.setItem of the localStorage item allows us to store in localStorage

95
Q

How to you retrieve data from localStorage?

A

.getItem

96
Q

What data type can localStorage save in the browser?

A

string values

97
Q

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

A

happens before the page will no longer be loaded (closing a tab, closing a browser window, navigating to a new window)

98
Q

What is a method?

A

a function which is a property of an object

99
Q

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

A

method definition has a code block and function keyword whereas method call doesn’t have the code block and you would pass in arguments if necessary

100
Q

Describe method definition syntax (structure).

A

has a function keyword, optional function name followed by a comma-separated list of 0 or more parameters as well as curly braces for the code block

101
Q

Describe method call syntax (structure).

A

the object name, member operator, the name of the function followed by a comma-separated list of 0 or more arguments in parentheses

102
Q

How is a method different from any other function?

A

methods must be attached to an object, you must specify where that method is coming from

** you can reference data within the function

103
Q

What is this in JavaScript?

A

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

if there is no value to the left of the dot when the function is called, then by default, this will be the global window object

** if you cannot see the function being called, then you do not know what the value of this will be

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

105
Q

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

A

call time

106
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

don’t know until call time

107
Q

Given the above character object, what is the result of the following code snippet? Why?

character.greet();

A

“it’s-a-me, Mario!”

why:

108
Q

What is this in JavaScript?

A

A keyword that refers to the object it belongs to

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

if there is no value to the left of the dot when the function is called, then by default, this will be the global window object

** if you cannot see the function being called, then you do not know what the value of this will be)

109
Q

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

A

you can’t tell until the method or function is called

110
Q

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

A

look at the object left of the dot when the function or method is being called

111
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

its nothing because there is nothing using this

112
Q

Given the above character object, what is the result of the following code snippet? Why?

character.greet();

A

“it’s-a-me, Mario!”

why: because this is referring to the character object and getting the firstName property out of that object which is “Mario”; we know it’s character because it’s left to the dot

113
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!”

why: there’s no reference to the value of the variable this. if there’s no dot at call time, the default is “window”

you’re no longer calling that function on the object that includes the this keyword so it’s undefined

114
Q

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

A

look at the object left of the dot when the function or method is being called

115
Q

What kind of inheritance does the JavaScript programming language use?

A

prototypal inheritance

116
Q

What is a prototype in JavaScript?

A

an object that contains properties and (predominantly) methods that can be used by other objects

117
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

because of prototypal inheritance, they borrow the prototype object and its methods

118
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

because of prototypal inheritance, they borrow the prototype object and its methods

*using methods on the prototype object

119
Q

What does the new operator do?

A
  1. creates a blank plain javascript object
  2. adds a property to the new object that links it to the prototype
  3. binds the new object instance as the “this” context
  4. returns this if the function doesn’t return an object
120
Q

What does the new operator do?

A

creates an instance (1 iteration) of a type of thing

  1. creates a blank plain javascript object
  2. adds a property to the new object that links it to the prototype
  3. binds the new object instance as the “this” context
  4. returns this if the function doesn’t return an object
121
Q

What does the instanceof operator do?

A

tests to see if the prototype property of a constructor appears anywhere in the prototype chain of an object

122
Q

What does the instanceof operator do?

A

checks if the object on the right of the operator is a prototype anywhere in the chain

tests to see if the prototype property of a constructor appears anywhere in the prototype chain of an object

returns a boolean value

123
Q

What is a “callback” function?

A

a function passed into another function as data (argument) which is then invoked inside the outer function to perform a specific task

124
Q

Besides adding an event listener callback function to an element or the document, what is one way to delay the execution of a JavaScript function until some point in the future?

A

setTimeout( ) function

125
Q

How can you set up a function to be called repeatedly without using a loop?

A

setInterval( ) function

126
Q

What is the default time delay if you omit the delay parameter from setTimeout() or setInterval()?

A

0

127
Q

What do setTimeout() and setInterval() return?

A

timerId

the delay time, a positive integer value identifying the timer created by the call to setTimeout( ) or setInterval( )

128
Q

What is AJAX?

A

Ajax allows you to request data from a server and load it without having to refresh the entire page

129
Q

What does the AJAX acronym stand for?

A

Asynchronous JavaScript And XML

130
Q

Which object is built into the browser for making HTTP requests in JavaScript?

A

XMLHttpRequest object

131
Q

What event is fired by XMLHttpRequest objects when they are finished loading the data from the server?

A

load events

132
Q

Bonus Question: An XMLHttpRequest object has an addEventListener() method just like DOM elements. How is it possible that they both share this functionality?

A

inheritance possibly?