JavaScript Flashcards

1
Q

What is the purpose of variables?

A

to store data (temporarily) that can be accessed later

have a value that persists

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;

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

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

letters, numbers, dollar signs, underscore
no dash or period
cannot 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

score != Score

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

store data with text content

save 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

count, math, measurement

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

to make decisions

determine which part of a 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
assigns a value to a variable
updates value given to a variable

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

How do you update (reassign) the value of a variable?

A

using equal sign (assignment operator) to assign it a new value
var is only necessary the first time

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 no value
undefined means variable has not been declared/given a value
null MUST be assigned

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

to know which value or variable is being logged

helpful for catching bugs

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

Give 5 examples of JavaScript primitives

A

string, number, boolean, 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 (always)

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

What is string concatenation?

A

combination of two or more string values (or number)

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

What purpose(s) does the + operator serve in JavaScript?

A

adds on value to another 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?

A

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 += operator do?

A

adds the value of the value on right to the current variable and assigns the result to that variable

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

What are objects used for?

A

storing a set of variables and functions
bringing data together
create a collection of stuff that make sense

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

What are object properties?

A

individual pieces of data stored in an object

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

Describe object literal notation

A

The object which contains a set of properties and values inside curly braces being assigned to a variable

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 the delete operator followed by the object and property name

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

storing a list or set 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

var nameOfArray = straight brackets filled with items

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 for the values of the array are indexed

to add something to array: push method

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

holds the number of items 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

nameOfArray.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 special kind of object that can be called and reused to perform a task

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

Describe the parts of a function defintion

A

function keyword and name of the function, called with 0 or more parameters, with the code block enclosed in curly braces

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 with parentheses called with 0 or more params

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 diffs between a function call and a function definition?

A
calling a function requires the name of the function with arguments
function definition is the function itself that takes parameters
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
34
Q

What is the diff between a parameter and an argument?

A

argument: passed into function when it’s called
parameter: tied to the function definition

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

Why are function parameters useful?

A

to pass additional info into the function and have dynamic data
gives us a tool that can be more generalized

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

prevents any more code in function’s code block to be run

produces values / exits the function

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 test your functions and methods to see if you see what you expect

38
Q

What is a method?

A

a function which is a property of an object

39
Q

How is a method different from any other function?

A

a method is associated with an object

while a function is not

40
Q

How do you remove the last element from an array?

A

pop method

pop method of the array object

41
Q

How do you round a number down to the nearest integer?

A

floor method of the Math object

42
Q

How do you generate a random number?

A

Math.random, then multiply that by the range, and call it with the floor method

43
Q

How do you delete an element from an array?

A

splice method of the array object

44
Q

How do you append an item to an array?

A

push method of the array object

45
Q

How do you break a string up into an array?

A

split method of the array object

‘ ‘ gets replaced with individual items in an array

46
Q

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

A

no.

you can log values of the strings to the console to check

47
Q

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

A

a lot because strings are an important date type

48
Q

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

A

no

for ex, pop method removes an item, and sometimes we don’t need it anymore

49
Q

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

A

a lot because arrays are important. They change values and stuff

50
Q

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

A

MDN

include the language as well

51
Q

Give 6 examples of comparison operators

A

greater than, less than, greater than or equal to, less than or equal to, strictly equal to, strictly not equal to

52
Q

What data type do comparison expressions evaluate to?

A

boolean

53
Q

What is the purpose of an if statement?

A

to execute something if the condition is a truthy

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

if keyword followed by condition inside parentheses, followed by curly braces for the if statement

56
Q

What are the three logical operators?

A

&&, double pipes ||, and !

57
Q

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

A

by using logical operators between the expressions inside parentheses

58
Q

What is the purpose of a loop?

A

to do something repeatedly

59
Q

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

A

If the value of the condition expression is true, the loop statements execute
If the condition is false, the for loop stops
*tells the loop when to run

60
Q

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

A

The process of executing a loop

*each instance of running the for loop code block

61
Q

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

A

it runs as long as it’s true

62
Q

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

A

It’s the first thing that’s run

*before anything, allows us to set a start

63
Q

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

A

it runs after the initialization and for each iteration as long as the condition is true

64
Q

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

A

It runs last

After 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’ keyword

66
Q

What does the ++ increment operator do?

A

Increments the variable by 1.

67
Q

How do you iterate through the keys of an object?

A
  • for (var key in object)
68
Q

What is the difference between these two snippets of code?

element. addEventListener(‘click’, handleClick)
element. addEventListener(‘click’, handleClick())

A

First one is a variable, second is a function being called (which isn’t good)

69
Q

What is JSON?

A
  • Text-based data format following JavaScript object syntax
  • Can be used independently from JavaScript
  • Exists as a string, useful for transmitting data across a network
  • Needs to be converted to a native JavaScript object when you want to access the data
  • string format meant to replicate the format of JavaScript objects
70
Q

What are serialization and deserialization?

A
  • Serialization: process of turning an object in memory into a stream of bytes so you can store it on disk or send it over the network
  • Deserialization: reverse process, turning a stream of bytes into an object in memory
71
Q

Why are serialization and deserialization useful?

A

You can transfer objects across networks and deserialize them again later
* efficiently

72
Q

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

A

JSON.stringify()

73
Q

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

A

JSON.parse()

74
Q

How do you store data in localStorage?

A

localStorage.setItem(‘key’, ‘value’)

75
Q

How do you retrieve data from localStorage?

A

localStorage.getItem(‘keyName’)

76
Q

What data type can localStorage save in the browser?

A

strings

77
Q

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

A

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

78
Q

What is a method?

A

A function which is a property of an object
Two kinds: instance, built-in tasks performed by an object instance / static, tasks that are called directly on an object constructor

79
Q

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

A

A method definition has a function defined with the code block, while a method call is just the property of an object

80
Q

Describe method definition syntax (structure).

A

Assign an object literal to a variable, and each property key is a method with a value of a function definition

81
Q

Describe method call syntax (structure).

A

The name of the variable for the object, dot notation, method name, and finally the arguments

82
Q

How is a method different from any other function?

A

Methods are properties of objects, while functions are not

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 complex things in simple ways / “generalization”

86
Q

What does API stand for?

A

Application programming interface

87
Q

What is the purpose of an API?

A

Allows apps to send information between each other / let one piece of software communicate with another
The purpose of every software API is to give programmers a way to interact with a system in a simplified, consistent fashion: aka, an abstraction.
* allows users to use a piece of code without having to understand everything about it

88
Q

What is a “callback” function?

A

A function passed into another function as an argument

89
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

Calling a number value in milliseconds as an argument in the methods
* setTimeout function

90
Q

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

A

setInterval() method

91
Q

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

A

0

92
Q

What do setTimeout() and setInterval() return?

A

A numeric, non-zero value that identifies the timer created by the call to the method (setInterval())
* a number, as a unique ID, to identify the timers created from the functions, not mathematical value