JavaScript Flashcards

1
Q

What is the purpose of variables?

A

Gives us a way to call data later

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

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
Set the variable equal to the desired value
var varName = value;
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

Any number, letter, _ , or $

But they must start with a letter, underscore (_), or $

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

You must capitalize the variable’s spelling the same, in order to use it
var weTall is not the same as var wetall

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

Stings are the form of data that allow you to enter a chain of text.

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

Numbers are used for calculations and placement

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

Booleans are true and false

They are used to decide whether an action should be taken or not

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 assigns a value to a variable

The 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
Set it equal to a different value, lower in the code
var name = 'old name';
name = 'new name';
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 means the items do not exist yet. Its data type is an object which was defined by a developer intentionally.

Undefined means the variable has not been given a value yet. Its data type undefined.

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

You can know, in the console, what you are logging and make sure you are referring to the correct data.

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

Give five examples of JavaScript primitives.

A
Numbers
String
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

arithmetic: involving numbers (numeric)

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

What is string concatenation?

A

Adding two or more strings to make one new, larger string

‘Sting’ + ‘ adding’ = ‘Strings adding’

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

Adding numbers together

Concatenating 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

Booleans

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 the item on the right, to the item on the left. THEN it replaced the item on the left with the new value

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

What are objects used for?

A

Give us a way to collect our data into smaller groups

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

What are object properties?

A

The items within an object. Keys & their values

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

Describe object literal notation.

A

var varName = {
property: value,
property: value
}

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 object.property;

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.property = x

Bracket notation: object[‘property’] = x

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

What are arrays used for?

A

To make a series of data you can tie to a specific number

When you dont know how many pieces of that data you need

Groups of data that have the same type of data

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

Describe array literal notation.

A

var varName = [‘item’, ‘item’, ‘item’]

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

The values are assigned to counted indexes, as opposed to random, unordered properties

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

Arrays start on 0 and count up

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

arrayName.length gives the total number of an 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

Since the array indexing starts at 0, to get the last value in your array, you can subtract 1 from the array.length

var endItem = arrayName.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 faster way to access a chain of commands we need to use once or several times, throughout our program

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
function sayHello(name) {
	var $fullGreeting = "Hello, " + name;
	return $fullGreeting;
}
var fullGreeting = sayHello("John");
console.log(fullGreeting);
--------------------------------------------------
"Hello, John"
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
32
Q

Describe the parts of a function call.

A

function: function keyword
functionName
(paramaters, parameters)
{….code….}: code block

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

Definition: This would be to console the function without () at the end. The result would be the full view of the function.

Calling the function would be to run it and expect a result. This is done by adding () at the end of the function’s name.

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 go into the structure of the function. They act as placeholders for the data users will be inputting

Arguments are the actual data users input which, will be used in our function accordingly.

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

Why are function parameters useful?

A

Parameters are the placeholders for functions.

They will be replaced in the code block with the data users provide.

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
It sends the desired variable out of the scope of the function, which can then be used by another variable:
    function greet (name) {
        var greeting =  "Hello, " + name;
        return myName;
    }
    var sayHi = greet('john');
    console.log(sayHi);

It ends the code block from running any lines below it.

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

Why do we log things to the console?

A

We log things to the console to make sure the value of an object is as intended.

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

What is a method?

A

A method is a function stored as a property inside 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

A method is a function saved onto an object

We call them by naming the object, adding a DOT, and then the name of the method

sting.method();

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

How do you remove the last element from an array?

A

array.push()

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

Math.floor()

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

Math.random();

The number is a decimal between 0 and 1

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

.pop() remove last item

.shift() removed first

.splice(startIndex, # to delete, items to insert)

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() add to last

.unshift() add to first

.splice(startIndex, # to delete, items to insert)

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

How do you break a string up into an array?

A

array.split(‘separate indicator’);

If you want to separate each word, the separate indicator would be (“ “)

Each letter (“”)

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

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

A

No, they make copies you can assign to new variables

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

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

A

There are lots!

Around 40 or so

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

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

A

Sometimes, but not always needed

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

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

A

There are lots!

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

Give 6 examples of comparison operators.

A
===
<
>
!==
<=
>=
52
Q

What data type do comparison expressions evaluate to?

A

Booleans, true or false

53
Q

What is the purpose of an if statement?

A

To make decisions

54
Q

Is else required in order to use an if statement?

A

No. The default would just be for the code to do nothing.

55
Q

Describe the syntax (structure) of an if statement.

A
if (operand comparison-operator operand) {
      Code to run if test === true
} else {
	Code to run if test != true
}
56
Q

What are the three logical operators?

A

&& and
|| or
! not

57
Q

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

A

Logical AND (&&) or logical OR (||)

58
Q

What is the purpose of a loop?

A

Loops allow us to repeat the behavior in a code with a stopping point.

59
Q

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

A

The condition needs to be true for the loop’s code to run again

60
Q

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

A

One passthrough of the code

61
Q

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

A

Before each iteration/passthrough

62
Q

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

A

Once, at the very beginning.

63
Q

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

A

Before each iteration

64
Q

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

A

After each iteration of the code block

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

66
Q

What does the ++ increment operator do?

A

i++ adds one additional amount to the value of i

67
Q

How do you iterate through the keys of an object?

A

For…in loops

var person = {name: "Dave", age: 26};
-----------------------------------------------
function keysInObject(person) {
    var placeHolder = [];
    for (var properties in person) {
	placeHolder.push(properties);
    }
    return placeHolder;
}
-----------------------------------------------
keysInObject(person);
["name", "age"]
68
Q

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

A

‘focus’

69
Q

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

A

‘blur’

70
Q

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

A

‘input’

71
Q

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

A

‘submit’

72
Q

What does the event.preventDefault() method do? What would it do to an unchecked checkbox?

A

even.preventDefault();

It stops an event from doing the actions of an object by default

When you click on the unchecked checkbox, a check would not appear

73
Q

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

A

It sends the values of the form up into the url of the page

74
Q

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

A

formVar.element

75
Q

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

A

formVar.element.name.value

76
Q

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

A

Without constant checks to your code, step by step, you won’t know where the issue arises from.

77
Q

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

A

You can constantly see the changes you are making to your code and test the functionality.

78
Q

What is the event.target?

A

The literal thing that was used by the user to trigger the event

79
Q

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

A

The element doesn’t not appear on the web page

80
Q

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

A

It takes a CSS selector, as a string

It returns a boolean for weather or not the element you are comparing contains the same item

event.target.matches(“.tab”)

81
Q

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

A

element.getAttribute(‘attributeName’);

82
Q

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

A

Any time the value of an item may be defined or changed

83
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

I’d need to an a separate event listener for the specific new tab, which would make me copy all the code over again.

84
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

I would have one IF condition for each tab, to make sure the value of the tab matched the event.target.

85
Q

What is JSON?

A

JavaScript Object Notation (JSON)

A standardized text-based format for representing structured data, but in a form that can be sent over the internet

It’s based on JavaScript object syntax

86
Q

What are serialization and deserialization?

A

Serialization: (JSON.stringify(object);) The process of converting your JavaScript object into a string of bites for transferring data to another place.

Deserialization: (JSON.pase(JSONstring);) The process of converting the string of bites back into an object for JavaScript.

87
Q

Why are serialization and deserialization useful?

A

They allow us to date data structures and convert them into something we can send into a place, like something in our network to into memory.

88
Q

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

A
var object = [ { name: 'Bob' , age: 35 }, { name: 'Phil' , age: 40 } ]
JSON.stringify(object);
89
Q

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

A
var JSONstring = '[{"name":"Bob","age":35},{"name": "Phil","age":40}]'
JSON.parce(JSONstring);
90
Q

How to you store data in localStorage?

A

localStorage.setItem(‘keyName’, objectJSON);

91
Q

How to you retrieve data from localStorage?

A

var objectJSON = localStorage.getItem(‘keyName’);

92
Q

What data type can localStorage save in the browser?

A

The key of the an object, and the value, as a JSON string

93
Q

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

A

It fires right before a web page closes.

94
Q

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

A

The return of a method definition will describe the content of said method.

The return of the method call will be the result produced by the method and its commands.

95
Q

Describe method definition syntax (structure).

A
var calculator = {
	sum: function (x, y) { return x + y; },
	multiply: function (x, y) { return x * y; }
}
96
Q

Describe method call syntax (structure).

A

calculator.sum(5, 9);

97
Q

How is a method different from any other function?

A

Methods are called as part of an object which contains them.

98
Q

What is the defining characteristic of Object-Oriented Programming?

A

We use objects which contain data and code, together.

99
Q

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

A

1) Inheritance
2) Abstraction
3) Encapsulation
4) Polymorphism

100
Q

What is “abstraction”?

A

The process of taking a complex task and simplifying it with the use of tools.

(Like turning on a light with a light switch)

101
Q

What does API stand for?

A

Application Programming Interface

102
Q

What is the purpose of an API?

A

It is the connection between two or more different “types” of things. Such as one software to another or software to hardware, etc

103
Q

What is THIS in JavaScript?

A

A parameter for a function which is implicit (not exactly defined, but implied, based on from where the function containing this would be called.)

104
Q

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

A

It is not specifically defined. It is implied from where it is called.

Explicit parameters are something we clearly define.

Implicit parameters are determined by the location of the call.

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

It is not being called yet, so this is not referring to anything yet.

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

this.firstName is referring to the firstName property value, in the “character” object

108
Q

var hello = character.greet;
hello();

A

The greet method of the character object becomes the function definition for “hello”

Running hello(); will return with “It’s-a-me, undefined!” because the hello object does not contain anything names firstName.

109
Q

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

A

You can’t. On its definition, it is still not being used by something, therefore this is referring to nothing

110
Q

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

A

You can look to the left of the dot ( object.method )

If there is nothing to the left of the dot, this is referring to the global window object

111
Q

What is a prototype in JavaScript?

A

A general version of an object that other objects can use

112
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

Methods live on a prototype the data in the object uses

113
Q

If an object does not have it’s own property or method by a given key, where does JavaScript look for it?

A

It’ll go thorough the object’s __proto__ object, to find the method

114
Q

What does the new operator do?

A

It creates a new object which will contain the result of your constructor function’s code.

1) Created an object
2) Adds prototype object from the construction function
3) Binds THIS to the current, blank object
4) Runs constructor function

115
Q

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

A

.prototype

construtorFunction.prototype.behaviorName

116
Q

What does the instanceOf operator do?

A

It checks to see if an object was created using a given constructor

object instanceof constructorFunction

117
Q

What is a “callback” function?

A

A function being passed into another function as an argument.

greetFullName(fullName()) {

118
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

We can use setTimeout(function, delayedMilliseconds)

119
Q

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

A

var intervalID = setInterval(function, intervalInMilliseconds);

120
Q

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

A

0 milliseconds

121
Q

What do setTimeout() and setInterval() return?

A

They timeoutID is returned that counts the amount of times the interval has occurred, for a unique ID for each.

122
Q

What is AJAX?

A

A technology which allows us to load data into part of our page, without having to refresh our page.

123
Q

What does the AJAX acronym stand for?

A

Asynchronous JavaScript And XML

124
Q

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

A

XMLHttpRequest

So when we want to make a new call, we use var xyz = new XMLHttpRequest()

125
Q

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

A

Load

We can add an event listener on our new XMLHttpRequest object for ‘load’.

126
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

They share the same EventTarget prototype