JavaScript Flashcards

1
Q

What is the purpose of variables?

A

hold on to data for the future

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 (var is the keyword)

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

=

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 (_). It must not start
with a number. You cannot use keywords or reserved words.

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

All variables are case sensitive,
so score and Score would be
different variable names, but
it is bad practice to create two
variables that have the same
name using different cases.

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

hold a sequence 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

store numbers for calculations

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 helpful when
determining which part of a
script should run.

“make a choice”

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

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

You just use the
variable name, the equals sign
(also known as the assignment
operator), and the new value for
that attribute.

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= can only be assigned (is/was actually assigned on purpose) 1. i’ll get data later 2. i’ll never get it but need to show you

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. (javascript says “i never received it”

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

if you do not include “labels”, it can be very confusing instead of helpful. A console log “label” is simply a short string that describes the variable or value being logged.

Since the console.log() method can write multiple values to the console at the same time, it’s easy to include a “label” when printing a value.

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

Give five examples of JavaScript primitives.

A

string.
number.
bigint.
boolean.
undefined.
symbol.
null.

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

console.log(‘value of fullName:’, fullName);

A

log method of the console object is being called with 2 arguments, the string ‘value of fullname’ and the variable fullName.

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

var fullName = ‘Cody Miller’

A

string cody miller is being assigned to the variable fullName

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

console.log(‘typeof fullName:’, typeof fullName);

A

log method of the console object is being called with 2 arguments, the string typeof full name and th result of typeof fullname

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

var never;

A

variable never is being declared but never assigned a value

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

What data type is returned by an arithmetic operation?

A

basic math

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

What is string concatenation?

A

two or more strings to create 1 string

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

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

A

adds one value to another and concatenate

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

What does the += “plus-equals” operator do?

A

adds

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

motto += ‘ is the GOAT”

A

the value of the variable motto is being concatenated with the string ‘ is the goat’ and the result is being assigned to the variable motto

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

What are objects used for?

A

Objects group together a set of variables and functions to create a model of a something

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

What are object properties?

A

IN AN OBJECT: VARIABLES BECOME KNOWN AS PROPERTIES

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

Describe object literal notation.

A

object stored in a variable (optional),
content separates each key from its value inside curly braces

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

How do you remove a property from an object?

A

delete

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

What are the two ways to get or update the value of a property?

A

dot notation hotel.name = ‘park’;
bracket notation hotel[‘name’] = ‘park’

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

var student = {
firstname: Luke
lastname: skywalker
age: 25
};

A

object literal assigned to variable student

string luke assigned to variable firstname

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

code reading

A

. === “of”
[] === at

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

right of .
left of .

A

property
object

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

What are arrays used for?

A

whenever you are working
with a list or a set of values that
are related to each other.

Arrays are especially helpful
when you do not know how
many items a list will contain

where order is either important or not important

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

Describe array literal notation.

A

The values are assigned to the
array inside a pair of square
brackets, and each value is
separated by a comma. The
values in the array do not need
to be the same data type, so you
can store a string, a number and
a Boolean all in the same array.

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

How are arrays different from “plain” objects?

A

object = order of properties not important
array= index numbers dictate order of properties, has length

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

What number represents the first index of an array?

A

0

36
Q

What is the length property of an array?

A

how many items in the array

37
Q

How do you calculate the last index of an array?

A

.length -1

38
Q

What is a function in JavaScript?

A

Functions let you group a series of statements together to perform a specific task.

If different parts of a script repeat the same task, you can reuse the function (rather than repeating the same set of statements).

39
Q

Describe the parts of a function definition.

A
  1. The function keyword to begin the creation of a new function.
  2. An optional name. (Our function’s name is sayHello.)
  3. A comma-separated list of zero or more parameters, surrounded by () parentheses. (Our sayHello function doesn’t have any parameters.)
  4. The start of the function’s code block, as indicated by an { opening curly brace.
  5. An optional return statement. (Our sayHello function doesn’t have a return statement.)
  6. The end of the function’s code block, as indicated by a } closing curly brace.
40
Q

Describe the parts of a function call.

A

once defined - can be called (code within its code block to run)

write name of function with (parameters) next to it - this causes code block within function’s definition to be executed

41
Q

When comparing them side-by-side, what are the differences between a function call and a function definition?

A

parameters for definition / arguments for calling;

function keyword

42
Q

What is the difference between a parameter and an argument?

A

You can think of a parameter as a placeholder. It is basically a variable whose value is not known until we call the function and pass an argument. When the function’s code block is run, the parameter is will be holding the value of the argument.

43
Q

Why are function parameters useful?

A

gives u a process to use for values many times

44
Q

What two effects does a return statement have on the behavior of a function?

A
  1. Causes the function to produce a value we can use in our program.
  2. Prevents any more code in the function’s code block from being run.
45
Q

function code reading

A

function definition named convert with one parameter minutes with opening curly brace for code block

value of variable minutes multiply by 60 and result is assigned to variable seconds

value of variable seconds is being returned from the function

convertmnitestoseconds is being called with one parameter and the result is being returned to the variable ______.

46
Q

Why do we log things to the console?

A

The JavaScript console is a debugging tool. It is where the browser prints errors and warnings as they occur in your JavaScript code.

47
Q

What is a method?

A

A JavaScript method is a property of an object that contains a function definition. Methods are functions stored as object properties

48
Q

How is a method different from any other function?

A

Methods are functions stored as object properties.

The difference is that a method is associated with an object, while a function is not.

49
Q

How do you remove the last element from an array?

A

pop() method

50
Q

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

A

Math.floor() function

51
Q

How do you generate a random number?

A

Math. random()

52
Q

How do you delete an element from an array?

A

splice()

53
Q

How do you append an element to an array?

A

push()

54
Q

How do you break a string up into an array?

A

split()

55
Q

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

A

no,

console.log

in javascript, strings cannot be changed ever

56
Q

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

A

no

57
Q

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

A

mdn

58
Q

Give 6 examples of comparison operators.

A
59
Q

What data type do comparison expressions evaluate to?

A
60
Q

What is the purpose of an if statement?

A
61
Q

Is else required in order to use an if statement?

A
62
Q

Describe the syntax (structure) of an if statement.

A
63
Q

What are the three logical operators?

A
64
Q

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

A
65
Q

What is the purpose of a loop?

A

repeat a code block until specified condition is no longer met

66
Q

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

A

run until the counter reaches a specified number (brakes)

67
Q

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

A

one time a full loop runs

68
Q

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

A

before each iteration

69
Q

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

A

first time the loop is run (initialization = “before anything)

70
Q

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

A

after initialization - after each iteration of code block
until counter reaches a specified number

71
Q

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

A

after each iteration (the is the gas/accelerator)

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

73
Q

What does the ++ increment operator do?

A

increment by 1

74
Q

How do you iterate through the keys of an object?

A

arr[i].keyname

75
Q

What is JSON?

A

JSON is an extremely common data interchange format used to send and store information in computer systems. Before JSON, XML was a popular data interchange format, but JSON’s simplicity and readability has made it the preferred format.

76
Q

What are serialization and deserialization?

A

Serialization is a process of converting an Object into stream of bytes so that it can be transferred over a network or stored in a persistent storage.

Deserialization is the exact opposite - Fetch a stream of bytes from network or persistence storage and convert it back to the Object with the same state.

77
Q

Why are serialization and deserialization useful?

A

The purpose of serializing it into JSON is so that the message will be a format that can be understood and from there, deserialize it into an object type that makes sense for the consumer.

78
Q

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

A

JSON.stringify()

79
Q

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

A

JSON.parse()

80
Q

How do you store data in localStorage?

A

The setItem() method of the Storage interface, when passed a key name and value, will add that key to the given Storage object, or update that key’s value if it already exists.

81
Q

How do you retrieve data from localStorage?

A

The getItem() method of the Storage interface, when passed a key name, will return that key’s value, or null if the key does not exist, in the given Storage object.

82
Q

What data type can localStorage save in the browser?

A

strings

83
Q

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

A

The beforeunload event is fired when the window, the document and its resources are about to be unloaded. The document is still visible and the event is still cancelable at this point.

84
Q

if local storage acts wonky

A

shift / out the before unload event, delete local storage, refresh, and unshift out the before unload event

85
Q

Closures

A

Think of a closure as a container or a box. The outer function is like the container or the box, and the inner function is like an object that’s stored inside the container or the box. When the outer function is executed, it creates the container or the box, and the inner function is stored inside it.

Now, imagine that the inner function needs to access some variables that are outside of the container or the box. Normally, when the outer function is finished executing and the container or the box is closed, those variables would no longer be accessible. However, because the inner function is still inside the container or the box, it has access to those variables, even though they’re outside of the container or the box.

So, just like how an object stored inside a container or a box has access to other objects stored inside the same container or box, an inner function stored inside an outer function has access to variables defined in the outer function.

I hope this analogy helps make closures in JavaScript a bit easier to understand!