JavaScript Flashcards

1
Q

What is the purpose of variables?

A

Variables store data that will be used in the future

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 variableName

var keyword + variable name

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

Assignment operator ( = )

var variableName = expression;

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

all Alphanumeric characters and the underscore symbol

tip: can’t start variable names with number and recommended to not start with a capital

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

Uppercased letters and lowercased letters are completely different in the case of names

var Apple is not the same as var apple

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

String stores text data

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

number stores number data for a mathematical expression

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

Boolean is used for yes or no situations

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

Assignment operator ( = )

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

Assigning the variableName with a new expression or value

var A = 3
A = 6

variable A is now 6

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 is used when the developer intentionally wants a variable to be empty.

undefined occurs when a variable has not been assigned a value and is most oftentimes a bug in the code

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

console logging “labels” makes it easier for debugging when checking through many different variables

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

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

number data type

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

What is string concatenation?

A

String concatenation refers to the ( + ) in a string expression

Attaches a string value to another string value

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

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

A

Addition-Assignment Operator ( += )

Adds/Concatenates the left operand with a number/string and assigns the value back to the left operand

motto += 2
motto = motto + 2

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

What are objects used for?

A

Objects are used to sort and categorize sets of data

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

What are object properties?

A

Object properties are names of each category the data will be sorted into

{
propertyName: propertyValue;
}

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

Describe object literal notation.

A

var objectName = { propertyName: propertyValue, property2Name: property2Value };

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

Use delete keyword to remove property from an object

var object = { property1: property1Value };
delete object.property1;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

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

A

dot notation => object.property1

bracket notation => object[‘property1’]

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

What are arrays used for?

A

Arrays are used for creating/sorting/organizing data into ordered lists

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

Describe array literal notation.

A

var Array = [ value1, value2, value3 ];

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

Arrays are ordered while objects are not

Arrays do not have titles/property names for each value while objects do

Array values can be called upon based on their order in the list while Object values must use specific names of their properties to call upon values

Length of an array can easily be accessed while object’s cannot

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

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

length property is how many values are in the array

length of 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 an array?

A

array.length - 1

var lastIndexOfArray = array[ array.length - 1 ] ;

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

Why do we log things to the console?

A

Debugging

Developers are able to check for errors as they build their code

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

What is a method?

A

A function inside of an object

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

How is a method different from any other function?

A

A method is a function inside of an object.
JavaScript method is an object property that has a function value while a function is a block of code designed to perform a particular task.

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

How do you remove the last element from an array?

A

array.pop() method

pop() removes last element from an array and returns that element

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

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

A

Math.floor()

method

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

How do you generate a random number?

A

Math.random() method

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

How do you delete an element from an array?

A

splice() method

splice method changes contents of an array by removing or replacing existing elements and/or adding new elements in place

splce(start, deleteCount, item1, itemN)

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

How do you append an element to an array?

A

push() method

push method adds one or more elements to the end of an array and returns the new length of the array

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

How do you break a string up into an array?

A

split() method’
split method divdes a string into an ordered list of substring, puts these substrings into an array, and returns the array

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

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

A

String methods don’t normally change the original string and you can console.log the original string after the method has been used to make sure

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

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

A

Around 30-40ish

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

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

A

The return value is not always useful in every situation.

Depends on the context it is used in

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

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

A

Around 30ish

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

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

A

MDN Mozilla Developer Network

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

Give 6 examples of comparison operators.

A
<
>
===
!==
>=
<=
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
45
Q

What data type do comparison expressions evaluate to?

A

Boolean (True or False)

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

What is the purpose of an if statement?

A

Test whether a condition is true or false and then proceeds to do any statement in its code block if true

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

Is else required in order to use an if statement?

A

Else is not required to use an if statement

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

Describe the syntax (structure) of an if statement.

A

if ( condition ) { statement }

49
Q

What are the three logical operators?

A

|| Or
&& And
! Not

50
Q

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

A

With the two logical operators:
&& And
|| Or

51
Q

What is the purpose of a loop?

A

To run a code block a certain amount of times based on its condition

52
Q

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

A

Condition expression in a loop instructs the loop to run a specific amount of times

53
Q

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

A

Iteration refers to the specific amount of the times the loop has already cycled through

54
Q

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

A

At the start of every iteration of the while loop

55
Q

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

A

At the beginning of the loop

56
Q

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

A

At the start of every iteration of the for loop

57
Q

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

A

At the end of an iteration of the loop

basically bottom of the code block of the loop

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

59
Q

What does the ++ increment operator do?

A

The ++ operator increments a variable by 1

60
Q

How do you iterate through the keys of an object?

A

For in loop

for ( keys in object) {
Will cycle through every property in the object
}

61
Q

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

A

focus event is fired

62
Q

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

A

blur event is fired

63
Q

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

A

input event is fired

64
Q

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

A

submit event is fired

65
Q

What does the event.preventDefault() method do?

A

Prevents the default behavior for an event

66
Q

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

A

It would refresh the page

67
Q

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

A

elements property holds all of the form’s control

68
Q

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

A

Value property

69
Q

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

A

finishing the code and then realizing the entire block does not work

70
Q

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

A

Able to check if your code is working as you write it

Able to make changes to code and see it working realtime

71
Q

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

A

No it does not

It creates a new element node inside the DOM but is not attached to the DOM yet

72
Q

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

A

appendChild( ) method

73
Q

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

A

attribute title and name

74
Q

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

A

.querySelect( ) the parent element
.createElement( )
.appendChild( ) the element created to the parent element

75
Q

What is the textContent property of an element object for?

A

1) To append text to a node

2) To get textContent from an element

76
Q

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

A

.setAttribute( ) method

.className( ) method

77
Q

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

A

Makes it reusable and saves time so that you don’t have to repeat the same function over and over again

78
Q

What is the event.target?

A

where the HTML element originated from

79
Q

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

A

Event delegation allows the developer to traverse up the DOM tree and select ancestral elements based on relation to the element and more specifically tag names

80
Q

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

A

.tagName

event.target.tagName

81
Q

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

A

Traverses the element and its parents until it finds a node that matches the specified CSS selector

82
Q

How can you remove an element from the DOM?

A

Call the remove( ) method

83
Q

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

A

Add event listener to the parent( ) and look for tag name of the specific element

84
Q

What is the event.target?

A

The element that triggers the event

85
Q

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

A

Removes the element from the document flow completely

No longer visible

86
Q

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

A

returns a boolean of whether the DOM element matches the selected element in the argument

87
Q

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

A

getAttribute( ) method

88
Q

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

A

At every step of the solution

89
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 for each individual element`

90
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

Would have a long code block to check each condition manually

91
Q

What is JSON?

A

JavaScript Object Notation

92
Q

What are serialization and deserialization?

A

Serialization: Converting primitive data to serialized data ( series of bytes )
Deserialization: Converting the serialized data back to primitive data

In JavaScript, serialized data is string format

93
Q

Why are serialization and deserialization useful?

A

Serialization: To take complex data and put it in a format that is easily transmissable to somewhere else

Deserialization: Serialized data is not efficient to format and deserialization allows interacting with data more usable

94
Q

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

A

JSON.stringify( ) method

return is a string

95
Q

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

A

JSON.parse( ) method

96
Q

What is a method?

A

A function stored inside of an object

97
Q

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

A

A method definition will include an entire function definition while a method call will be object.methodName + ( )

98
Q

Describe method definition syntax (structure).

A

Any function definition being assigned.to a property (usually an anonymous function)

99
Q

Describe method call syntax (structure).

A

Object name. propertyName holding the function ( )

100
Q

How is a method different from any other function?

A

A method is stored inside a property of an object

101
Q

What is the defining characteristic of Object-Oriented Programming?

A

Create objects where data and behavior that utilizes that data are in a shared space
So now methods can just access the data attached to the object that that method is attached to
Saves the need for parameters frequently

Object can contain both data (as properties) and behavior (as methods)

102
Q

What are the four “principles” of Object-Oriented Programming?

A

Abstraction

Encapsulation

Inheritance

Polymorphism

103
Q

What is “abstraction”?

A

Simpler way to access complex behavior

Idea of abstraction is to “simplify”

104
Q

What does API stand for?

A

Application
Programming
Interface

105
Q

What is the purpose of an API?

A

Set of functionality that’s been created to allow you to access functionality on some resource

Set of tools that have been created to allow you to access functionality of a complex system easily

The DOM is an example of an API

106
Q

What is this in JavaScript?

A

‘this’ refers to the object you are currently working with

107
Q

What does it mean to say that this is an “implicit parameter”?

A

A value provided to the function when it is invoked that is not listed in the parameter list but will always be there

A parameter you don’t have to state in your parameter list

108
Q

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

A

this only has a value when it is running

Call time

109
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);
  }
};

Given the above character object, what is the result of the following code snippet? Why?
character.greet();

Given the above character object, what is the result of the following code snippet? Why?
var hello = character.greet;
hello();
A

this does not refer to anything yet because it has not been called

The string ‘it’s a me Mario!” will be outputted to the console

“It’s a me undefined!” because this acquires the property to the right of hello which in this case there is none which translates to the window object

110
Q

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

A

In a definition, you can’t know.

If you’re not using it, you are unable to know for sure.

111
Q

What does the new operator do?

A

Creates a blank plain javascript object
Points it to the constructor function’s prototype property
Executes the constructor function

112
Q

What property of JavaScript functions can store shared behavior for instances created with new?

A

Using the prototype property

113
Q

What does the instanceof operator do?

A

Checks whether the object contains the stated prototype

114
Q

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

A

value of this is the object to the left of the method call

115
Q

What kind of inheritance does the JavaScript programming language use?

A

Prototype based inheritance

or prototypal inheritance

116
Q

What is a prototype in JavaScript?

A
117
Q

What is a “callback” function?

A
118
Q

What is Array.prototype.filter useful for?

A

Creating a new array while excluding some elements.

119
Q

What is Array.prototype.map useful for?

A

Creating a new array containing the transformed elements of another.