JavaScript Flashcards

(119 cards)

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
Describe array literal notation.
var Array = [ value1, value2, value3 ];
26
How are arrays different from "plain" objects?
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
27
What number represents the first index of an array?
0 | array[0]
28
What is the length property of an array?
length property is how many values are in the array length of the array
29
How do you calculate the last index of an array?
array.length - 1 var lastIndexOfArray = array[ array.length - 1 ] ;
30
Why do we log things to the console?
Debugging | Developers are able to check for errors as they build their code
31
What is a method?
A function inside of an object
32
How is a method different from any other function?
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.
33
How do you remove the last element from an array?
array.pop() method | pop() removes last element from an array and returns that element
34
How do you round a number down to the nearest integer?
Math.floor() | method
35
How do you generate a random number?
Math.random() method
36
How do you delete an element from an array?
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)
37
How do you append an element to an array?
push() method | push method adds one or more elements to the end of an array and returns the new length of the array
38
How do you break a string up into an array?
split() method' split method divdes a string into an ordered list of substring, puts these substrings into an array, and returns the array
39
Do string methods change the original string? How would you check if you weren't sure?
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
40
Roughly how many string methods are there according to the MDN Web docs?
Around 30-40ish
41
Is the return value of a function or method useful in every situation?
The return value is not always useful in every situation. | Depends on the context it is used in
42
Roughly how many array methods are there according to the MDN Web docs?
Around 30ish
43
What three-letter acronym should you always include in your Google search about a JavaScript method or CSS property?
MDN Mozilla Developer Network
44
Give 6 examples of comparison operators.
``` < > === !== >= <= ```
45
What data type do comparison expressions evaluate to?
Boolean (True or False)
46
What is the purpose of an if statement?
Test whether a condition is true or false and then proceeds to do any statement in its code block if true
47
Is else required in order to use an if statement?
Else is not required to use an if statement
48
Describe the syntax (structure) of an if statement.
if ( condition ) { statement }
49
What are the three logical operators?
|| Or && And ! Not
50
How do you compare two different expressions in the same condition?
With the two logical operators: && And || Or
51
What is the purpose of a loop?
To run a code block a certain amount of times based on its condition
52
What is the purpose of a condition expression in a loop?
Condition expression in a loop instructs the loop to run a specific amount of times
53
What does "iteration" mean in the context of loops?
Iteration refers to the specific amount of the times the loop has already cycled through
54
When does the condition expression of a while loop get evaluated?
At the start of every iteration of the while loop
55
When does the initialization expression of a for loop get evaluated?
At the beginning of the loop
56
When does the condition expression of a for loop get evaluated?
At the start of every iteration of the for loop
57
When does the final expression of a for loop get evaluated?
At the end of an iteration of the loop | basically bottom of the code block of the loop
58
Besides a return statement, which exits its entire function block, which keyword exits a loop before its condition expression evaluates to false?
Break keyword
59
What does the ++ increment operator do?
The ++ operator increments a variable by 1
60
How do you iterate through the keys of an object?
For in loop for ( keys in object) { Will cycle through every property in the object }
61
What event is fired when a user places their cursor in a form control?
focus event is fired
62
What event is fired when a user's cursor leaves a form control?
blur event is fired
63
What event is fired as a user changes the value of a form control?
input event is fired
64
What event is fired when a user clicks the "submit" button within a < form > ?
submit event is fired
65
What does the event.preventDefault() method do?
Prevents the default behavior for an event
66
What does submitting a form without event.preventDefault() do?
It would refresh the page
67
What property of a form element object contains all of the form's controls.
elements property holds all of the form's control
68
What property of a form control object gets and sets its value?
Value property
69
What is one risk of writing a lot of code without checking to see if it works so far?
finishing the code and then realizing the entire block does not work
70
What is an advantage of having your console open when writing a JavaScript program?
Able to check if your code is working as you write it | Able to make changes to code and see it working realtime
71
Does the document.createElement() method insert a new element into the page?
No it does not | It creates a new element node inside the DOM but is not attached to the DOM yet
72
How do you add an element as a child to another element?
appendChild( ) method
73
What do you pass as the arguments to the element.setAttribute() method?
attribute title and name
74
What steps do you need to take in order to insert a new element into the page?
.querySelect( ) the parent element .createElement( ) .appendChild( ) the element created to the parent element
75
What is the textContent property of an element object for?
1) To append text to a node | 2) To get textContent from an element
76
Name two ways to set the class attribute of a DOM element.
.setAttribute( ) method .className( ) method
77
What are two advantages of defining a function to do create something (like the work of creating a DOM tree)?
Makes it reusable and saves time so that you don't have to repeat the same function over and over again
78
What is the event.target?
where the HTML element originated from
79
Why is it possible to listen for events on one element that actually happen its descendent elements?
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
What DOM element property tells you what type of element it is?
.tagName event.target.tagName
81
What does the element.closest() method take as its argument and what does it return?
Traverses the element and its parents until it finds a node that matches the specified CSS selector
82
How can you remove an element from the DOM?
Call the remove( ) method
83
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?
Add event listener to the parent( ) and look for tag name of the specific element
84
What is the event.target?
The element that triggers the event
85
What is the affect of setting an element to display: none?
Removes the element from the document flow completely No longer visible
86
What does the element.matches() method take as an argument and what does it return?
returns a boolean of whether the DOM element matches the selected element in the argument
87
How can you retrieve the value of an element's attribute?
getAttribute( ) method
88
At what steps of the solution would it be helpful to log things to the console?
At every step of the solution
89
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 for each individual element`
90
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?
Would have a long code block to check each condition manually
91
What is JSON?
JavaScript Object Notation
92
What are serialization and deserialization?
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
Why are serialization and deserialization useful?
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
How do you serialize a data structure into a JSON string using JavaScript?
JSON.stringify( ) method return is a string
95
How do you deserialize a JSON string into a data structure using JavaScript?
JSON.parse( ) method
96
What is a method?
A function stored inside of an object
97
How can you tell the difference between a method definition and a method call?
A method definition will include an entire function definition while a method call will be object.methodName + ( )
98
Describe method definition syntax (structure).
Any function definition being assigned.to a property (usually an anonymous function)
99
Describe method call syntax (structure).
Object name. propertyName holding the function ( )
100
How is a method different from any other function?
A method is stored inside a property of an object
101
What is the defining characteristic of Object-Oriented Programming?
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
What are the four "principles" of Object-Oriented Programming?
Abstraction Encapsulation Inheritance Polymorphism
103
What is "abstraction"?
Simpler way to access complex behavior Idea of abstraction is to "simplify"
104
What does API stand for?
Application Programming Interface
105
What is the purpose of an API?
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
What is this in JavaScript?
'this' refers to the object you are currently working with
107
What does it mean to say that this is an "implicit parameter"?
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
When is the value of this determined in a function; call time or definition time?
this only has a value when it is running Call time
109
``` 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(); ```
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
How can you tell what the value of this will be for a particular function or method definition?
In a definition, you can't know. | If you're not using it, you are unable to know for sure.
111
What does the new operator do?
Creates a blank plain javascript object Points it to the constructor function's prototype property Executes the constructor function
112
What property of JavaScript functions can store shared behavior for instances created with new?
Using the prototype property
113
What does the instanceof operator do?
Checks whether the object contains the stated prototype
114
How can you tell what the value of this is for a particular function or method call?
value of this is the object to the left of the method call
115
What kind of inheritance does the JavaScript programming language use?
Prototype based inheritance | or prototypal inheritance
116
What is a prototype in JavaScript?
117
What is a "callback" function?
118
What is Array.prototype.filter useful for?
Creating a new array while excluding some elements.
119
What is Array.prototype.map useful for?
Creating a new array containing the transformed elements of another.