Javascript Flashcards

1
Q

What is the purpose of variables?

A

To remember data

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

How do you declare a variable?

A

Use a variable keyword and write the variable name after it separated by a space
Ex. var isVariable;

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

You use an assignment operation like (=, <, >) to assign a value.
Ex. var isVariable = 10;

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

Must begin with a letter. Dollar sign, or an underscore, numbers
CANNOT start with 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

Variable names are remembered differently when the same letter is capitalized versus not.
Ex. isVariable /= isvariable

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 hold text within the quotation marks, which can include numbers, letters, symbols that the computer does not read as code, only 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

Remembers that it is a mathematical number, not text, used for math or incrementation

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 define whether it is true or false, basically made for decision making

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 to set a 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

Add another value to the same variable on a different line
The key word var is not needed for the second time since the variable was already assigned.
Ex. isCool = 7 instead of var isCool = 7

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 an empty value but this has to be assigned and is not defined by the computer. This means the developer said this is empty on purpose. Considered typeof object because of a bug.
Undefined also is an empty value no value is assigned, but javascript is the one telling you there is no 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

So that you know which value belongs to which variable.

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

Give five examples of JavaScript primitives.

A

Boolean, number, string, 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
(if you do something wrong with the arithmetic operation, javascript will output NaN aka not a number)

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

What is string concatenation?

A

When you use the + sign between strings, the result is the 2 strings put together.

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

Can add like in math
Can also combine 2 strings together

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 the value of the right and then adds it to the variable on the left and outputs the sum of the total value

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

What are objects used for?

A

They’re used to store a lot more than just 1 value. They can store as many properties for 1 variable.

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

What are object properties?

A

They are basically variables inside objects. Variables can only hold 1 value, but objects can hold multiple properties of the variable to keep all the data organized without creating a gazillion variables.

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

Describe object literal notation.

A

They are the easiest and most popular way to create objects. You define the variable and then type the different properties and their values in curly braces.
Ex. var hotel {
name: ‘Quay’,
rooms: 40,
booked: 25
};

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

Write delete before the property
Ex. delete student.lastName

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
var.newProperty
Bracket notation
var[‘newProperty’]

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

What are arrays used for?

A

Also used to store information, but in an orderly fashion

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

Describe array literal notation.

A

[ ]; Square brackets with values separated by commas

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

They are numbered, so easier to organize and recall via their number
Adding items to arrays uses push operator whereas objects use the assignment operator
Arrays have length properties whereas objects do not.

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
This is why it’s how many spaces to move to get to the value. To get to the first value, you don’t have to move any space. To get to the second value, you move 1 space.

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

Calculates the number of properties in the array
Do not have to calculate on our own

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

The length of the array - 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

They allow you to package up code for use later in your program
It’s a list of steps of a process that can be repeated.
They are a special kind of object that is “callable” and reusable

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
An optional name
Zero or more parameters
A code block
An optional return statement

32
Q

Describe the parts of a function call.

A

Function name
() with 0 or more arguments

33
Q

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

A

Function definition sets up the code for what should be passed and how it should be passed,
Keyword and code block in curly braces
Function call is when an argument is passed through the function definition set up prior
Keyword and argument in parenthesis

34
Q

What is the difference between a parameter and an argument?

A

Parameter is the definition of what the function will use to pass
Parameter is a placeholder for future values
Arguments are the value (which take the place of parameters) of what the function will use to pass
Parameter and arguments come together to make a variable to make the function happen

35
Q

Why are function parameters useful?

A

The parameters act as the variable of which the function can be called numerous times with different or similar results rather than resulting in only 1 result.
Parameters allow us to use a single tool to create variants for our code for the code to be able to do something different

36
Q

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

A

The return statement can cause the function to produce a value.
It also exits the function, so no code after the return statement is executed

37
Q

Why do we log things to the console?

A

To see the state of things, it gives a way for us to talk to javascript

38
Q

What is a method?

A

Method is a function stored into a property of an object.

39
Q

How is a method different from any other function?

A

A method is not a free floating function.
They have be provided context of where they are going to be doing the action
Must state object.

40
Q

How do you remove the last element from an array?

A

pop() method

41
Q

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

A

Math.floor() method

42
Q

How do you generate a random number?

A

Math.random() method
Creates a number somewhere between 0 - 1 (not including 1)
Supposed to be a % which is used to multiply by other numbers, where you actually get the randomized numbers between the number provided

43
Q

How do you delete an element from an array?

A

splice( start, how many, optional what to add ) method

44
Q

How do you append an element to an array?

A

push() method (add to the end)

45
Q

How do you break a string up into an array?

A

split() method and splits it at the argument that is passed.
That’s why when we passed a space character(‘ ‘) as an argument, it split at the space acting like a chopping knife splitting

46
Q

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

A

No.
We can double check by console.log to see if the original is still there.

47
Q

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

A

50(?) A LOT

48
Q

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

A

Not always.
pop() can return what you just took off at the end, but you’re taking it off because you want to remove it from the array.
But if you wanted to remove it and then use it elsewhere, then you can return it somewhere else.

49
Q

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

A

50+ A LOT

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

Being able to branching actions and making decisions
Checking if something is present or not

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

Use the logical operators &&, || in between the condition

58
Q

What is the purpose of a loop?

A

Loops can run the same function numerous times until it returns false.
Helps do multiple codes in shorter code instead of having to write out every transaction

59
Q

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

A

This limits how many times the loop will run

60
Q

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

A

It’s when the code will be run again from the beginning to end

61
Q

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

A

Before each pass through the loop

62
Q

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

A

Once before the loop begins

63
Q

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

A

Before each loop iteration , at which if evaluates to true, the statement is executed. If false, it exits the loop and goes to the first statement after the for construct.

64
Q

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

A

At the end of each loop iteration, before the next evaluation of condition expression

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

It increases by 1
Means +1

67
Q

How do you iterate through the keys of an object?

A

Use the for… in loop
For (var key in object) {
statement
}

68
Q

What is JSON?

A

JavaScript Object Notation

69
Q

What are serialization and deserialization?

A

Serialization is converting an object to a string (stream of bytes)
Deserialization is converting a string (stream of bytes) to the native object (array, object, etc)

70
Q

Why are serialization and deserialization useful?

A

This is useful when you want to transmit data across a network or web applications
It converts the object in memory into a stream of bytes that can easily be stored on a disk or sent over a network and vice versa
Strings are understood in all languages, so it makes the data easily transmittable

71
Q

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

A

JSON.stringify(object);

72
Q

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

A

JSON.parse(string);

73
Q

How do you store data in localStorage?

A

storage.setItem(keyName, keyValue);
keyName (variable name, kebob case like ‘shopping-cart-case’) should be a string
keyValue should also be a string

74
Q

How do you retrieve data from localStorage?

A

storage.getItem(keyName);
keyName should be a string

75
Q

What data type can localStorage save in the browser?

A

Strings only
So objects, arrays must all be stringify to strings

76
Q

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

A

Right when the tab/window is about to close and then reload, includes refreshing