Javascript Flashcards

1
Q

What is the purpose of variables?

A

variables are used to store data that can be accessed 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

a keyword (like var) then the name of the variable

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

variable name = (assignment operator) 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

letters, numbers, $, _

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

variables spelled with the same characters but have differences in capitalization are considered different

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

stores data containing a series of characters

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

used for calculations, determining the size, movement, or time associated with something

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

defines whether something is true or false, for decisions

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

variableName = 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

null is the intentional absence of value

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

labels provide context to whatever you log in the console

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

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

What is string concatenation?

A

combining a string with another value to create a new 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

adds numbers and concatenates 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 the value to the right of the operator to the value on the left and assigns the result to the variable on the left

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

What are objects used for?

A

group together a set of related variables and functions

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

What are object properties?

A

variables that are part of an object

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

Describe object literal notation.

A

objectName = { 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 keyword followed by the 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 or 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

arrays store a list of values that are related to each other

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

Describe array literal notation.

A

arrayName = [ value, … } ;

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 keys are index numbers, so there’s a specific order to the values

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

the number of entries in 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

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

section of code that performs a task and can be reused

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 keyword, function name, ( ) with 0 or more parameters, { } code block, and and 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

function name, ( ) containing the argument(s)

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

function definition has a function keyword, parameters, { } containing a code block. function call only has the function name and arguments in ( )

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 are for definition, arguments are for calls

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

Why are function parameters useful?

A

they allow different arguments to be passed through a function

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

return statements stop the code block from running and make the function produce a value

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 make sure our code is working properly and producing the expected results

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

What is a method?

A

a function that 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 a part of an object, they must be called with the object

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

.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

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( ) * (range + 1) + start

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

.splice(index, # of items deleted)

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( )

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

.split( ‘ ‘ )

46
Q

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

A

no, console.log(string) to check

47
Q

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

A

about 36

48
Q

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

A

no, sometimes you don’t need a return value because it won’t be useful

49
Q

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

A

about 36

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

booleans

53
Q

What is the purpose of an if statement?

A

to run a code block if a condition is met

54
Q

Is else required in order to use an if statement?

A

no, sometimes nothing needs to happen if a condition isn’t met

55
Q

Describe the syntax (structure) of an if statement.

A

if (condition) { 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

put a pair of ( ) around each individual expression and use a logical operator in between

58
Q

What is the purpose of a loop?

A

loops run a block of code a certain amount of times while a condition is met

59
Q

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

A

conditions determine whether a code loop is run, otherwise infinite loops occur

60
Q

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

A

each time a loops runs through a code block

61
Q

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

A

before each iteration of a loop

62
Q

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

A

once before the loop starts

63
Q

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

A

before each iteration of a loop

64
Q

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

A

after each iteration of a loop

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

adds one to a value and either returns the updated value or returns the value then increments it

67
Q

How do you iterate through the keys of an object?

A

for … in loops

68
Q

What is JSON?

A

a text-based format following JS object syntax, used for transmitting data across a network

69
Q

What are serialization and deserialization?

A

serialization turns an object to be turned into a format that can be sent across a network. deserialization converts it back into an object

70
Q

Why are serialization and deserialization useful?

A

they allow objects to be turned into transferrable data and converted back

71
Q

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

A

JSON.stringify( )

72
Q

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

A

JSON.parse( )

73
Q

How do you store data in localStorage?

A

.setItem(keyName, keyValue)

74
Q

How to you retrieve data from localStorage?

A

.getItem(keyName)

75
Q

What data type can localStorage save in the browser?

A

objects

76
Q

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

A

fired when the window, document, and its resources are about to be unloaded

77
Q

What is a method?

A

a function which is a property of an object

78
Q

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

A

a method definition has the function keyword, and a code block

79
Q

Describe method definition syntax (structure).

A

function functionName(params) { code block};

80
Q

Describe method call syntax (structure).

A

objectName.methodName(argument)

81
Q

How is a method different from any other function?

A

methods are part of objects

82
Q

What is the defining characteristic of Object-Oriented Programming?

A

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

83
Q

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

A

abstraction, encapsulation, inheritance, polymorphism

84
Q

What is “abstraction”?

A

being able to work with complex things in simple ways

85
Q

What does API stand for?

A

application programming interface

86
Q

What is the purpose of an API?

A

it is an abstraction, a set of tools that people can use

87
Q

What is this in JavaScript?

A

this is an implicit parameter in all JS functions

88
Q

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

A

it’s available in a function’s code block even though it was never included in the function’s parameter list or declared with var

89
Q

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

A

call time

90
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

nothing, the function hasn’t been called yet

91
Q
Given the above character object, what is the result of the following code snippet? Why?
var character = {
  firstName: 'Mario',
  greet: function () {
    var message = 'It\'s-a-me, ' + this.firstName + '!';
    console.log(message);
  }
};
character.greet( );
A

it’s-a-me, Mario! this.firstName is Mario in this context

92
Q
Given the above character object, what is the result of the following code snippet? Why?
var character = {
  firstName: 'Mario',
  greet: function () {
    var message = 'It\'s-a-me, ' + this.firstName + '!';
    console.log(message);
  }
};
var hello = character.greet;
hello( );
A

it’s-a-me, undefined! this is the window object, which doesn’t have a firstName property

93
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 call time

94
Q

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

A

It’s the object that the method is being called on

95
Q

What kind of inheritance does the JavaScript programming language use?

A

prototype-based inheritance

96
Q

What is a prototype in JavaScript?

A

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

97
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

they get the method from its prototype object

98
Q

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

A

the prototype object

99
Q

What does the new operator do?

A

creates a new empty JS object, sets the __proto__ property of the object as the constructor function’s prototype, makes new object this, new returns object

100
Q

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

A

.prototype

101
Q

What does the instanceof operator do?

A

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

102
Q

What is a “callback” function?

A

a function passed into another function as an argument

103
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( )

104
Q

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

A

setInterval( )

105
Q

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

A

0, which executes immediately, or the next event cycle

106
Q

What do setTimeout( ) and setInterval( ) return?

A

an id number that can be passed to clearInterval( ) or clearTimeout( ) to cancel the timer

107
Q

What must the return value of myFunction be if the following expression is possible?
myFunction( )( );

A

another function

108
Q
What does this code do?
const wrap = value => ( ) => value;
A

returns a function that returns the value variable

109
Q

In JavaScript, when is a function’s scope determined; when it is called or when it is defined?

A

when it is defined

110
Q

What allows JavaScript functions to “remember” values from their surroundings?

A

closures