JavaScript Flashcards

1
Q

What is the purpose of variables?

A

It is to store bits of information to use 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

You have to use a variable keyword like ‘var’ and then follow it with a variable name. From there, you use the assignment operator ‘=’ and assign a value to it

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] = [variable 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

The name must begin with a letter, dollar sign, or an underscore
Can contain letters, numbers, dollar signs, or underscores

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

Score and Score would be different values

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

Used for working with any kind of text

Frequently used for adding new content to a page

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

Counting and calculating

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

Serves as an on/off switch

Helpful in determining which part of the script should run

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

Gives value to something

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

To update you would re-assign a value to the variable

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.

Every Object is derived from null value, and therefore typeof operator returns object for it:

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

So that there is context to the output

Helps with debugging

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

Give five examples of JavaScript primitives.

A

data that is not an object and has no methods. There are 7 primitive data types: string, number,, boolean, undefined,, and null.

We can disregard bigInt and symbol for the time being

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

The process of joining together two or more strings to create one 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

To add or concatenate

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

It adds then re-assigns the variable

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

What are objects used for?

A

They are used for storing different variables and values of different data types

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

What are object properties?

A

A variable that is in an object.

Individual key in an object that correlates with a value

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

Describe object literal notation.

A

Create a variable and assign it to a value with curly braces. In the curly braces have names that indicate properties or methods and assign them value by using a colon

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

To delete a property, you use the keyword delete and then use the dot notation to identify the property or method you want to remove from the object

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

Using dot notation or using square brackets

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

What are arrays used for?

A

When working with a list or a set of values related to each other
Especially helpful when you do not know how many items a list will contain
Representing lists 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 array = []

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

Each value in the array doesnt need a variable name/key

All arrays come with length property

Other ways to add to an array compared to plain objects

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

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

Array.length gives you the length 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

Take the length and subtract by 1, then use that as an index

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

A function is a code that you can re-use

packing up code for reuse throughout a program

giving a name to a handful of code statements to

make it code easier to read
making code “dynamic”, meaning that it can be written once to handle many (or even infinite!) situations

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 to begin the creation of a new function.

An optional name. (Our function’s name is sayHello.)

A comma-separated list of zero or more parameters, surrounded by () parentheses. (Our sayHello function doesn’t have any parameters.)

The start of the function’s code block, as indicated by an { opening curly brace.

An optional return statement. (Our sayHello function doesn’t have a return statement.)

The end of the function’s code block, as indicated by a } closing curly brace.

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

simply writing the name of the function and placing () parentheses next to it.

The function’s name. Again, our function’s name is sayHello.

A comma-separated list of zero or more arguments surrounded by () parentheses.

Our sayHello function does not take any arguments.

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 call uses the already defined function by including the function name and parentheses. In parentheses there might be arguments.

Function definition first states that it is a function by using function keyword, then defines the function name, sets up parameters (optional), followed by curly braces, inside the curly braces is code block.

Calling function gives an output while defining a function does not

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

Parameter

as a placeholder.

A variable whose value is not known until we call the function and pass in an argument

Parameters are declared when we define the function

Arguments

When we call a function, we pass in the arguments

Is the actual name of what the placeholder was

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

Why are function parameters useful?

A

They serve as a placeholder, they tell you what kind of value will be need to be placed there when calling the function

Give us the ability to have varying results based on the arguments we give

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

Causes the function to produce a value we can use in our program.

Prevents any more code in the function’s code block from being run.

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

What is an expression?

A

A chunk of code that needs to be evaluated to lead to a value

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

Why do we log things to the console?

A

console.log()

Debugging mechanism to understand what output we are getting

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

How is a method different from any other function?

A

It is a function that is stored in a property of an object

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

How do you remove the last element from an array?

A

array.pop()

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

How do you generate a random number?

A

Math.random()

44
Q

How do you delete an element from an array?

A

array.splice(index,0)

45
Q

How do you append an element to an array?

A

array.push()

46
Q

How do you break a string up into an array?

A

array.split()

47
Q

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

A

No, you would check by console logging the original string variable

48
Q

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

A

50 - ish

49
Q

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

A

no

50
Q

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

A

40

51
Q

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

A

MDN

52
Q

Give 6 examples of comparison operators.

A
===
!==
><
<=
>=
53
Q

What data type do comparison expressions evaluate to?

A

boolean

54
Q

What is the purpose of an if statement?

A

The if statement evaluates a condition. If the condition evaluates true, and statements in the subsequent code block are executed.
Lets you run different behavior based on data
Allow us to have different pathways in our code

55
Q

Is else required in order to use an if statement?

A

no

56
Q

Describe the syntax (structure) of an if statement.\

A

If (score >= 50) {
congratulate();
}

If keyword, condition, opening curly brace, code block, closing curly brace

57
Q

What are the three logical operators?

A

&& - and
|| - or
! - not

58
Q

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

A

You would use logical operators.

So two expressions and logical operators

59
Q

What is the purpose of a loop?

A

It allows us to run a code for a specified number of times

60
Q

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

A

It checks a condition and stops the code once the condition was met

61
Q

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

A

An iteration is when the code is looped over

62
Q

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

A

The beginning / before each iteration of the loop

63
Q

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

A

At the very beginning and doesnt happen again

64
Q

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

A

After the initialization. Before each iteration of the loop

65
Q

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

A

After the code block runs. At the end of the iteration. Right before the condition runs.

66
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

67
Q

What does the ++ increment operator do?

A

It adds one to the variable and re-assigns

68
Q

How do you iterate through the keys of an object?

A
For in loops
const object = { a: 1, b: 2, c: 3 };

for (const property in object) {
console.log(${property}: ${object[property]});
}

69
Q

What is JSON?

A

JavaScript Object Notation

as standard text-based format for representing structured data based on JavaScript object syntax. It is commonly used for transmitting data in web applications

JSON is a text-based data format following JavaScript object syntax

JSON exists as a string — useful when you want to transmit data across a network. It needs to be converted to a native JavaScript object when you want to access the data.

70
Q

What are serialization and deserialization?

A

Serialization is the process of turning an object in memory into a stream of bytes so you can do stuff like store it on disk or send it over the network.

Deserialization is the reverse process: turning a stream of bytes into an object in memory.

71
Q

Why are serialization and deserialization useful?

A

Useful for sending over data and using the data and save to hard drive

72
Q

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

A

JSON.stringify(array object)

73
Q

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

A

JSON.parse(string)

74
Q

How to you store data in localStorage?

A

localStorage.setItem(key, value)

75
Q

How to you retrieve data from localStorage?

A

localStorage.getItem(‘key’)

76
Q

What data type can localStorage save in the browser?

A

JSON string

77
Q

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

A

Before the page reloads

78
Q

What is a method?

A

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

79
Q

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

A

Method definition has a code block with opening and closing brackets and says the word ‘function’

A method call does not have neither of the two

80
Q

Describe method definition syntax (structure).

A

Value (method name) first then a colon ‘:’ after the colon is the function definition, possible parameters and the function code block

81
Q

Describe method call syntax (structure).

A

The Name of the object dot the name of the method and parentheses with arguments inside if needed

82
Q

How is a method different from any other function?

A

A method is within the object. Any other function is not in an object

83
Q

What is the defining characteristic of Object-Oriented Programming?

A

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

84
Q

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

A

Abstraction
Encapsulation
Inheritance
Polymorphism

85
Q

What is “abstraction”?

A

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

Abstraction in computer programming is a way to reduce complexity and allow efficient design and implementation in complex software systems. It hides the technical complexity of systems behind simpler APIs.

What something does without needing to know how it does it

86
Q

What does API stand for?

A

An application programming interface (API) is a connection between computers or between computer programs. It is a type of software interface, offering a service to other pieces of software

87
Q

What is the purpose of an API?

A

One purpose of APIs is to hide the internal details of how a system works, exposing only those parts a programmer will find useful and keeping them consistent even if the internal details later change

Software abstraction

88
Q

What is this in JavaScript?

A

it’s an implicit parameter, of all JS functions

This is the variable that holds the object that the function is working with

89
Q

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

A

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

Explicit means you have to name it to use use it.

This doesn’t need to be defined to use

90
Q

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

A

Call time

91
Q

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

A

You can’t tell because it isn’t being used yet

92
Q

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

A

Whenever you are dealing with this it needs to be being called and the value of this is whatever is being called. Essentially to the left of the dot

93
Q

What kind of inheritance does the JavaScript programming language use?

A

Prototype based inheritance or prototypal

94
Q

What is a prototype in JavaScript?

A

An object that other objects are able to build upon

95
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

It uses a prototype

96
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 checks to see if it has a prototype

97
Q

What does the new operator do?

A

The new operator lets developers create an instance of a user-defined object type or of one of the built-in object types that has a constructor function.

Creates a blank, plain JavaScript object.

Adds a property to the new object (__proto__) that links to the constructor function’s prototype object

Binds the newly created object instance as the this context (i.e. all references to this in the constructor function now refer to the object created in the first step).

Returns this if the function doesn’t return an object.

98
Q

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

A

.prototype

99
Q

What does the instanceof operator do?

A

Returns a boolean if an object is an instance of another object

100
Q

.prototype is in constructor function and __proto__ is in the new instance

A

.prototype is in constructor function and __proto__ is in the new instance

101
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.

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

103
Q

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

A

setInterval()

104
Q

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

A

No delay

105
Q

What do setTimeout() and setInterval() return?

A

The returned timeoutID is a positive integer value which identifies the timer created by the call to setTimeout(). This value can be passed to clearTimeout() to cancel the timeout.