JavaScript Flashcards

1
Q

What is the purpose of variables?

A

To store data or information or values.

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

How do you declare a variable?

A

Using the variable keywords such as: ‘var’, ‘let’, or ‘const’

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

by using the assignment operator =

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

letters, numbers, $, _, capitol letters. Do not start with a number.

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

upper case and lower case matters.

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 values

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

to store numbers

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

there are only 2 values, true or false

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

assigns a value to a variable

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

assign the variable a new value

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

a null value represents a reference that points, generally intentionally, to a nonexistent or invalid object or address.
undefined is a primitive value automatically assigned to variables that have just been declared, or to formal arguments for which there are no actual arguments.

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

it is much clearer which variables are being logged and in what order.

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

Give five examples of JavaScript primitives.

A

strings, numbers, booleans, undefined, null

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

a number

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

What is string concatenation?

A

combining multiple strings into 1 string

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

to add numbers, or to concatenate 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

boolean

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

adds more to a variable then reassigns it. called addition assignment

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

What are objects used for?

A

Object group together variables and functions to create a model of something from the real world.

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

What are object properties?

A

variables inside an object

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

Describe object literal notation.

A

Name/key/property is separated from value by a colon.
Name/key/property-value pairs are separated by commas.
No comma follows the last name/key/property-value pair.

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

by using the delete keyword.
delete objectName.propertyName

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 and bracket notation

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

What are arrays used for?

A

storing a list of values in a variable

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

Describe array literal notation.

A

var array = [item1, item2, item3, etc ]

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 have index numbers(starting at 0) instead of keys/property names.

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

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 returns how many things are in 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

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

What is a function in JavaScript?

A

Functions are a special kind of object that is “callable”. The code inside the function runs when the function is “called”.

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

the function keyword,
an optional name,
zero or more parameters,
a code block,
an optional return statement

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

The function’s name. A comma-separated list of zero or more arguments surrounded by () parentheses.

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

The function definition contains the code.
Calling a function does NOT use the “function” keyword.

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

A parameter is like a placeholder. When we define a function, we declare parameters and that when we call a function, we pass it arguments.

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

Why are function parameters useful?

A

Parameters allow a function to perform tasks without knowing the specific input values ahead of time.

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

A return statement causes the function to produce a value.
A return statement also exits the function; no code after the return statement is executed.

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

To check if our code is working 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 which is a property 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

methods are associated with objects.

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

calling the pop() method.
array.pop()

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

calling the floor() method of the Math object.
Math.floor()

42
Q

How do you generate a random number?

A

calling the random() method of the Math object
Math.random()

43
Q

How do you delete an element from an array?

A

calling the splice() method with 2 arguments, the first argument is the index at which to start removing elements, and the second argument is how many elements to remove. Adding a third argument will place the third argument at first argument index.

44
Q

How do you append an element to an array?

A

calling the push() method will append one or more elements to the end of an array.
using the unshift() method will append one or more elements to the beginning of an array.

45
Q

How do you break a string up into an array?

A

calling the split() method

46
Q

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

A

No,

47
Q

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

A

50

48
Q

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

A

No.

49
Q

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

A

40

50
Q

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

A

MDN

51
Q

Give 6 examples of comparison operators.

A

<, >, <=, >=, ==, ===, !=. !==

52
Q

What data type do comparison expressions evaluate to?

A

Boolean.

53
Q

What is the purpose of an if statement?

A

To run code if a condition is met.

54
Q

Is else required in order to use an if statement?

A

No.

55
Q

Describe the syntax (structure) of an if statement.

A

keyword ‘if’ (CONDITION HERE) {
CODE BLOCK
}

56
Q

What are the three logical operators?

A

&&, ||, !

57
Q

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

A

&& or ||

58
Q

What is the purpose of a loop?

A

To run the same code more than once without having to write it more than once.

59
Q

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

A

To tell the loop when to stop.

60
Q

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

A

everytime a loop completes a cycle

61
Q

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

A

at the start of each iteration.

62
Q

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

A

At the start of the loop.

63
Q

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

A

at the beginning of each iteration

64
Q

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

A

at the end of each iteration

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

increments value by 1

67
Q

How do you iterate through the keys of an object?

A

for in loop

68
Q

What is a method?

A

A method is a function which is a property of an object.

69
Q

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

A

the function keyword

70
Q

Describe method definition syntax (structure).

A

{property: function (param) {
code block
}}.

71
Q

Describe method call syntax (structure).

A

object.property(arguments)

72
Q

How is a method different from any other function?

A

Function — a set of instructions that perform a task. Method — a set of instructions that are associated with an object.

73
Q

What is the defining characteristic of Object-Oriented Programming?

A

objects can contain both data (as properties) and behavior (as methods).

74
Q

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

A

Abstraction, Encapsulation, Inheritance, Polymorphism

75
Q

What is “abstraction”?

A

boils down to being able to work with (possibly) complex things in simple ways.

76
Q

What does API stand for?

A

Application Programming Interface.

77
Q

What is the purpose of an API?

A

to give programmers a way to interact with a system in a simplified, consistent fashion: aka, an abstraction.

78
Q

What is this in JavaScript?

A

this is an implicit parameter of all JavaScript functions. In the most basic sense, this refers to the object its in, or the window object.

79
Q

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

A

capable of being understood from something else though unexpressed

80
Q

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

A

call time

81
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

The window object, because its not being called.

82
Q

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

A

“It’s-a-me, Mario!” because its being called by the character object.

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

84
Q

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

A

You can’t because the this value is determined when it is being called.

85
Q

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

A

based on which object is calling it. left of the dot(.).

86
Q

What kind of inheritance does the JavaScript programming language use?

A

JavaScript includes a specific kind of inheritance known as prototype-based (or prototypal) inheritance. JavaScript objects give certain behaviors (methods) or data (properties) to other objects.

87
Q

What is a prototype in JavaScript?

A

a JavaScript prototype is simply an object that contains properties and (predominantly) methods that can be used by other objects.

88
Q

How is it possible to call methods on strings, arrays, and numbers even though those methods don’t actually exist on strings, arrays, and numbers?

A

Those methods are defined on a “prototype” object and arrays, strings, and numbers simply borrow those methods when they’re needed.
Inherited from the prototype chain.

89
Q

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

A

the object’s prototype.
Prototype chain.

90
Q

What does the new operator do?

A
  1. Creates a blank, plain JavaScript object. For convenience, let’s call it newInstance.
  2. Points newInstance’s [[Prototype]] to the constructor function’s prototype property, if the prototype is an Object. Otherwise, newInstance stays as a plain object with Object.prototype as its [[Prototype]].
    Note: Properties/objects added to the constructor function’s prototype property are therefore accessible to all instances created from the constructor function.
  3. Executes the constructor function with the given arguments, binding newInstance as the ‘this’ context (i.e. all references to this in the constructor function now refer to newInstance).
  4. If the constructor function returns a non-primitive, this return value becomes the result of the whole new expression. Otherwise, if the constructor function doesn’t return anything or returns a primitive, newInstance is returned instead. (Normally constructors don’t return a value, but they can choose to do so to override the normal object creation process.)
91
Q

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

A

prototype

92
Q

What does the instanceof operator do?

A

The instanceof operator tests to see if the prototype property of a constructor appears anywhere in the prototype chain of an object. The return value is a boolean value.

93
Q

What is a “callback” function?

A

A callback function is a function passed into another function as an argument, which is then invoked inside the outer function to complete some kind of routine or action.

94
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, delay, param1, param2, etc)

95
Q

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

A

setInterval(function, delay, param1, param2, etc)

96
Q

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

A

If this parameter is omitted, a value of 0 is used, meaning execute “immediately”, or more accurately, the next event cycle.

97
Q

What do setTimeout() and setInterval() return?

A

ID’s used to cancel the respective timers using clearTimeout(“respectiveID”) and clearInterval(“respectiveID”)

98
Q

What is AJAX?

A

AJAX is a technique for loading data into part of a page without having to refresh the entire page (Asynchronous). The data is often sent in JSON format (JavaScript Object Notation)

99
Q

What does the AJAX acronym stand for?

A

Asynchronous JavaScript And XML

100
Q

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

A

XMLHttpRequest

101
Q

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

A

The load event is fired when an XMLHttpRequest transaction completes successfully.

102
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

prototypal inheritance. EventTarget prototype.